Results 1 to 9 of 9
  1. #1
    Deluyxe is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default Error Message when reading an input file.

    Hello all, I keep getting an error when I am trying to read in a word from an input file. This is the code, the error and the input file. I know some of the things in the worker class aren't all updated with what I need to do my calculations, but I am just looking to get it to print the string. Thanks

    package GroupProject;

    import java.io.*;
    import java.util.Scanner;
    import java.text.NumberFormat;

    public class GroupProject
    {


    public static void main(String[] args) throws IOException

    {

    // Declare variables
    int num1, num2, num3;
    double fee;
    double totalFee=0;
    String dest;


    // Build the scanner object
    Scanner fileScan = new Scanner(new File("manifest.inp"));

    // Print a message to the user explaining what the program does
    System.out.println("This program calculates fees for leaving a car or truck in a parking garage.");
    NumberFormat fmt = NumberFormat.getCurrencyInstance();
    // Read in data from file
    while (fileScan.hasNext())
    {
    dest = fileScan.nextLine();
    num1 = fileScan.nextInt();
    num2 = fileScan.nextInt();
    num3 = fileScan.nextInt();


    passenger Calculator = new passenger(dest, num1, num2, num3);

    fee=Calculator.calcFees();

    System.out.println(Calculator);
    totalFee+=fee;
    }
    System.out.println("The day's total fees are:" +fmt.format(totalFee));

    }

    }

    package GroupProject;

    import java.text.NumberFormat;

    public class passenger
    {

    // instance data
    private int luggage, time, base;
    private double sum = 0;
    String going;


    // constructor
    public passenger(String dest, int num1, int num2, int num3)
    {
    luggage = num1;
    time = num2;
    base = num3;
    dest = going;
    }

    public double calcFees()
    {
    switch(luggage)
    {
    case 1:
    sum += 15;

    break;
    case 2:
    sum += 30;

    break;
    }

    switch(time)
    {
    case 1:
    sum += 0;

    break;
    case 2:
    sum += 50;

    break;
    case 3:
    sum += -20;

    break;
    }

    switch(base)
    {
    case 1:
    sum += 10;

    break;
    case 2:
    sum += 25;

    break;
    case 3:
    sum += 50;

    break;
    }
    return sum;
    }

    // toString method
    public String toString()
    {
    NumberFormat fmt = NumberFormat.getCurrencyInstance();
    String info = "The cost for this vehicle is: " +fmt.format(sum)+ " and they are headed to " +going;

    return info;
    }




    }

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at GroupProject.GroupProject.main(GroupProject.java:3 2)


    LosAngelas
    2 2 2

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Not sure if this would cause your error, but within the constructor for the passenger, you use dest = going; when it should be going = dest. It would definitely mess up the String being returned by your calcFees method.

    BTW, class names conventionally begin with a capital letter. You may want to change passenger to Passenger
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I have the feeling that you're trying to read something that just isn't there. This makes me nervous:
    Java Code:
    while (fileScan.hasNext())
    {
      dest = fileScan.nextLine();
      num1 = fileScan.nextInt();
      num2 = fileScan.nextInt();
      num3 = fileScan.nextInt();
    as you're checking that the file hasNext once but then read from the file 4 times. Thus while the first read is checked, the other 3 are unsafe.

    Also, you would do well to post your data file here so that we can see its structure.

  4. #4
    Deluyxe is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default

    Thanks for the replies, I got it working to read in the string. The only issue I have now, is for the input file, I want it too look something like:

    String Int Int Int
    String Int Int Int
    etc.

    What is the method for just reading the String, and what if the string has a space in it? Thanks

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If the String has a space in it, and you're using a space as your delimiter, you risk crashing the program. Better would be to avoid having spaces in the String if possible. Another solution would be to use a character of some sort to act as place-holders for your String's space, then replace them with spaces when you extract your Strings. Another solution would be to use something other than space for your delimiter. I've used something simple like %% in the past, and it worked well. And still another way to solve this is to use a Comma-Separated Values (CSV) file., but then you still need to create or borrow a CSV parser.
    Last edited by Fubarable; 04-26-2009 at 02:02 AM.

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    Java Code:
    dest = fileScan.nextLine();
    dest get the Line data, ie, String int int int both store in dest

    other than Fubarable method, you may
    Java Code:
     dest = fileScan.next();
      num1 = fileScan.nextInt();
      num2 = fileScan.nextInt();
      num3 = fileScan.nextInt();

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'm guessing that he was asking, what would happen if the data were like so:

    Chicago 42 29 120
    Detroit 23 19 293
    Michigan City 20 11 118

    The space in Michigan City might throw your code off as it is read as a delimiter in the standard Scanner.

  8. #8
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    sorry
    what if the string has a space in it
    i miss this point...

    then... i suggest to use Scanner.nextLine() to read the whole line
    and use String.split(" ")
    and then... the last 3 elemets in the String array should be int, others are substrings of a Word

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Simple but effective. I like it. And it works because you know that the line of data has 3 and only 3 ints at the end. rep to mtyoung, thanks.

Similar Threads

  1. Reading data from csv file based on specific input
    By jaiminparikh in forum Advanced Java
    Replies: 14
    Last Post: 02-13-2009, 09:07 PM
  2. Problem in reading HTML input field while uploading file
    By sudipanand in forum Java Servlet
    Replies: 1
    Last Post: 11-27-2008, 09:26 AM
  3. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-18-2008, 11:51 PM
  4. reading from a zip file, error
    By Mr tuition in forum AWT / Swing
    Replies: 1
    Last Post: 01-16-2008, 12:39 AM
  5. Replies: 7
    Last Post: 08-12-2007, 03:47 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
  •