Why does my Java calculator pause at loop? - java

After calculator() has ran, my program pauses...unless I enter an input to unpause the program. Then it continues to run and it prints out. However the input that I entered earlier to unpause the program is stored into answer. Please read my comments in the code to have a better understanding. If you still don't understand then feel free to copy the code to see what I am talking about.
public static void main(String[] args) {
boolean repeat = true;
while (repeat){
calculator(); //-Program pauses after this has ran.
System.out.println("Do you wish to repeat(y/n)?"); // This does not appear unless I enter an input.
String answer = userInput.next(); //The input that I entered earlier to unpause the program gets stored into answer.
if (answer.equalsIgnoreCase("y")){
repeat = true;
} else { repeat = false;}}} //Program terminates here because the input that I used to unpause the program isn't "y".
Full code below:
package calculatorAttempt;
import java.util.Scanner;
class CalculatorV2 {
static Scanner userInput = new Scanner(System.in);
public static void calculator(){
System.out.print(":");
if (userInput.hasNextInt()){
int num1 = userInput.nextInt();
System.out.print(":");
String inString = userInput.next();
System.out.print(":");
int num2 = userInput.nextInt();
System.out.print("=");
if (inString.equals("+")){
System.out.print(num1+num2);}
if (inString.equals("-")){
System.out.print(num1-num2);}
if (inString.equals("*")||(inString.equalsIgnoreCase("x"))){
System.out.print(num1+num2);}
if (inString.equals("/")){
float intTofloat = (float)num1/num2;
System.out.println(intTofloat);} }//If Integer
if (userInput.hasNextFloat()){
float num1 = userInput.nextFloat();
System.out.print(":");
String inString = userInput.next();
System.out.print(":");
float num2 = userInput.nextFloat();
System.out.print("=");
if (inString.equals("+")){
System.out.print(num1+num2);}
if (inString.equals("-")){
System.out.print(num1-num2);}
if (inString.equals("*")||(inString.equalsIgnoreCase("x"))){
System.out.print(num1*num2);}
if (inString.equals("/")){
System.out.print(num1/num2);} }//If Float
}//Public Void Calculator
public static void main(String[] args) {
boolean repeat = true;
while (repeat){
calculator();
System.out.println("Do you wish to repeat(y/n)?");
String answer = userInput.next();
if (answer.equalsIgnoreCase("y")){
repeat = true;
} else { repeat = false;}}
}//Main
}//Class
I am beginner so please bear with me :^) . Thanks.

This happens because of this line:
if (userInput.hasNextFloat()){
The hasNext...() methods in Scanner are blocking methods. They block if there is no input other than white space in the scanner, waiting for something to be entered. As soon as something real (not spaces or newlines) is entered, then they check whether it is a float, an int or whatever, and return a true/false reply to you.
After you finish an integer calculation, your program calls hasNextFloat(), and therefore, it blocks, waiting until you enter something. If that something is not a float, it will return false, and the if will not work.
You can experiment a little and see:
If you run your program and start with a float (say, 17.2), the program will show you the Do you wish to repeat? question after it calculates the result.
If you run your program and start with an int, and then, after you get the result, enter a float, it will print the : that is asking you for the float operator.
So basically, that should not be an if. It should be an else if structure.

Just use else if instead of if to check userInput.hasNextFloat().
Your code sample will look like.
if (userInput.hasNextInt()){
int num1 = userInput.nextInt();
....
....
....
} else if (userInput.hasNextFloat()){
float num1 = userInput.nextFloat();
....
....
...
}

You must use hasNext() instead of hasNextInt() and remove the if(hasNextFloat()) part. And use the Number class because it accepts both ints and floats:
if (userInput.hasNext()) {
Number num1 = userInput.nextByte();
System.out.print(":");
String inString = userInput.next();
System.out.print(":");
Number num2 = userInput.nextByte();
System.out.print("=");
switch (inString) {
case "+":
System.out.print(num1.doubleValue() + num2.doubleValue());
break;
case "-":
System.out.print(num1.doubleValue() - num2.doubleValue());
break;
case "*":
case "x":
case "X":
System.out.print(num1.doubleValue() * num2.doubleValue());
break;
case "/":
double intTofloat = num1.doubleValue() / num2.doubleValue();
System.out.println(intTofloat);
break;
default:
System.out.println(INVALID OPERATOR!);
break;
}
}

Related

do while loop with a Yes/No user prompt

Im having issues with my code. The code is to find a factorial of a number, then ask if you want to run the program again, its suppose to run again then exit. However, when I enter Y to restart the program it breaks and wont restart and when I enter N to exit it wont exit the program.
private static Object Cont;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Greetings
System.out.println("Welcome to my factorial program! ");
System.out.println("Please choose from the following: ");
//Menu
System.out.println("1. Run Program");
System.out.println("2. Exit Program");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("This program will determine the factorial value of positive integers.");
do {
System.out.println("The starting number is 1.");
System.out.println("Please enter an ending integer value:");
int n = scanner.nextInt();
for (int i = 1; i <= n; i++) {
System.out.println(i + "! = " + fact(i));//call to function
}
System.out.println("Run factorial program again? (Y for Yes, N for No): ");
String Cont = scanner.next();
if (Cont.equals("N")) {
break;
}
} while (Cont.equals("Y"));// do while loop
break;
//Menu Exit
case 2:
System.out.println("Thank you for using the program.");
System.out.println("Goodbye");
default:
System.exit(1); // remebered from last week to set this to one
System.out.println("Goodbye");
break;
}
}//Factorial Math
static long fact(int x) {
long f = 1;
for (int i = 1; i <= x; i++) {
f = f * i;
}
return f;
} //End Main Method
What am I missing or doing wrong?
You have a couple of problems here.
The first problem is that you have declared two distinct Cont variables. The first one is a static field. The second one is a local variable that is declared in the body of the loop.
I don't know why you declared the static field, but I imagine you did it because the } while (Cont.equals("Y")); didn't compile without it. (That is because the Cont variable declared in the loop is not in scope outside of the loop's body.) Unfortunately, it was not the correct solution. Because, you now have code that is assigning to one variable and tests a different one. Naturally, that doesn't work.
To my mind, the correct solution is to get rid of the static field, and the declaration in the loop body. Than add a declaration for Cont before the start of the loop. (It shouldn't have an initialization). Finally, in the loop you just need to read (using the scanner) and assign a string to Cont so that you can test in the loop condition.
The second problem is that you have a redundant test in there. If you are going to test to see if you need to continue using } while (Cont.equals("Y")); you don't also need to test if Cont is "N" and break.
Relatedly, equals("Y") is not the same as not equals("N"). (Consider "Hello" ... or "n". They are neither "Y" or "N".) So if you really want to stop the loop when the user types N, then the loop termination condition should be:
} while (!Cont.equals("N")); // keep looping while not 'N'
Finally there are a couple of significant style-related issues.
Declaring a static field is usually a mistake.
It is a mistake to use a field when you should be using a local variable. State that is only relevant to a single execution of a method should be1 represented using a local variable.
It is a major stylistic error for a variable to start with an uppercase letter. Cont should be cont.
If you ever work in a professional Java development team that pays attention to style, you will get a hard time for ignoring Java identifiers conventions. And (IMO) your teacher should dock style marks for this mistake.
1 - Reasons: 1) It makes the method harder to read because the variable declaration is further away from its use. 2) It typically makes the code non-reentrant. 3) It opens you up to unwanted coupling between methods; e.g. if two methods accidentally share the same "local variable declared as a field". 4) In many cases it uses more memory.
You need an additional break and declare Cont before the do loop:
//private static Object Cont; This is is not declared on the right location, we'll declare it later
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Greetings
System.out.println("Welcome to my factorial program! ");
System.out.println("Please choose from the following: ");
//Menu
System.out.println("1. Run Program");
System.out.println("2. Exit Program");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("This program will determine the factorial value of positive integers.");
String Cont = null; // Cont must be declared here
do {
System.out.println("The starting number is 1.");
System.out.println("Please enter an ending integer value:");
int n = scanner.nextInt();
for (int i = 1; i <= n; i++) {
System.out.println(i + "! = " + fact(i));//call to function
}
System.out.println("Run factorial program again? (Y for Yes, N for No): ");
Cont = scanner.next();
if (Cont.equals("N")) {
break;
}
} while (Cont.equals("Y"));// do while loop
break;
//Menu Exit
case 2:
System.out.println("Thank you for using the program.");
System.out.println("Goodbye");
break; // requires this additional break
default:
System.exit(1); // remembered from last week to set this to one
System.out.println("Goodbye");
break;
}
}//Factorial Math
static long fact(int x) {
long f = 1;
for (int i = 1; i <= x; i++) {
f = f * i;
}
return f;
} //End Main Method

How to correct the scope of a variable that is created within a do-while loop?

I am struggling to get the correct scope for my variable "input".
I am making a calculator for a university task, and I've got everything working apart from when I tried to make it loop by wrapping my main code in a do-while loop. Because the variable "input" is declared in the "do" part of the loop, it didn't know what it was when I was trying to use it in the "while" condition. To fix this I then declared "input" as a string before my do-while loop to make it a global. However, now the scanner that takes the value of input will not work.
AM I doing something stupid or am I missing something?
import java.util.Scanner;
public class Calculator {
public static void main(String [] args) {
String input;
do {
System.out.println("Welcome to the calculator. Please enter an operator (+, -, /, *) below:");
Scanner myScanner = new Scanner(System.in);
String oper = myScanner.nextLine();
System.out.println("Now please enter two numbers:");
double a = myScanner.nextDouble();
double b = myScanner.nextDouble();
switch (oper) {
case "+" :
System.out.println(CalculatorUtils.add(a, b));
break;
case "-" :
System.out.println(CalculatorUtils.subtract(a, b));
break;
case "/" :
System.out.println(CalculatorUtils.divide(a, b));
break;
case "*" :
System.out.println(CalculatorUtils.multiply(a, b));
break;
}
System.out.println("Do you want to complete another calculation? (y/n)");
input = myScanner.nextLine();
}
while (input.contentEquals("y"));
}
}
I expect this to be the output:
Welcome to the calculator. Please enter an operator (+, -, /, *) below:
+
Now please enter two numbers:
32.5
12.5
45.0
Do you want to complete another calculation? (y/n)
y
(This is where the code would start again)
However I'm not being able to enter my input when being asked if I would like to do another calculation.
Here is the fix.
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
String input;
do {
System.out.println("Welcome to the calculator. Please enter an operator (+, -, /, *) below:");
Scanner myScanner = new Scanner(System.in);
String oper = myScanner.nextLine();
System.out.println("Now please enter two numbers:");
double a = myScanner.nextDouble();
double b = myScanner.nextDouble();
switch (oper) {
case "+":
System.out.println(CalculatorUtils.add(a, b));
break;
case "-":
System.out.println(CalculatorUtils.subtract(a, b));
break;
case "/":
System.out.println(CalculatorUtils.divide(a, b));
break;
case "*":
System.out.println(CalculatorUtils.multiply(a, b));
break;
}
myScanner.nextLine();
System.out.println("Do you want to complete another calculation? (y/n)");
input = myScanner.nextLine();
myScanner.nextLine();
}
while (input.contentEquals("y"));
}
}
It happens because second time you call myScanner.nextLine() it just scans enter from before. It will happen after myScanner.nextDouble() but not after myScanner.nextLine() because myScanner.nextLine() reads/scans until including next newLine character (\n) whereas myScanner.nextDouble() will just scan a double and leave.
Here is similar thread
What you do not want to do is create a Scanner on every trip around the loop. Move the definition and initialization of your Scanner variable outside the loop:
String input;
Scanner myScanner = new Scanner(System.in);
do {
System.out.println("Welcome to the calculator. Please enter an operator (+, -, /, *) below:");
String oper = myScanner.nextLine();
// rest of loop...
} while (input.contentEquals("y"));
This may or may not solve you're immediate problem, but it's still the right thing to do in general.

How to fix loop while/try catch error in java

