473,386 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

string scanning

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.

2 1784
Nepomuk
3,112 Expert 2GB
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
Norgy
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

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

Similar topics

0
by: Ajay | last post by:
hi! I am developing an application for a pocket pc. i have a small gui which allows users to select files using a file dialog box. however the file, selected is represented as '/My...
4
by: francescomoi | last post by:
Hi. I'm trying to remove some characters within a string and substitute others. For instance, I want to convert: John's new house, great ---> Johns-new-house-great I tried with:...
2
by: excite | last post by:
I am trying to write a long string into a txt file, the string includes some backslash, looks like: 'd:\#########\#######\####### d:\######\####\####\### d:\###\###\' then I got a error saying...
7
by: bcpkh | last post by:
Hello All I need to check a string to make sure it does not contain any non numeric characters, the problem that I face is that the string is fairly long, 2784601121574585949, strtol etc. can't...
2
by: jgbid | last post by:
Hi, I'm trying to build an IP Scanner inc c# for a specific port (80) and for specific IP Ranges. For example 24.36.148.1 to 24.36.148.255 My first step was to use TcpClient, but there are...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
14
by: nishit.gupta | last post by:
Is their any single fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" , It can also...
1
kirubagari
by: kirubagari | last post by:
For i = 49 To mfilesize Step 6 rich1.SelStart = Len(rich1.Text) rich1.SelText = "Before : " & HexByte2Char(arrByte(i)) & _ " " & HexByte2Char(arrByte(i + 1)) & " " _ &...
23
by: Rotsey | last post by:
Hi, I am writing an app that scans hard drives and logs info about every fine on the drive. The first iteration of my code used a class and a generic list to store the data and rhis took...
6
by: shapper | last post by:
Hello, I have a string and I need to get as many words possible counting from the beginning but without exceeding 120 characters. I can't break words and I the string shouldn't end with a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.