By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,517 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

string scanning

P: 56
Expand|Select|Wrap|Line Numbers
  1.  public static void main(String[] args) {
  2.         // TODO code application logic here
  3.         Scanner input = new Scanner(System.in);
  4.         int noOfCases = input.nextInt();
  5.         System.out.println("no of c is :" + noOfCases);
  6.         for (int i = 0; i < noOfCases; i++) {
  7.             Scanner in = new Scanner(System.in);
  8.             credit = in.nextInt();
  9.             System.out.println("no of cr is :" + credit);
  10.             noOfItems = in.nextInt();
  11.             System.out.println("no of it is :" + noOfItems);
  12.             prices = in.nextLine();
  13.             System.out.println("no of pr is :" + prices);
  14.            // pri(prices);
  15.  
  16.         }
  17.  
  18.     }
when running this code, the program do not give me the chance to input the prices, what is the problem?
Jun 13 '13 #1

✓ answered by Nepomuk

First of all, it is difficult to know what you are trying to do. The variables credit, noOfItems and prices are never defined. But as Scanner#nextInt() will always give us an integer and Scanner#nextLine() will return a String (both according to the Java API docs) I'm guessing you have a definition similar to this one:
Expand|Select|Wrap|Line Numbers
  1. int credit, noOfItems;
  2. String prices;
OK, next step. When I add these and run the program, it throws an InputMismatchException when I would expect to input the prices and enter anything but an integer. But wait, it already output the line
Expand|Select|Wrap|Line Numbers
  1. no of pr is :
Why so? Well, nextLine() will scan for the rest of the line, remove any line separator and return the result. This means, that it won't wait for you to input something but will rather just return an empty String. Change the last printout to
Expand|Select|Wrap|Line Numbers
  1. System.out.println("no of pr is : '" + prices + "'");
and you'll see an empty result. It will then continue with the second reading of the credit

So, what's the solution? Well, depending on what exactly you're expecting for the prices variable, use a different method from the Scanner class.

PS.: You don't need two Scanners. Just use input rather than in inside the loop.

Share this Question
Share on Google+
2 Replies


Nepomuk
Expert 2.5K+
P: 3,112
First of all, it is difficult to know what you are trying to do. The variables credit, noOfItems and prices are never defined. But as Scanner#nextInt() will always give us an integer and Scanner#nextLine() will return a String (both according to the Java API docs) I'm guessing you have a definition similar to this one:
Expand|Select|Wrap|Line Numbers
  1. int credit, noOfItems;
  2. String prices;
OK, next step. When I add these and run the program, it throws an InputMismatchException when I would expect to input the prices and enter anything but an integer. But wait, it already output the line
Expand|Select|Wrap|Line Numbers
  1. no of pr is :
Why so? Well, nextLine() will scan for the rest of the line, remove any line separator and return the result. This means, that it won't wait for you to input something but will rather just return an empty String. Change the last printout to
Expand|Select|Wrap|Line Numbers
  1. System.out.println("no of pr is : '" + prices + "'");
and you'll see an empty result. It will then continue with the second reading of the credit

So, what's the solution? Well, depending on what exactly you're expecting for the prices variable, use a different method from the Scanner class.

PS.: You don't need two Scanners. Just use input rather than in inside the loop.
Jun 13 '13 #2

P: 56
Actually, i declared those variables outside the main, but i did not put the declaration here, sorry for that :)
and thank you
Jun 14 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.