Results 1 to 2 of 2
Thread: Input problem
- 06-04-2010, 12:42 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
Input problem
Hello again, so after some help I got from here and fixing a lot of errors I finally got my program running, but.. it seems there is a problem with input.
So, the code is this:
When I get to this line "y=Integer.parseInt(br.readLine());" I get a NumberFormatException. For input string "". What exactly this means. I didn't input anything at all. I suspect it has to do with earlier inputs but I, honestly, don't understand.Java Code:import java.io.*; class Vehicle{ private String number; private char status; private int years; Vehicle(String n, char s, int y){ number=n; status=s; years=y; } public static int [] CalculateFee(Vehicle table[]){ int Fee[]=new int[table.length]; for(int i=0; i<table.length; i++){ if(table[i].status=='1') Fee[i]=200; else if(table[i].status=='2') Fee[i]=2000; else Fee[i]=1000; if(table[i].years<=5) Fee[i]+=30; else if(table[i].years<=10) Fee[i]+=60; else if(table[i].years<=15) Fee[i]+=90; else Fee[i]+=120; } return Fee; } public String GetNumber(){ return number; } } class VehicleDemo1{ public static void main(String args[]) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String n; char s; int y; System.out.println("How many vehicles you want to give for calculation: "); int many=Integer.parseInt(br.readLine()); System.out.println(); Vehicle table[]=new Vehicle[many]; for(int i=0; i<table.length; i++){ System.out.println(i+" vehicle"); System.out.println("Type the number of the vehicle: "); n=br.readLine(); System.out.println("Type the status of the vehicle (1=legal, 2=illegal, 3=under investigation): "); s=(char) System.in.read(); System.out.println("Type the years the vehicle is being used: "); y=Integer.parseInt(br.readLine()); table[i]=new Vehicle(n, s, y); } int Fee[]=new int[table.length]; Fee=Vehicle.CalculateFee(table); for(int i=0; i<table.length; i++){ System.out.println("The vehicle with the number "+table[i].GetNumber()+" has a fee of "+Fee[i]+" euro"); System.out.println(); } } }
In fact, I have many problems with inputs in lots of programs where I ask for input and it says the question 3 times before it lets me type.
I believe that java inputs something by itself before letting me type.
Any idea ? Thanks :)
- 06-04-2010, 01:07 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Suppose you want to read an int followed by any String value, so you do:
And you duly type "123<enter>foo<enter>"; where <enter> is the character for your enter key. The nextInt() reads the integer 123 fine but leaves the following characters in the input buffer: "<enter>foo<enter>" because they cannot be part of an integer. When your scanner wants to read an entire line it 'sees' the first <enter> character and assumes an empty line so it returns "".Java Code:int i= scanner.nextInt(); String s= scanner.readLine();
So after reading an int as the last token on a line always put a dummy scanner.readLine() call in there to get rid of that pending <enter> character.
kind regards,
Jos
Similar Threads
-
Problem in Hardware Input
By kirly in forum New To JavaReplies: 4Last Post: 01-22-2010, 08:37 AM -
problem with Scanner in Getting users input
By kliane in forum New To JavaReplies: 8Last Post: 01-17-2010, 04:37 PM -
problem with changes of Dynamic input width box
By synclenovo in forum New To JavaReplies: 1Last Post: 04-09-2009, 12:15 AM -
Problem - input in two-dimensional array
By PVL268 in forum New To JavaReplies: 5Last Post: 03-09-2009, 04:58 AM -
Scanner input problem
By slayer_azure in forum New To JavaReplies: 3Last Post: 05-26-2008, 10:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks