What to change in my code

char ch=s.next().charAt(0);
int n1=s.nextInt();
int n2=s.nextInt();
if(ch==‘X’||ch==‘x’) {
return;
}
if(ch==’+’)
{
System.out.println(n1+n2);
}
else if(ch==’-’) {
System.out.println(n1-n2);
}
else if(ch==’’) {
System.out.println(n1
n2);
}
else if(ch==’%’) {
System.out.println(n1%n2);

	}
	else if(ch=='/') {
		System.out.println(n1/n2);
		
	}
	else
	{
		System.out.println("Invalid operation.Try again");
	}
}}

Hii Ritu,
Put this code in a loop and loop should run till ch != ‘X’ && ch != ‘x’.

	Scanner s = new Scanner(System.in);  		char ch=s.next().charAt(0);  		int n1=s.nextInt(); 		int n2=s.nextInt(); while(ch!='X'&&ch!='x') { 		if(ch=='+') { 			System.out.println(n1+n2); 			}  		else if(ch=='-')  		{ 			System.out.println(n1-n2); 			}  		else if(ch=='*') { 			System.out.println(n1*n2);  			} 		else if(ch=='%') { 			System.out.println(n1%n2); 			} 		else if(ch=='/') { 			System.out.println(n1/n2); 			}  		else 		{  			System.out.println("Invalid operation.Try again"); 			} 		} 	} }

Hi Ritu,
You will take n1 and n2 inside if and else if condition because if the user has entered an invalid character then user will not give you n1 and n2. Also you have to take ch every time the loop runs.

how to take ch everytime the loop runs my code is giving run time error Scanner s = new Scanner(System.in); char ch=s.next().charAt(0); while(ch!=‘X’&&ch!=‘x’) { int n1=s.nextInt(); int n2=s.nextInt(); if(ch==’+’) { System.out.println(n1+n2); } else if(ch==’-’) { System.out.println(n1-n2); } else if(ch==’’) { System.out.println(n1n2); } else if(ch==’%’) { System.out.println(n1%n2); } else if(ch==’/’) { System.out.println(n1/n2); } else { System.out.println(“Invalid operation.Try again”); } } } }

Hii Ritu,
You can take ch every time the loop runs in this way:-
do{
char ch=s.next().charAt(0);
//work
}while(ch!=‘X’&&ch!=‘x’);

Your code shows run time error as you take the value of ch from user only once outside the loop so the loop runs infinitely.

1 Like

Scanner s = new Scanner(System.in); do { char ch=s.next().charAt(0); int n1=s.nextInt(); int n2=s.nextInt(); if(ch==’+’) { System.out.println(n1+n2); } else if(ch==’-’) { System.out.println(n1-n2); } else if(ch==’’) { System.out.println(n1n2); } else if(ch==’%’) { System.out.println(n1%n2); } else if(ch==’/’) { System.out.println(n1/n2); } else { System.out.println(“Invalid operation.Try again”); } }while(ch!=‘X’&&ch!=‘x’); } }

CH CANNOT BE RESOLVED TO A VARIABLE ISSUE

Hii RItu,
declare ch outside the do while loop.

Scanner s = new Scanner(System.in);  		char ch=s.next().charAt(0);  		 		do { 	 	  	int n1=s.nextInt(); 	int n2=s.nextInt(); 		if(ch=='+') { 			System.out.println(n1+n2); 			}  		else if(ch=='-')  		{ 			System.out.println(n1-n2); 			}  		else if(ch=='*') { 			System.out.println(n1*n2);  			} 		else if(ch=='%') { 			System.out.println(n1%n2); 			} 		else if(ch=='/') { 			System.out.println(n1/n2); 			}  		else 		{  			System.out.println("Invalid operation.Try again"); 		} 			 }while(ch!='X'&&ch!='x'); 		 		} 	} Run error

Hi Ritu

Please send the link to your code. It will be easier for us to debug it and tell your mistake.

import java.util.; public class Main { public static void main(String args[]) { Scanner s=new Scanner(System.in); char ch=s.next().charAt(0); do { int n1=s.nextInt(); int n2=s.nextInt(); if(ch==’+’) { System.out.println(n1+n2); } else if(ch==’-’) { System.out.println(n1-n2); } else if(ch==’’) { System.out.println(n1*n2); } else if(ch==’%’) { System.out.println(n1%n2); } else if(ch==’/’) { System.out.println(n1/n2); } else { System.out.println(“Invalid operation.Try again”); } }while(ch!=‘X’&&ch!=‘x’); } } this is my code giving run time error

i am sorry,but cant find the save button to copy he url

my code is working perfectly fine for one time char ,not taking mulltiple characters what to do

Hi Ritu

I have made the changes to your code. It should work now.

import java.util.*;

public class Main {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
char ch;
do {
ch = s.next().charAt(0);

		if (ch == '+') {
			int n1 = s.nextInt();
			int n2 = s.nextInt();
			System.out.println(n1 + n2);
			
		} else if (ch == '-') {
			int n1 = s.nextInt();
			int n2 = s.nextInt();
			System.out.println(n1 - n2);
		} else if (ch == '*') {
			int n1 = s.nextInt();
			int n2 = s.nextInt();
			System.out.println(n1 * n2);
		} else if (ch == '%') {
			int n1 = s.nextInt();
			int n2 = s.nextInt();
			System.out.println(n1 % n2);
		} else if (ch == '/') {
			int n1 = s.nextInt();
			int n2 = s.nextInt();
			System.out.println(n1 / n2);
		} else {
			if (ch != 'x' && ch != 'X') {
				System.out.println("Invalid operation. Try again.");
			}
		}
	} while (ch != 'X' && ch != 'x');
}

}

Your code was giving run error because you were taking input n1 and n2 when characters like ; ? are entered. You have to take input for n1 and n2 only when the characters like + - * / % are entered.

import java.util.; public class Main { public static void main(String args[]) { Scanner s=new Scanner(System.in); char ch; do{ ch=s.next().charAt(0); if(ch==’+’){ int n1=s.nextInt(); int n2=s.nextInt(); System.out.println(n1+n2); } else if(ch==’-’){ int n1=s.nextInt(); int n2=s.nextInt(); System.out.println(n1-n2); } else if(ch==’’){ int n1=s.nextInt(); int n2=s.nextInt(); System.out.println(n1*n2); } else if(ch==’%’){ int n1=s.nextInt(); int n2=s.nextInt(); System.out.println(n1%n2); } else if(ch==’/’){ int n1=s.nextInt(); int n2=s.nextInt(); System.out.println(n1/n2); } else{ if(ch!=‘X’&&ch!=‘x’){ System.out.println(“Invlid operation.Try again.”); } } }while(ch!=‘X’&&ch!=‘x’); } }

ma’am, this is the code you corrected working fine in eclipse .only 2 test cases passed .what is wrong

Hi Ritu

The previous code I sent is working fine and passing all the test cases too. Please read the code again and try to understand the logic.