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!");
				}
			}
		}
		
		
		
		

	}

}

It's happened because user input digit and press <Enter>, 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.

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!");
                }
            }
        }

    }

}

@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!

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

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

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

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

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.