Trouble with using Scanner methods nextInt() and nextLine() Together

I am using Scanner methods nextInt() and nextLine() for reading input.

I want my program to work for two inputs

Input 1:

10000 A

Output 1:

17600

 Input 2:
 
 10000
 
 A

Output 2:

17600

This Code is working for Input 1 only and giving Error in Input 2  After I Enter 10000. It is not taking A in next Line And executing program before taking A in Input 2. I want this program to work on both type of inputs.
how can i do that?

    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner s = new Scanner(System.in);
    		int sal = s.nextInt();
    		
    
    
    		double hra = sal*20.0/100;
    		double da = sal*50.0/100;
    		double pf = sal*11.0/100;
    		String str = s.nextLine();
    		char c = str.charAt(0);
    		int allow = 0;
    		if(c=='A') {
    			 allow = 1700;
    		}else if(c=='B') {
    			allow = 1500;
    		}else {allow = 1300;}
             double t = Math.round(sal + hra + da + allow - pf);
    		 int ts = (int)t;
    		 System.out.println(ts);
    
    	}
    
    }

Open in new window

Sarbjyot SinghAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krakatoaCommented:
Code in code tags please.

nextInt reads an int. nextLine reads a line. Maybe you can tell us what it is you are looking for rather than us having to read code that is not in the (far more) digestible code tags presentation format ?

And btw, you ought not go around giving method names to classes as you have done here : "Main". It's not at all protocol.
CEHJCommented:
 int sal = s.nextInt();

Open in new window

Why would you read an int before prompting for anything?
Currently your program will only operate without error when an int followed by a string is entered on the same line followed by Enter. How would the user know what to enter?
mccarlIT Business Systems Analyst / Software DeveloperCommented:
@krakatoa,

Firstly, I know that it is a fairly new EE feature so you may not know about it, but if you look just above the question title, there is an indicator to tell you that this is the question askers first question and so they may not know about all the features of the site. To a new user, "Code in code tags please" might not be particularly helpful. As for your comment, "Maybe you can tell us what it is you are looking for" well the question asker has told us what they are looking for.


@Sarbjyot,

Basically, all you need to do it to put the s.nextLine() call within a loop so that it keeps looking for input until the user provides it. (You probably should follows CEHJ advice to prompt the user for what they need to enter and in what format, i.e. you can say that they can enter all on one line or over multiple lines, etc.

And to krakatoa's comment, if you don't already know, at the top of the text area where you type in your question (or comments here) there is a button labelled "CODE" where you can highlight some of the text and press this button to format the selected text as code (note I have edited your question to include this code formatting)

Here is an example of putting the nextLIne() call in a do...while loop.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int sal = s.nextInt();

        double hra = sal * 20.0 / 100;
        double da = sal * 50.0 / 100;
        double pf = sal * 11.0 / 100;

        String str;
        do {
            str = s.nextLine().trim();
        } while (str.length() == 0);

        char c = str.charAt(0);
        int allow = 0;
        if (c == 'A') {
            allow = 1700;
        } else if (c == 'B') {
            allow = 1500;
        } else {
            allow = 1300;
        }
        double t = Math.round(sal + hra + da + allow - pf);
        int ts = (int) t;
        System.out.println(ts);

    }

}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaCommented:
Consider me named, shamed and totally rehabilitated in the paths of righteousness, for I have sinned. I know never to say “please” again, O mccarl.
CEHJCommented:
Here is an example of putting the nextLIne() call in a do...while loop.
Yes, but that again makes no sense - there's no user input prompt
Also, nextLine is being used in that example in quite a different way (in a loop). So quite confusing without comment on that
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.