I am working on creating a simple calculator program (first week into this java programming).
problem background: only 5 options are valid. (1-add; 2- subtract; 3- multiple;4. divide; and 5.exit). when users enter 1-4 options, results will populate but users need to loop back to reenter data till option 5 is selected. 5 is to exit the program(the only way to end the loop/program). my questions: 1. how to stop try-catch from running nonstop? is there a better way to implement try-catch? e.g., handling string data error messages.Ideally, if a string is entered, the code should loop back to restart again by producing the message "please reenter the number..." until a valid number is entered by users 2. I am trying to use as many static methods as possible in the main class. i am not sure whether the way I did was accurate?
Here is the code I input:
12 2
//-everything works well.
2 //-enter again
s s (string entry-error)
then, the below message populates:
"You have entered invalid floats. please re-enter:
Exception in thread "main" java.util.InputMismatchException
...
at calculator.performSubtract(calculator.java:65)
at calculator.main(calculator.java:34)"
code(sample)
public class calculator {
//use static methods to implement the program
static Scanner userInput = new Scanner(System.in);
static int userChoice;
static float numberOne;
static float numberTwo;
static float answer;
static int choice;
public static void main(String[] args) {
do {
//this menu statement has to be repeated unless 5 is entered (exit the
//program)
System.out.println("Welcome to <dj's> Handy Calculator\n\n\t \1. Addition \n\t 2. Subtraction\n\t 3. Multiplication\n\t 4. Division\n\t 5. Exit\n\n");
System.out.print("What would you like to do? ");
try {
choice = userInput.nextInt();
}catch (InputMismatchException e) {
continue;
}
switch (choice) {
case 2: performSubtract();
break;
...
case 5: exit();
break;
}
}while(choice >0 && choice <5);
userInput.close();
}
public static void performSubtract () {
//catch error statement.
try {
numberOne = userInput.nextFloat();
numberTwo= userInput.nextFloat();
answer= numberOne-numberTwo;
} catch (Exception e) {
System.out.println("You have entered invalid floats. please re-enter: ");
numberOne = userInput.nextFloat();
numberTwo= userInput.nextFloat();
}
System.out.printf("Please enter two floats to subtraction, separated by a space: %.1f %.1f\n", numberOne, numberTwo);
System.out.printf("Result of subtraction %.1f and %.1f is %.1f\n", numberOne, numberOne, answer);
System.out.println("\nPress enter key to continue...");
}
}
I believe the issue is that you are not clearing the problem token from the scanner.
Your catch statement prints an error message, and then goes around to try and parse the same token into an int or float again.
You might check here: https://www.geeksforgeeks.org/scanner-nextint-method-in-java-with-examples/
It looks like you need to call userInput.next() to advance past the invalid token.
Also, hasNextInt() will let you avoid the catch altogether if you prefer.
Your error lies in the fact that Scanner.nextFloat, when reading an invalid input, does not advance the current token. This means that when you call nextFloat twice again in the catch statement, you once again read the tokens s and s, the first of which will cause an InputMismatchException to be thrown once again. You should change your performSubtract method to look something like this:
public static void performSubtract () {
//catch errors
System.out.println("Please enter two floats to subtraction, separated by a space");
userInput.nextLine();//ignore the new line
do {
try {
String[] nextLineTokens = userInput.nextLine().split("\\s+");
if(nextLineTokens.length != 2)
{
System.out.println("You have not entered two floats. please re-enter:");
continue;
}
numberOne = Float.parseFloat(nextLineTokens[0]);
numberTwo = Float.parseFloat(nextLineTokens[1]);
answer= numberOne-numberTwo;
break;
}
catch (Exception e) {
System.out.println("You have entered invalid floats. please re-enter: ");
}
} while (true);
System.out.printf("You entered: %.1f %.1f\n", numberOne, numberTwo);
System.out.printf("Result of subtraction %.1f minus %.1f is %.1f\n", numberOne, numberTwo, answer);
System.out.println("\nPress enter key to continue...");
userInput.nextLine();
}
Additionally, your parsing code continues if you enter an invalid input, but exits if you type in a number that is not 1-5. If it is the first time that you read in input, the code exits for invalid inputs as well. You should probably change your parse iteration loop as so:
public static void main(String[] args) {
while(choice != 5) {
//this menu statement has to be repeated unless 5 is entered (exit the
//program)
System.out.println("Welcome to <dj's> Handy Calculator\n\n\t 1. Addition \n\t 2. Subtraction\n\t 3. Multiplication\n\t 4. Division\n\t 5. Exit\n\n");
System.out.print("What would you like to do? ");
try {
choice = userInput.nextInt();
}
catch (InputMismatchException e) {
userInput.next();
continue;
}
switch (choice) {
case 2: performSubtract();
break;
// ...
case 5: exit();
break;
}
}
userInput.close();
}
To the first question: try-catch block are usually used to see if your code are running through without error. By what you explain what you are trying to do, I would instead use a while-loop before the assignment for numberOne and numberTwo whether the input was float or not like:
// pseudo
while(input != float || input2 != float)
{
print(please reenter)
}
numberOne = input
numberTwo = input2

Java validation error from user input

I have the following code, where the idea is that the user will input two numbers and the sum of the two will be calculated.
If an invalid value, e.g. a character is entered, an error message should be outputted but I keep getting errors
Java
package calculator;
import java.util.Scanner;
public class calculator {
/**
* #param args
*/
public static void main(String[] args) {
double n1, n2;
String operation;
Scanner scannerObject = new Scanner(System.in);
System.out.println("Enter first number");
n1 = scannerObject. nextDouble();
System.out.println("Enter second number");
n2 = scannerObject. nextDouble();
Scanner op = new Scanner(System.in);
System.out.println("Enter your operation");
operation = op.next();
switch (operation) {
case "+":
System.out.println("Your answer is " + (n1 + n2));
break;
case "-":
System.out.println("Your answer is " + (n1 - n2));
break;
case "/":
System.out.println("Your answer is " + (n1 / n2));
break;
case "*":
System.out.println("Your asnwer is " + (n1 * n2));
break;
default:
System.out.println("I do not know!");}
}
int function(){
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer between 1-100: ");
int range;
while(true){
if(input.hasNextInt()){
range = input.nextInt();
if(0<=range && range <= 100)
break;
else
continue;
}
input.nextLine(); //Comsume the garbage value
System.out.println("Enter an integer between 1-100:");
}
return range;
}
}
and these are the error messages I get:
Errors
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at calculator.calculator.main(calculator.java:14)
I've tried so many different things but can't get it to work as I want it.
Can anyone be of any assistance here?
Thanks for reading
This exception is thrown by an instance of the Scanner class to indicate that a retrieved token does not match the pattern for the expected type, or that the retrieved token is out of range.
You can see the documentation for the exception here: https://docs.oracle.com/javase/7/docs/api/java/util/InputMismatchException.html
Taken from documention on Scanner
double nextDouble()
Returns the next token as a long. If the next token is not a float or
is out of range, InputMismatchException is thrown.
I suspect that your not inputting your number correctly. Ensure that your input is of the correct format.
You should also set the locale of your scanner as some locales expect a comma , instead of a dot ., such as:
Scanner scanner = new Scanner(System.in).useLocale(Locale.US);
Your first two inputs should be numbers. If this is true, then it's probably the decimal mark for your numbers. You need a dot(.) not a comma (,)
It seems that you are not entering any integer as input.
You can solve this by handling the exception this way :
try {
if(input.hasNextInt()){
range = input.nextInt();
if(0<=range && range <= 100)
break;
else
continue;
}
input.nextLine();
}
catch (InputMismatchException e) {
input.nextLine();
}
Your issue is at,
scannerObject. nextDouble();
You are trying to get a double but entering a string. You will need to do some sort of a input validation like below to stop program from crashing incase of invalid inputs.
try {
System.out.println("Enter first number");
n1 = scannerObject. nextDouble();
}
catch(InputMismatchException inEx) {
System.out.println("Invalid input");
}
Then you may want to create a loop to get the input again and agin until valid input is detected.
Edit
You'll need to,
import java.util.InputMismatchException;
Also create a loop to get a valid input from a user. Something like below. This is just an example, you'll need to do something like this to work with your code. Also need to make sure n1 and n2 are initiated before you actually use their values.
boolean notValidInput = true;
while(notValidInput) {
try {
System.out.println("Enter first number");
n1 = scannerObject. nextDouble();
notValidInput = false;
}
catch(InputMismatchException inEx) {
System.out.println("Invalid input. Please try again!");
}
}

Trouble trying to restart my Java program

After looking up numerous ways to restart a Java program within itself, a while loop seemed like the easiest option. Here's an example of a basic calculator program I'm trying this with:
import java.util.Scanner;
class a {
public static void main(String args[]){
boolean done = false;
int oper;
Scanner input = new Scanner(System.in);
System.out.println("McMackins Calc v2.0 (Now with fewer crashes!)");
while (!done)
{
System.out.println("What operation? (0 for quit, 1 for add, 2 for subtract, 3 for multiply, 4 for divide, 5 for divide with remainder, 6 for average, 7 for account interest):");
while (!input.hasNextInt()){
System.out.println("Enter a valid integer.");
input.next();
}
oper = input.nextInt();
switch (oper){
case 0:
done = true;
break;
case 1:
add addObject = new add();
addObject.getSum();
break;
case 2:
sub subObject = new sub();
subObject.getDifference();
break;
case 3:
times multObject = new times();
multObject.getProduct();
break;
case 4:
divide divObject = new divide();
divObject.getQuotient();
break;
case 5:
remain remObject = new remain();
remObject.getRemainder();
break;
case 6:
avg avgObject = new avg();
avgObject.getAvg();
break;
case 7:
interest intObject = new interest();
intObject.getInterest();
break;
default:
System.out.println("Invalid entry.");
break;
}
}
input.close();
}
}
However, this seems to throw out a NoSuchElementException at the end of the first time through the loop, and crashes the program. The function of this class is to take the initial input from the user to determine which class to use, which will determine which mathematical operation to perform. Everything works fine without the while (!done) loop.
Example usage:
McMackins Calc v2.0 (Now with fewer crashes!)
What operation? (0 for quit, 1 for add, 2 for subtract, 3 for multiply, 4 for divide, 5 for divide with remainder, 6 for average, 7 for account interest):
1
How many addends?
1
Enter your numbers now.
1
You have entered 1 addend.
The sum is: 1.0
What operation? (0 for quit, 1 for add, 2 for subtract, 3 for multiply, 4 for divide, 5 for divide with remainder, 6 for average, 7 for account interest):
Enter a valid integer.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at a.main(a.java:13)
I've also tried just having the other classes refer back to this one, but since main is a static method, I cannot access it the way I intended.
Note that I'm a bit of a beginner at Java, which is why my program is pretty simple, so try to keep it simple if it can be, or post code and then in DETAIL explain what it means so I can not only fix this problem, but future ones as well.
Thank you!
EDIT:
The code is formatted better within my editor. The braces came out in odd positions when I posted it here.
Since apparently a is written correctly, this is my add class. Hopefully this will clear something up.
import java.util.Scanner;
public class add {
public void getSum(){
Scanner input = new Scanner(System.in);
double total, addend;
int entries, count;
total = 0;
count = 0;
System.out.println("How many addends?");
while (!input.hasNextInt()){
System.out.println("Enter a valid integer.");
input.next();
}
entries = input.nextInt();
System.out.println("Enter your numbers now.");
while (count < entries){
while (!input.hasNextDouble()){
System.out.println("Enter a valid number.");
input.next();
}
addend = input.nextDouble();
total = total + addend;
count++;
if (count == 1){
System.out.println("You have entered " + count + " addend.");
}else if (count > entries){
System.out.println("You have entered too many addends! Contact program developer.");
}else{
System.out.println("You have entered " + count + " addends.");
}
}
System.out.println("The sum is: " + total);
input.close();
}
}
public static void main(String args[]){
boolean done = false;
int oper;
Scanner input = new Scanner(System.in);
System.out.println("McMackins Calc v2.0 (Now with fewer crashes!)");
while (!done) {
System.out.println("What operation? (0 for quit, 1 for add, 2 for subtract, 3 for multiply, 4 for divide, 5 for divide with remainder, 6 for average, 7 for account interest):");
while (!input.hasNextInt()){
System.out.println("Enter a valid integer.");
input.next();
}
oper = input.nextInt();
switch (oper){
case 0:
done = true;
break;
case 1:
System.out.println("1");
break;
case 2:
System.out.println("2");
break;
case 3:
System.out.println("3");
break;
case 4:
System.out.println("4");
break;
case 5:
System.out.println("5");
break;
case 6:
System.out.println("6");
break;
case 7:
System.out.println("7");
break;
default:
System.out.println("Invalid entry.");
break;
}
}
input.close();
}
This seemed to work for me so perhaps the error is something to do with your own classes (add, divide) etc.
Also, it's best to keep with convention when creating your own classes by capitalizing the first letter e.g. "add" should be "Add".
You could probably make this a little bit easier to read by building a general "Operations" class which holds an add method, a subtract method etc.
EDIT:
try this for your add method:
public static int add() {
Scanner s = new Scanner(System.in);
int counter = 0;
System.out.println("How many numbers to add?");
int numCount = s.nextInt();
for(int i = 0; i < numCount; i++) {
System.out.println("enter number");
counter += s.nextInt();
}
return counter;
}
Use bufferedreader and inputstream instead of Scanner class. This class creates a lot of bugs and errors, since sometimes it takes more arguments, that you expect it to take.
Also:
while (!input.hasNextInt()){
System.out.println("Enter a valid integer.");
input.next();
}
Your using hasNextInt method wrong, instead of it try to make simple while loop with Boolean and input.next() should be replaced with input.nextLine().
Another thing, you should check,if user typed integer instead of string or something in the while loop and it range. If everything is okay, you should change Boolean value to true and make him go out of the while loop.
For future users who are wondering how to fix this issue, through some reprogramming, I discovered that my problem was closing the input variable BEFORE the end of the loop. By having the program restart indefinitely and only close input when done, this program works fine.
Thanks to Benjamin's response, I am currently in the process of cleaning up and shortening my code by way of for loops.

Categories