Can't get results from boolean to appear correctly - java

My program asks the user for a number and then decides if the number is between the range of two randomly generated numbers or outside of it. Everything works fine, except the program keeps giving the result that the guessed number is outside the range, even when it is inside the range. Not sure how to get the answer to show correctly. Boolean result = true is there since a "Cannot find symbol" error appears if it is not.
Code:
public static int getValidGuess(Scanner get)
{
int num;
System.out.print("Guess a number: --> ");
num = get.nextInt();
return num;
} // getValidGuess end
public static boolean displayGuessResults(int start, int end, int num)
{
int n1, n2;
boolean result = true;
Random gen = new Random();
n1 = gen.nextInt(99) + 1;
n2 = gen.nextInt(99) + 1;
if(n1 < n2)
{
start = n1;
end = n2;
} // if end
else
{
start = n2;
end = n1;
} //else end
if(num > start && num < end){
result = true;
System.out.println("\nThe 2 random numbers are " + start +
" and " + end);
System.out.println("Good Guess!");
} //if end
if(num < start || num > end){
result = false;
System.out.println("\nThe 2 random numbers are " + start +
" and " + end);
System.out.println("Outside range.");
} //if end
return result;
} // displayGuessResults end
public static void main(String[] args) {
// start code here
int start = 0, end = 0, num = 0, input;
Scanner scan = new Scanner(System.in);
String doAgain = "Yes";
while (doAgain.equalsIgnoreCase("YES")) {
// call method
input = getValidGuess(scan);
displayGuessResults(start, end, num);
System.out.print("\nEnter YES to repeat --> ");
doAgain = scan.next();
} //end while loop
} //main end

Your displayGuessResult should be improved:
public static boolean displayGuessResults(int num) {
boolean result = true;
Random gen = new Random();
int n1 = gen.nextInt(99) + 1;
int n2 = gen.nextInt(99) + 1;
int start = Math.min(n1, n2);
int end = Math.max(n1, n2);
System.out.println("\nThe 2 random numbers are " + start + " and " + end);
if(num >= start && num <= end){
result = true;
System.out.println("Good Guess!");
} else {
result = false;
System.out.println("Outside range.");
}
return result;
} // displayGuessResults end
and you have to call it using input read from the Scanner:
input = getValidGuess(scan);
displayGuessResults(input);

Related

How to fix variable not equaling user input

My program asks the user for a number and then validates if the number is either within the range of two randomly generated numbers or outside the range. The variable num is supposed to be the user's guess, but it keeps equating to 0. I'm uncertain if it has to do with the num = 0 in main, which is there because I get a "variable might not of been initialized" error if the = 0 is not there.
Code:
public static int getValidGuess(Scanner get)
{
int num;
System.out.print("Guess a number: --> ");
num = get.nextInt();
return num;
} // getValidGuess end
public static boolean displayGuessResults(int start, int end, int num)
{
boolean result;
Random gen = new Random();
int n1 = gen.nextInt(99) + 1;
int n2 = gen.nextInt(99) + 1;
if (n1 < n2){
start = n1;
end = n2;
} //if end
else
{
start = n2;
end = n1;
} //else end
System.out.println("\nThe 2 random numbers are " + start + " and " + end);
System.out.println("User Guess is " + num);
if(num >= start && num <= end){
result = true;
System.out.println("Good Guess!");
}
else if(num < start || num > end){
result = false;
System.out.println("Outside Range.");
}
else{
result = false;
}
return result;
} // displayGuessResults end
public static void main(String[] args) {
// start code here
int start = 0, end = 0, num = 0;
Scanner scan = new Scanner(System.in);
String doAgain = "Yes";
while (doAgain.equalsIgnoreCase("YES")) {
// call method
getValidGuess(scan);
displayGuessResults(start, end, num);
System.out.print("\nEnter YES to repeat --> ");
doAgain = scan.next();
} //end while loop
} //main end
Variables in different functions aren't magically the same just because they have the same name. If you want to be able to share variables without passing them as parameters or return values, then you need to declare them in the class instead.
Specifically, here's your two choices. Choice 1 (recommended): change getValidGuess(scan); to num = getValidGuess(scan);. Choice 2: put public static int num = 0; right in your class, outside all of your functions, and remove the declarations of num from all of your functions.

How to find factorial and show result of counting in console?

public class Car {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
System.out.println(n+"!="+factorial(n));
}
public static int factorial(int num) {
return (num == 0) ? 1 : num * factorial (num - 1);
}
}
how make this code to text in console 3! = 1*2*3 = 6?
Don't use recursion for this. Besides, it isn't really efficient or necessary.
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int fact = 1;
String s = n + "! = 1";
for (int i = 2; i <= n; i++) {
fact *= i;
s += "*" + i;
}
s += " = ";
System.out.println(s + fact);
There can be many ways to do it e.g. you can build the required string or print the trail while calculating the factorial. In the following example, I have done the former.
As an aside, you should check the input whether it is a positive integer.
import java.util.Scanner;
public class Car {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int n = in.nextInt();
if (n >= 0) {
StringBuilder strFact = new StringBuilder();
int fact = factorial(n, strFact);
if (strFact.length() > 0) {
// Delete the last '*'
strFact.deleteCharAt(strFact.length() - 1);
System.out.println(n + "!= " + strFact + " = " + fact);
} else {
System.out.println(n + "!= " + fact);
}
} else {
System.out.println("This is an invalid input.");
}
}
public static int factorial(int num, StringBuilder strFact) {
int fact;
if (num == 0) {
fact = 1;
} else {
fact = num * factorial(num - 1, strFact);
strFact.append(num + "*");
}
return fact;
}
}
A sample run:
Enter an integer: 3
3!= 1*2*3 = 6

Descending Fibonacci numbers using an iterator

So my code essentially works, at least how I expected. But my issue is my Fibonacci numbers are supposed to be descending not ascending. The reason I am struggling with this is because the starting number is user input, so unlike ascending I don't know what the first step is everytime. I considered just loading the ints into an array, sorting and printing them, but I feel like that defeats the purpose of the iterator... Any thoughts? Here is my code:
import java.util.Iterator;
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.*;
import java.text.DateFormat;
public class Conversion1
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int choice = 0;
int prime = 0, p = 0;
int fib = 0, f = 0;
long pTime = 0;
long fTime = 0;
long pETime = 0;
long fETime = 0;
int ff[];
long start = System.currentTimeMillis();
while (choice != 3)
{
System.out.println("Enter 1 or 2 to print out Fibonacci or Prime number iterator or enter 3 to exit.");
System.out.println("1 - Fibonacci Number Iterator");
System.out.println("2 - Prime Number Iterator");
System.out.println("3 - Exit");
choice = scanner.nextInt();
switch (choice)
{
case 1:
fTime += System.currentTimeMillis();
System.out.println("Enter the Max value");
int maxInt2 = scanner.nextInt();
ff = new int[maxInt2];
Iterator iterator = new FibonacciIterator(maxInt2);
while (iterator.hasNext())
{
ff[f] = (int) iterator.next();
f++;
}
for (int i = 0; i < f; i++)
System.out.println(ff[i]);
fETime += System.currentTimeMillis();
fib++;
break;
case 2:
pTime += System.currentTimeMillis();
System.out.println("Enter the Max value");
int maxInt = scanner.nextInt();
Iterator iterator2 = new PrimeIterator(maxInt);
while (iterator2.hasNext())
{
System.out.println(iterator2.next());
p++;
}
System.out.println("\n");
pETime += System.currentTimeMillis();
prime++;
break;
case 3:
long pFinal = pETime - pTime;
long fFinal = fETime - fTime;
double fseconds = ((fFinal / 1000) % 60);
double pseconds = ((pFinal / 1000) % 60);
System.out.println("\n");
System.out.println(fib + " Fibonacci commands yielding " + f + " individual outputs requiring "
+ fseconds + " seconds.");
System.out.println("\n");
System.out.println(prime + " Prime commands yielding " + p + " individual outputs requiring " + pseconds
+ " seconds.");
System.out.println("\n");
long end = System.currentTimeMillis();
DateFormat df = new SimpleDateFormat("HH':'mm':'ss");
System.out.println("Program started at " + df.format(new Date(start)) + " and terminated at: "
+ df.format(new Date(end)));
System.out.println("\n");
System.out.println("Program Ended");
System.out.println("\n");
break;
default:
System.out.println("Invalid Input");
}
}
}
static class PrimeIterator implements java.util.Iterator
{
private int limit = 0;
private int current;
private int a_number;
public PrimeIterator(int current)
{
this.current = current;
}
#Override
public Integer next()
{
return current;
}
static boolean isPrime(int number)
{
for (int divisor = 2; divisor < number; divisor++)
if (number % divisor == 0)
return false;
return true;
}
#Override
public boolean hasNext()
{
current--;
while (true)
{
if (isPrime(current))
break;
current--;
}
if (current <= limit)
return false;
else
return true;
}
}
static class FibonacciIterator implements java.util.Iterator
{
private int limit;
private int current = 1;// -1,1,0,1,1,2,3,5
private int prev = -1;
public FibonacciIterator(int limit)
{
this.limit = limit;
}
#Override
public Integer next()
{
return current;
}
#Override
public boolean hasNext()
{
int temp = current;
current = current + prev;// -1+1=0
prev = temp;
if (current >= limit)
return false;
else
return true;
}
#Override
public void remove()
{
throw new UnsupportedOperationException("Method not supported");
}
}
}
If you have to reverse the order, can you not just flip the order of the Fibonacci values captured?
for (int i = f - 1; i >= 0; i--)
System.out.println(ff[i]);
As opposed to what you have where you start at index 0.
for (int i = 0; i < f; i++)
System.out.println(ff[i]);

Java: How to use binary search with an array

For this java program, I'm trying to use a binary search for the array I have created in the Class Numbers, however, when I enter the 4th choice, the program just ends. The method binSearch is in Class Numbers. I can enter the required the size of the array, generate random numbers, search for specific numbers, and display them. However, when I want to use a binary search, the program ends as previously said. What is the reason why the program ends and what needs to be done to fix that certain method?
public class Numbers {
int[] array;
private int sizeOfArray;
public Numbers() {
sizeOfArray = 0;
array= new int [sizeOfArray];
}
public Numbers(int sizeOfArray) {
this.sizeOfArray = sizeOfArray;
array= new int [sizeOfArray];
}
public void generateNumbers() {
Random randomNumber = new Random();
int theNumber = 0;
for (int i = 0; i < sizeOfArray; i++) {
theNumber = randomNumber.nextInt(50);
array[i] = theNumber;
}
}
public int count(int num) {
int theNumbers = 0;
for (int i = 0; i < sizeOfArray; i++) {
if (array[i] == num) {
theNumbers++;
}
}
return theNumbers;
} // end count method
public String toString() {
String myArray = "";
for (int i = 0; i < sizeOfArray; i++) {
myArray += array[i] + " ";
}
return myArray;
}
public int binSearch(int[] array, int key) {
int low = 0;
int high = sizeOfArray - 1;
//int middle = (low + high + 1) /2;
int location = -1;
while (high >= low) {
int middle1 = (low + high) / 2;
if (array[middle1] == key ) {
//return true;
}
if (array[middle1] < key) {
low = middle1 + 1;
}
if (array[middle1] > key) {
high = middle1 - 1;
}
}
//return false;
return location;
}
}
and here is the main menu:
boolean isDone = false;
String input = null;
Numbers theNumber = new Numbers();
Scanner scanner = new Scanner (System.in);
try {
while (isDone == false) {
/*Menu options */
System.out.println("Enter 1 to create array size");
System.out.println("Enter 2 to generate random numbers");
System.out.println("Enter 3 to search and display number of occurrences");
System.out.println("Enter 4 to binary search to find whether specific number exists");
System.out.println("Enter 5 to display the array");
System.out.println("Enter 6 to quit the program");
input = scanner.nextLine();
switch (input) {
case "1":
int intNumber1 = 0;
System.out.println("Enter required size:");
intNumber1 = Integer.valueOf(scanner.nextLine());
theNumber = new Numbers(intNumber1);
System.out.println("Array has been generated.");
break;
case "2":
//theNumber = new Numbers();
theNumber.generateNumbers();
System.out.println("Numbers have been generated and stored.");
break;
case "3":
int intNumber2 = 0;
System.out.println("Enter number to search for: ");
intNumber2 = Integer.valueOf(scanner.nextLine());
System.out.println("Number of occurences of " + intNumber2 + " in the array is " + theNumber.count(intNumber2) + ".");
break;
case "4":
int key = 0;
theNumber.binSearch(null, key);
System.out.println("Array is sorted: ");
break;
case "5":
int theNumbers = 0;
if (theNumbers == 0)
{
System.out.println("No array has not been generated yet.");
}
else
{
System.out.println("The numbers are: ");
}
System.out.println(theNumber.toString());
break;
case "6":
isDone = true;
System.out.println("Bye... See you again");
scanner.close();
break;
default:
System.out.println("These are invalid choices...please reenter.");
break;
}
}
}
catch (Exception exception)
{
System.out.println("This is an invalid choice...please reenter.");
scanner.nextLine();
return;
}

Sum of Numbers (expected output issue)

I am having issues with my program. The point is that the user needs to enter a starting number (ex:1) and an ending number (ex:5), the output should be 1+2+3+4+5=15. I, on the other hand, am getting 1+2+3+4+5+. I am unsure as to how to fix it, any help would be greatly appreciated. Thanks!Also is there anyway to not return null? My program keeps asking me to return it to null.
import java.util.Scanner;
public class SumOfNumbers
{
public String getSum(int start, int end)
{
int sum=0;
Scanner scanner = new Scanner(System.in) ; //Scanner used to get keyboard values
System.out.println("Enter your starting number: ");
start = scanner.nextInt(); //first number
System.out.println("Enter your ending number: ");
end =scanner.nextInt(); //second number
for(int i=start;i<=end;i++)
{
sum=sum+i; //calculating sum
scanner.close();
System.out.print(i + "+");
}
return null;
}
}
You currently output + after every number, instead output the first number and then start looping (but in the loop, output a + before every number). Also, don't forget to output the sum after your loop. Something like
int sum = start;
System.out.print(start);
for (int i = start + 1; i <= end; i++) {
System.out.print("+" + i);
sum += i;
}
System.out.println(" = " + sum);
However, since you apparently need to return this result as a String you should be doing something like
int sum = start;
StringBuilder sb = new StringBuilder();
sb.append(start);
for (int i = start + 1; i <= end; i++) {
sb.append("+").append(i);
sum += i;
}
sb.append("=").append(sum);
// System.out.println(sb.toString());
return sb.toString();
And you should probably be using the start and end values you pass to the function (instead of ignoring them and prompting the user in the function).
I have tried to add both of the outputs together in the return statement by making them into strings but no luck
The code above is logically equivalent to (but more efficient then)
int sum = start;
String result = String.valueOf(start);
for (int i = start + 1; i <= end; i++) {
result = result + "+" + i;
sum = sum + i;
}
result = result + "=" + sum;
// System.out.println(result);
return result;
It could also be written more concisely like
int sum = start;
String result = String.valueOf(start);
for (int i = start + 1; i <= end; i++) {
result += "+" + i;
sum += i;
}
result += "=" + sum;
// System.out.println(result);
return result;
Or, if we're using Java 8+, with lambdas like
StringBuilder sb = new StringBuilder(String.valueOf(start));
int sum = IntStream.rangeClosed(start, end).sum();
IntStream.rangeClosed(start + 1, end).forEach(x -> sb.append("+").append(x));
sb.append("=").append(sum);
// System.out.println(sb);
return sb.toString();
hope this will help
class SumOfNumbers
{
public void getSum(int start, int end)
{
int sum=0;
Scanner scanner = new Scanner(System.in) ; //Scanner used to get keyboard values
System.out.println("Enter your starting number: ");
start = scanner.nextInt(); //first number
System.out.println("Enter your ending number: ");
end =scanner.nextInt(); //second number
for(int i=start;i<=end;i++)
{
sum=sum+i; //calculating sum
scanner.close();
if (i==end) {
System.out.print(i );
}
else {
System.out.print(i + "+");
}
}
System.out.print("="+ sum);
}
}
output
Enter your starting number:
1
Enter your ending number:
15
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15=120
Here is a method that will do what you are asking for:
public String output(int startNum, int endNum) {
String reply = "";
int answer = 0;
for (int x = startNum; x <= endNum; x++) {
if (x == startNum) reply = "" + x;
else if (x == endNum) reply = reply + "+" + x + "=";
else reply = reply + "+" + x;
answer += x;
}
reply = reply + answer;
return reply;
}
Here is the output:
System.out.println(output(1,10));
1+2+3+4+5+6+7+8+9+10=55

Categories