Results 1 to 9 of 9
- 04-25-2009, 06:17 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 2
- Rep Power
- 0
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
- 04-25-2009, 06:26 PM #2
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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 PassengerIf 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!
-
I have the feeling that you're trying to read something that just isn't there. This makes me nervous:
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.Java Code:while (fileScan.hasNext()) { dest = fileScan.nextLine(); num1 = fileScan.nextInt(); num2 = fileScan.nextInt(); num3 = fileScan.nextInt();
Also, you would do well to post your data file here so that we can see its structure.
- 04-26-2009, 01:09 AM #4
Member
- Join Date
- Apr 2009
- Posts
- 2
- Rep Power
- 0
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
-
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.
- 04-26-2009, 06:56 AM #6
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
dest get the Line data, ie, String int int int both store in destJava Code:dest = fileScan.nextLine();
other than Fubarable method, you may
Java Code:dest = fileScan.next(); num1 = fileScan.nextInt(); num2 = fileScan.nextInt(); num3 = fileScan.nextInt();
-
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.
- 04-26-2009, 07:09 AM #8
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
sorry
i miss this point...what if the string has a space in it
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
-
Similar Threads
-
Reading data from csv file based on specific input
By jaiminparikh in forum Advanced JavaReplies: 14Last Post: 02-13-2009, 09:07 PM -
Problem in reading HTML input field while uploading file
By sudipanand in forum Java ServletReplies: 1Last Post: 11-27-2008, 09:26 AM -
Reading input file into an array
By littlefire in forum New To JavaReplies: 6Last Post: 10-18-2008, 11:51 PM -
reading from a zip file, error
By Mr tuition in forum AWT / SwingReplies: 1Last Post: 01-16-2008, 12:39 AM -
Help with error message when running JAR via HTML file
By Simmy in forum AWT / SwingReplies: 7Last Post: 08-12-2007, 03:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks