Results 1 to 2 of 2

Thread: Input problem

  1. #1
    Sotsiak is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default 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:
    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();
      }
     }
    }
    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.

    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 :)

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Sotsiak View Post
    I believe that java inputs something by itself before letting me type.
    Any idea ? Thanks :)
    Suppose you want to read an int followed by any String value, so you do:
    Java Code:
    int i= scanner.nextInt();
    String s= scanner.readLine();
    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 "".

    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

  1. Problem in Hardware Input
    By kirly in forum New To Java
    Replies: 4
    Last Post: 01-22-2010, 08:37 AM
  2. problem with Scanner in Getting users input
    By kliane in forum New To Java
    Replies: 8
    Last Post: 01-17-2010, 04:37 PM
  3. problem with changes of Dynamic input width box
    By synclenovo in forum New To Java
    Replies: 1
    Last Post: 04-09-2009, 12:15 AM
  4. Problem - input in two-dimensional array
    By PVL268 in forum New To Java
    Replies: 5
    Last Post: 03-09-2009, 04:58 AM
  5. Scanner input problem
    By slayer_azure in forum New To Java
    Replies: 3
    Last Post: 05-26-2008, 10:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •