Hello,
I make a program loop..but it doesn't work as i want ><" !

this is the OUTPUT -look careful, please- :

Welcome to our miniCalculator
if you want to use add, enter add
if you want to use subtraction, enter sub
if you want to use division, enter div
if you want to use multiplication, enter mul
if you want to exit, enter bye
add
Please enter First number
4
Please enter Second number
5
4 + 5 = 9.0

if you want to use add, enter add
if you want to use subtraction, enter sub
if you want to use division, enter div
if you want to use multiplication, enter mul
if you want to exit, enter bye
Please enter First number

this is the code:

import java.util.Scanner;

public class MiniCalculator {
	String display; // for displaying calculation result.
	static String operation, store1;
	static double memory;
	static double store;
	static double number1,number2;
	static boolean trueOrfalse;

	public static void main(String [] args) {
 		Scanner in = new Scanner (System.in);
 		System.out.println ("Welcome to our miniCalculator");
 		
 		while(trueOrfalse == false){
 			
		   System.out.println ("if you want to use add, enter add");
		   System.out.println ("if you want to use subtraction, enter sub");
		   System.out.println ("if you want to use division, enter div"); 
		   System.out.println ("if you want to use multiplication, enter mul");
		   System.out.println ("if you want to exit, enter bye");
		   operation = in.nextLine();
		   
			   
			   if (operation.equalsIgnoreCase("bye")){
				   trueOrfalse = true ;
				   System.exit(0);
				   break;
			   }
			   

		   System.out.println ("Please enter First number");
		   number1 = in.nextDouble();
		   
		   System.out.println ("Please enter Second number");
		   number2 = in.nextDouble();
		   
		   if (operation.equalsIgnoreCase("add")){
			 store = (MiniCalculator.addTwoNumbers());
			   System.out.print( (int)number1 + " + " + (int)number2 + " = ");
			   System.out.println (store + "\n"); 
		   }
		   else if(operation.equalsIgnoreCase("sub")){
				 store = (MiniCalculator.subtractTwoNumbers());
				   System.out.print( number1 + " - " + number2 + " = ");
				   System.out.println (store + "\n"); 
			   }
		   else if(operation.equalsIgnoreCase("div")){
			   	
		   store1 = (MiniCalculator.divideTwoNumbers());
				   System.out.print( number1 + " ÷ " + number2);
				   System.out.println (store1 + "\n"); 
			   }
		   else if(operation.equalsIgnoreCase("mul")){
			   	
			   store = (MiniCalculator.multiplyTwoNumbers());
					   System.out.print( number1 + " × " + number2 + " = ");
					   System.out.println (store + "\n"); 
				   }
		
 		}
 		  
	}

...
...
...
	}

}

Please HELP me T_T !

Recommended Answers

All 15 Replies

This is just a guess, but I'm thinking your input String still has the carriage return or other whitespace, and so it's not equal to the comparison string ("bye\n"!="bye")

instead of this

if (operation.equalsIgnoreCase("bye")){

try

if (operation.trim().equalsIgnoreCase("bye")){

And see if that works.

I tried your code and the bye seems to work fine. what exactly is it not doing?

Also try debugging your code by adding print statements to show what is in the operation variable after it is read:
System.out.println("operation=" + operation + "<");

hmmmmm...Thank you...
but...
I'm Sorry but the problem is in line #19 from the OUTPUT,,
I don't want it to print that "Please enter first number" again.. understand me ^^"?


ok..

the full code is here, test it please:

import java.util.Scanner;

public class MiniCalculator {
	String display; // for displaying calculation result.
	static String operation, store1;
	static double memory;
	static double store;
	static double number1,number2;
	static boolean trueOrfalse;

	public static void main(String [] args) {
 		Scanner in = new Scanner (System.in);
 		System.out.println ("Welcome to our miniCalculator");
 		
 		while(trueOrfalse == false){
 			
		   System.out.println ("if you want to use add, enter add");
		   System.out.println ("if you want to use subtraction, enter sub");
		   System.out.println ("if you want to use division, enter div"); 
		   System.out.println ("if you want to use multiplication, enter mul");
		   System.out.println ("if you want to exit, enter bye");
		   operation = in.nextLine();
		   
			   
			   if (operation.equalsIgnoreCase("bye")){
				   trueOrfalse = true ;
				   System.exit(0);
				   break;
			   }
			   

		   System.out.println ("Please enter First number");
		   number1 = in.nextDouble();
		   
		   System.out.println ("Please enter Second number");
		   number2 = in.nextDouble();
		   
		   if (operation.equalsIgnoreCase("add")){
			 store = (MiniCalculator.addTwoNumbers());
			   System.out.print( (int)number1 + " + " + (int)number2 + " = ");
			   System.out.println (store + "\n"); 
		   }
		   else if(operation.equalsIgnoreCase("sub")){
				 store = (MiniCalculator.subtractTwoNumbers());
				   System.out.print( number1 + " - " + number2 + " = ");
				   System.out.println (store + "\n"); 
			   }
		   else if(operation.equalsIgnoreCase("div")){
			   	
		   store1 = (MiniCalculator.divideTwoNumbers());
				   System.out.print( number1 + " ÷ " + number2);
				   System.out.println (store1 + "\n"); 
			   }
		   else if(operation.equalsIgnoreCase("mul")){
			   	
			   store = (MiniCalculator.multiplyTwoNumbers());
					   System.out.print( number1 + " × " + number2 + " = ");
					   System.out.println (store + "\n"); 
				   }
			  
 		
 		}
 		
		  
	}



	public void setDisplay(String theDisplay) {
	 display = theDisplay;
	}
	public String getDisplay(){
	 return display;
	}
	public void setMemory(double theMemory) {
	 memory = theMemory;
	}
	public double getMemory(){
	 return memory;
	}
	public static double addTwoNumbers () {
	return memory=(number1+number2);
	}
	public static double subtractTwoNumbers () {
	return memory=(number1-number2);
	}
	public static double multiplyTwoNumbers () {
	return memory=(number1*number2);
	}
	public static double addToMemory(){
	return memory = memory + number1;
	}
	public static double square () {
	return memory=(number1*number1);
	}
	public double removeFromMemory(){
	return memory = memory - number1;
	}
	public static String divideTwoNumbers () {
	if (number2==0) {
	return "NA"; 
	}
	else{
	return "="+(number1/ number2); 
	}
	}

}

Did you try this?

try debugging your code by adding print statements to show what is in the operation variable after it is read:
System.out.println("operation=" + operation + "<");

Why isn't "bye" being recognized and the code exiting?

Yes, I did...
yeah..the code exiting correctly..
but the problem is .. the code doesn't loop correctly..

I'm sorry for annoying ><"

the program must to be like this:

----------------------------------------------
1. Display the following message for the user:
Welcome to our miniCalculator
if you want to use add, enter add
if you want to use subtraction, enter sub
if you want to use division, enter div
if you want to use multiplication, enter mul
if you want to exit, enter bye

2. Perform what the user request, and redisplay the menu again، except if the
user enter bye, this will end the program.
- If the user enter (add or sub or div or mul) the program will ask the
user to enter the first number, then the second number, and after that
will display the result and redisplay the menu again.
- If the user enter bye, this will end the program.
----------------------------------------------


Example:
______________________________

Welcome to our miniCalculator
if you want to use add, enter add
if you want to use subtraction, enter sub
if you want to use division, enter div
if you want to use multiplication, enter mul
if you want to exit, enter bye
add
please enter first number
2
please enter second number
4
2+4=6
if you want to use add, enter add
if you want to use subtraction, enter sub
if you want to use division, enter div
if you want to use multiplication, enter mul
if you want to exit, enter bye
bye
___________________________________

What is printed out if you add the following statement after the read into the operation variable?

System.out.println("operation=" + operation + "<");

What is printed out if you add the following statement after the read into the operation variable?

System.out.println("operation=" + operation + "<");

This:
______________________
Welcome to our miniCalculator
if you want to use add, enter add
if you want to use subtraction, enter sub
if you want to use division, enter div
if you want to use multiplication, enter mul
if you want to exit, enter bye
add
operation=add<
Please enter First number
...
...
______________________

hmmmm.. if I add the previous statement
before this: (operation = in.nextLine(); )
it will print: operation=null<

^^"

You didn't show all of the output from the print statement in your previous post.
What does it show for all of the inputs to the program. The ... and ... are useless information. Show ALL of the console.

Here's what for one time I ran the program:

D:\JavaDevelopment\Testing\ForumQuestions5>java MiniCalculator
Welcome to our miniCalculator
if you want to use add, enter add
if you want to use subtraction, enter sub
if you want to use division, enter div
if you want to use multiplication, enter mul
if you want to exit, enter bye
add
operation=add<
Please enter First number
3
Please enter Second number
4 bye
3 + 4 = 7.0

if you want to use add, enter add
if you want to use subtraction, enter sub
if you want to use division, enter div
if you want to use multiplication, enter mul
if you want to exit, enter bye
operation= bye<
Please enter First number

Notice the space before the bye.

There is a problem with how the Scanner class works. nextLine and nextDouble operated differently with respect to lineend.

Ahaaaaa !
.
.
now I had understood..^___^
Thank you so much..^_^

I wrote my code like this:

...
...
System.out.println ("Welcome to our miniCalculator");
 		
 		while(operation != "bye"){
 				
		   System.out.println ("if you want to use add, enter add");
		   System.out.println ("if you want to use subtraction, enter sub");
		   System.out.println ("if you want to use division, enter div"); 
		   System.out.println ("if you want to use multiplication, enter mul");
		   System.out.println ("if you want to exit, enter bye");
		   operation = in.next();
		   System.out.println("operation=" + operation + "<");  
		   
		   if (operation.equalsIgnoreCase("bye")){
			   trueOrfalse = true ;
			   System.exit(0);
			   break;
		   }
		   
		    System.out.println ("Please enter First number");
		   number1 = in.nextDouble();
...
...

And the OUTPUT was as I want..^__^ :

Welcome to our miniCalculator
if you want to use add, enter add
if you want to use subtraction, enter sub
if you want to use division, enter div
if you want to use multiplication, enter mul
if you want to exit, enter bye
add
operation=add<
Please enter First number
1
Please enter Second number
2
1 + 2 = 3.0

if you want to use add, enter add
if you want to use subtraction, enter sub
if you want to use division, enter div
if you want to use multiplication, enter mul
if you want to exit, enter bye
bye
operation=bye<

Thank you again for giving me a little of your time..
and forgive me if I annoying .. ^^"

Best Wishes for you ^__^

Glad you got it. Scanner can be tricky.

You can also replace the

System.out.println("operation=" + operation + "<");

with

while( operation.equals(""));

Then the program waits for input. But the only diffirence wit normR1's sollution is you won't get the "operation=add<"

And you also don't need the System.exit(0); in your code. The break gets you out of the while-loop and ends the program.

My code was for debugging only. It would be removed when the bug was found.
replacing it does NOT help in the debug process. The OP needs to see what is happening and a way to do that is to print out values as they are read.

And you also don't need the System.exit(0); in your code. The break gets you out of the while-loop and ends the program.

Aha..right .. ^^ !

My code was for debugging only. It would be removed when the bug was found.

yup yup !

Thanks all of you ^.^ !
Bye :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.