Results 1 to 5 of 5
Thread: Help with Code
- 11-13-2012, 03:51 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 3
- Rep Power
- 0
Help with Code
Alright so the object of the program is to input data from a text file. However, I'm having issues when parsing the text file. BlueJ returns a java.lang.NumberFormatException: for Input String: "945" at the pressure[number] line. Are there any other ways to work this program?
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.io.PrintWriter;
public class Hurricanes2
{
public static void main(String[] args) throws IOException
{
File hurricaneData = new File("hurcdata.txt");
Scanner inFile = new Scanner(hurricaneData);
PrintWriter outFile = new PrintWriter (new File("Summary.txt"));
int index = 0;
while(inFile.hasNextLine())
{
String blank = inFile.nextLine();
index++;
}
int[ ] years = new int[ index ];
String[ ] month = new String [ index ];
int[ ] pressure = new int [ index ];
int [ ] windSpeed = new int [ index ];
String[ ] name = new String [ index ];
int number = 0;
File hurricaneData2 = new File("hurcdata.txt");
Scanner inFile2 = new Scanner(hurricaneData2);
while(inFile2.hasNextLine())
{
String data = inFile2.nextLine();
String year = data.substring(0 , 4);
years[number] = Integer.parseInt(year);
String monthOfHurricane = data.substring(5 , 8);
month[number] = monthOfHurricane;
int y = data.indexOf("", 9);
int x = data.indexOf("", 13);
String pressureOfHurricane = data.substring(y, x );
pressure[number] = Integer.parseInt(pressureOfHurricane);
int z = data.indexOf("", 15);
String windSpeedofHurricane = data.substring(x, (z+1));
windSpeed[number] = Integer.parseInt(windSpeedofHurricane);
String nameOfHurricane = data.substring((z+1));
name[number] = nameOfHurricane;
number++;
}
System.out.println(years[3]);
System.out.println(month[3]);
System.out.println(pressure[3]);
System.out.println(windSpeed[3]);
System.out.println(name[3]);
inFile.close();
}
}
First lines of text file:
1980 Aug 945 100 Allen
1983 Aug 962 100 Alicia
1984 Sep 949 100 Diana
1985 Jul 1002 65 Bob
- 11-13-2012, 04:00 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Re: Help with Code
Please use [code] tags [/code] when posting code.
Check how your splitting of the line is working by printing out each element as you substring it, complete with '-' before and after.
eg
I expect there's a space character in there causing the exception.Java Code:System.out.println("-" + y + "-");Please do not ask for code as refusal often offends.
- 11-13-2012, 11:28 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 3
- Rep Power
- 0
Re: Help with Code
That seems to be the issue. There's space characters in the string. However, why is this occurring?
Because I used indexOf to find the spaces shouldn't the program have found the first space and skipped over it? How can I remedy my situation?
- 11-14-2012, 09:56 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Re: Help with Code
All I can say is that the index is wrong somewhere.
Can I suggest simply using the split() method?
That's the usual way of doing this.Please do not ask for code as refusal often offends.
- 11-14-2012, 05:18 PM #5
Re: Help with Code
Please go through the Forum Rules -- particularly the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
I want the source code of DUK's Bank , the example code in Java EE 5 Tutorial 2010
By zahra in forum New To JavaReplies: 16Last Post: 01-31-2012, 08:36 PM -
My code was not executed properly.It will jumping to exception handling.my code is
By vinay4051 in forum EclipseReplies: 3Last Post: 08-10-2011, 09:17 AM -
servlet include method copying sorce code and executing source code as output how to
By shamkuma2k in forum Advanced JavaReplies: 0Last Post: 08-07-2011, 08:32 PM -
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks