954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

equalsIgnoreCase not working eventhough no compiling error. Help !

The Question is:

Write, compile ad test a program that stores two integers and allow the user to enter a character. If the character is A, add the two integers, if it is S subtract the second integer from the first one and if it is M multiply the two integers. Display the arithmetic.

Problem: Although there is no compiling error or syntax error the above code is not working. Why is it so?

This is the output am getting in the Console window:
X: 2
Y: 3
Z: Invalid choice!

import java.util.*;
public class Calculate {

	
	public static void main(String[] args) {
		int x,y;
		String z;
		
		Scanner sn =new Scanner(System.in);
		
		System.out.print("X: \t");
		x=sn.nextInt();
		System.out.print("Y: \t");
		y=sn.nextInt();
		System.out.print("Z: \t");
		z=sn.nextLine();
		
		
		if(z.equalsIgnoreCase("A")){
			System.out.println("Sum: \t"+(x+y));
		}
		else{
			if(z.equalsIgnoreCase("S")){
				System.out.println("Subtraction answer: \t"+(y-x));
			}
			else{
				if(z.equalsIgnoreCase("M")){
					System.out.println("Multiplication answer: \t"+(x*y));
				}
				else{
					System.out.println("Invalid choice!");
				}
			}
		}
		
		
		
		

	}

}
kreshan_489
Newbie Poster
7 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

It's happened because user input digit and press , y=sn.nextInt(); read the int, but not enter, so line z=sn.nextLine(); force read line "\n" of previous input. you can add one more sn.nextLine(); in line 15, it's can help.

koderz
Newbie Poster
5 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

I found the answer. Yet i want to understand why Java cannot scan text using nextline() after scanning for integer values. I used two variable to create 2 instances of Scanner each used to read text and integer.
Here is the working code:

import java.util.*;
public class Calculate {


public static void main(String[] args) {
int x,y;
String z;

Scanner str_sn =new Scanner(System.in);
Scanner int_sn= new Scanner(System.in);

System.out.print("X: \t");
x=int_sn.nextInt();
System.out.print("Y: \t");
y=int_sn.nextInt();
System.out.print("Z: \t");
z=str_sn.nextLine();

if(z.equals("A")){
System.out.println("Sum: \t"+(x+y));
}
else{
if(z.equals("S")){
System.out.println("Subtraction answer: \t"+(y-x));
}
else{
if(z.equals("M")){
System.out.println("Multiplication answer: \t"+(x*y));
}
else{
System.out.println("Invalid choice!");
}
}
}

}

}

kreshan_489
Newbie Poster
7 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

@Koderz: Thanks Koderz. sn is taking the nextLine() but i am getting invalid choice as message whatever choice i am inserting.


import java.util.*;
public class Calculate {


public static void main(String[] args) {
int x,y;
String z;

Scanner sn= new Scanner(System.in);

System.out.print("X: \t");
x=sn.nextInt();
System.out.print("Y: \t");
y=sn.nextInt();
System.out.print("Z: \t");
z=sn.nextLine();
sn.nextLine();

if(z.equals("A")){
System.out.println("Sum: \t"+(x+y));
}
else{
if(z.equals("S")){
System.out.println("Subtraction answer: \t"+(y-x));
}
else{
if(z.equals("M")){
System.out.println("Multiplication answer: \t"+(x*y));
}
else{
System.out.println("Invalid choice!");
}
}
}

}

}

Output:

X: 2
Y: 3
Z: A
Invalid choice!

kreshan_489
Newbie Poster
7 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

@koderz: Sorry i inserted sn.nextline() at line 17. The code is working perfectly fine. Thanks again.

kreshan_489
Newbie Poster
7 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

You type:
z=sn.nextLine();
sn.nextLine();

but need:
sn.nextLine();
z=sn.nextLine();

koderz
Newbie Poster
5 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

@koderz: Sorry i inserted sn.nextline() at line 17. The code is working perfectly fine. Thanks again.

kreshan_489
Newbie Poster
7 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You