Jump to content

Java what's wrong here

oliverjrose99

the loop runs even if operation is right, what am i doing wrong

import java.util.Scanner;public class Main {	public static void main(String[] args) {		//Create object for Scanner		Scanner input = new Scanner(System.in);				//Create variables		int answer, fnum, snum, num;		String operation;				//Operation loop		do {			 System.out.println("Operations:\n+\n-\n*\n/\nEnter the operation:");			 operation = input.nextLine();			 			 if (operation != "+" && operation != "-" && operation != "*" && operation != "/") {				 System.out.println("\nNot a valid operation, Try again...");			 }		} while (operation != "+" && operation != "-" && operation != "*" && operation != "/");				System.out.println("Done...");	}}
Link to comment
Share on other sites

Link to post
Share on other sites

A do-while loop will always execute once because the check is performed after the loop is executed. Do this... check  if we still should do it... do it again... etc.

 

Your probably wanting a while loop...

while Condition...do this

You can read more about it here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

There are 10 types of people in this world, those who can read binary and those who can't.

There are 10 types of people in this world, those who can read hexadecimal and F the rest.

~Fletch

Link to comment
Share on other sites

Link to post
Share on other sites

this wont work

while (operation != "+" && operation != "-" && operation != "*" && operation != "/") {	System.out.println("Operations:\n+\n-\n*\n/\nEnter the operation:");	operation = input.nextLine();	if (operation != "+" && operation != "-" && operation != "*" && operation != "/") {		System.out.println("Not a valid operation, Try again...");	}}
Link to comment
Share on other sites

Link to post
Share on other sites

Your first post was almost right....the big thing is != and == are insanely misleading in java.  != and == compares objects, so strings get messed up with != and ==.

 

For strings, and other objects don't use ==, instead use .equals

 

so your code

import java.util.Scanner;public class Main {	public static void main(String[] args) {		//Create object for Scanner		Scanner input = new Scanner(System.in);				//Create variables		int answer, fnum, snum, num;		String operation;				//Operation loop		do {			 System.out.println("Operations:\n+\n-\n*\n/\nEnter the operation:");			 operation = input.nextLine();			 			 if (!operation.equals("+") && !operation.equals("-") && !operation.equals("*") && !operation.equals("/")) {                         //As another note, when this condition happens you will get the text printing out, but you will also exit the while loop                         //so you will end up it printing not a valid operation and then printing done....but I will let you fix that				 System.out.println("\nNot a valid operation, Try again...");			 }		} while (!operation.equals("+") && !operation.equals("-") && !operation.equals("*") && !operation.equals("/"));				System.out.println("Done...");	}}

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

 

Your first post was almost right....the big thing is != and == are insanely misleading in java.  != and == compares objects, so strings get messed up with != and ==.

 

For strings, and other objects don't use ==, instead use .equals

 

so your code

import java.util.Scanner;public class Main {	public static void main(String[] args) {		//Create object for Scanner		Scanner input = new Scanner(System.in);				//Create variables		int answer, fnum, snum, num;		String operation;				//Operation loop		do {			 System.out.println("Operations:\n+\n-\n*\n/\nEnter the operation:");			 operation = input.nextLine();			 			 if (!operation.equals("+") && !operation.equals("-") && !operation.equals("*") && !operation.equals("/")) {                         //As another note, when this condition happens you will get the text printing out, but you will also exit the while loop                         //so you will end up it printing not a valid operation and then printing done....but I will let you fix that				 System.out.println("\nNot a valid operation, Try again...");			 }		} while (!operation.equals("+") && !operation.equals("-") && !operation.equals("*") && !operation.equals("/"));				System.out.println("Done...");	}}

thanks alot, im new to java

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×