I am trying to run a couple of functions in my main and It won't output anything if there is anything that you see wrong please help me out. I have to use simple, sometimes slower ways because we can use what we haven't learned yet. Here is my code.
package a3;
import java.util.Scanner;
public class LoopsAndImages {
public static void main(String[] args){
String test = "A rabbit has a carrot";
Boolean numbers = hasMoreEvenThanOdd("12344");
System.out.println(test);
System.out.println(hideLetterA(test)+" This is the new string");
System.out.println(numbers);
System.out.println(makeTextTriangle(5));
}
public static String hideLetterA(String sentence) {
Scanner s=new Scanner(sentence);
while(s.hasNext()) {
for(int i=0; i<sentence.length();i++) {
char c=sentence.charAt(i);
int charValue = (int) c;
if(charValue==65 || charValue==97) {
c='*';
}
}
}
s.close();
return sentence;
}
public static Boolean hasMoreEvenThanOdd(String sentence) {
int x=Integer.parseInt(sentence);
if((x%2)==0) {
return true;
}else {
return false;
}
}
public static String makeTextTriangle(int a) {
String stars="";
for(int i=0; i<a; i++) {
for(int j=0; j<i;j++) {
stars="* ";
}
System.out.println();
}
return stars;
}
}
package a3;
import java.util.Scanner;
public class LoopsAndImages {
public static void main(String[] args) {
String test = "A rabbit has a carrot";
Boolean numbers = hasMoreEvenThanOdd("12344");
System.out.println(test);
System.out.println(hideLetterA(test) + " This is the new string");
System.out.println(numbers);
System.out.println(makeTextTriangle(5));
}
public static String hideLetterA(String sentence) {
String result = "";
for(int i = 0; i < sentence.length(); i++) {
if(sentence.charAt(i) == 'a' || sentence.charAt(i) == 'A') {
result += "*";
} else {
result += sentence.charAt(i);
}
}
return result;
}
public static Boolean hasMoreEvenThanOdd(String sentence) {
int x = Integer.parseInt(sentence);
int evenCount = 0, oddCount = 0, num;
while (x > 0) {
num = x % 10;
if(num % 2 == 0) evenCount++;
if(num % 2 == 1) oddCount++;
x /= 10;
}
return evenCount > oddCount;
}
public static String makeTextTriangle(int a) {
String stars = "";
for (int i = 0; i < a; i++) {
for (int j = 0; j <= i; j++) {
stars += "*";
}
stars += "\n";
}
return stars;
}
}
I am trying to run a couple of functions in my main and It won't output...
Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.print("There are 5 humans.\n"); array(); } public static String[] array() { //Let the user...
This java code won't run and I can't figure out the problem with it. Please help import java.io.*; import java.util.*; import java.math.*; import java.util.Scanner; public class Fibonacci { // Returns n-th Fibonacci number static BigInteger fib(int n) { BigInteger[] Fibo = new BigInteger[n+2]; Fibo[0] = BigInteger.ZERO; Fibo[1] = BigInteger.ONE; for (int j=2 ; j<=n ; j++) { Fibo[j] = Fibo[j-1].add(Fibo[j-2]); } return (Fibo[n]); }...
I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact { public static void main(String[] args) { Random Rand = new Random(); Scanner sc = new Scanner(System.in); System.out.println("welcome to the contact application"); System.out.println(); int die1; int die2; int Total; String choice = "y";...
Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...
import java.util.Scanner; public class SieveOfEratosthenes { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int num = sc.nextInt(); boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false;...
Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter a word:"); String word = kb.next(); String reverse = ""; for (int i=word.length()-1; i>=0; i--) reverse += word.charAt(i); boolean result = reverse.equalsIgnoreCase(word); if (result) System.out.println("The word " +word+ " is a Palindrome."); else System.out.println("The word " +word+ " is not a Palindrome."); } } Rewrite the program so that the main method is: public static void...
I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation problem: at EvenOdd.main(EvenOdd.java:10) I am unable to find what I can do to fix this issue. Can you please assist? My code is below: import java.util.InputMismatchException; import java.util.Scanner; public class EvenOdd { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Welcome to this Odd program!"); int userInput1 = input.nextInt(); } public...
JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in); System.out.println("Seed:"); int seed = scanner.nextInt(); System.out.println("Length"); int length = scanner.nextInt(); Random random...
(Reading & Writing Business Objects) I need to have my Classes be able to talk to Files. How do I make it such that I can look in a File for an account number and I am able to pull up all the details? The file should be delimited by colons (":"). The Code for testing 'Select' that goes in main is: Account a1 = new Account(); a1.select(“90001”); a1.display(); Below is what it should look like for accounts 90000:3003:SAV:8855.90 &...
I was wanting to know how would I call my add class through my if statement. Here is my class: import java.util.Scanner; public class Main { public static void main(String[] args) { int num1 = 0; int num2 = 0; int answer = 0; int userInput = 0; Scanner sc = new Scanner(System.in); System.out.println("Hello, this is a program where you can choose...