Results 1 to 16 of 16
- 05-09-2011, 11:56 PM #1
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
Line Reader changeing to read 1 line
Hi Everyone, I'm new to java and working on reading a text file into my java program. The issue with the code I have built is it only reads the text on each line and errors if there is more that one text on the line. Looking for some advice.
Here is the txt reader,
public boolean readInterestRates() { //start
boolean status = false;
try { //start
FileReader fis = new FileReader(new File("loanterms.txt"));
BufferedReader br = new BufferedReader(fis);
String line = null;
int i = 0;
while ((line = br.readLine()) != null) {
rates[i] = Double.parseDouble(line);
i++;
}
fis.close();
status = true;
} catch (Exception e) { //start
JOptionPane.showMessageDialog(this, "File Read Error!!!");
status = false;
} //end
Is it something there or how i'm trying to have a combobox display?
readInterestRates();
loanOptionBox.addItem("Select");
loanOptionBox.addItem("" + rates[0] + "%");
loanOptionBox.addItem("" + rates[1] + "%");
loanOptionBox.addItem(" " + rates[2] + "%");
loanOptionBox.addItem(" " + rates[3] + "%");
loanOptionBox.addItem("" + rates[4] + "%");
loanOptionBox.addActionListener(this);
Thanks all.
- 05-10-2011, 12:04 AM #2
Well, you are trying to make a double out of the entire line you read. If there's more data (or non-numerical data), a NumberFormatException (I think) is thrown.
- 05-10-2011, 12:09 AM #3
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
So the double on the rates[i] would cause the file read error if say for example its 1 2 3 4 in the text file but be ok if its input as?
1
2
3
4
- 05-10-2011, 12:15 AM #4
Right. It can't make "1 2 3 4" into a single number, thus it fails. It can be solved in various ways, but it might be easier to just put them on different lines if you can.
- 05-10-2011, 12:17 AM #5
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
I can't put them on different lines, but am researching for some of the other ways right now, what are some of the various ways to look at? I'm not trying to make it a single number but use the combobox to select what number to use. so i could use the combobox to pick the 3rd number out of 1 2 3 4 5.
Last edited by Javanooby; 05-10-2011 at 12:20 AM.
- 05-10-2011, 12:25 AM #6
I have to go, but I'll give you String (Java Platform SE 6) to read through. There are a few things in there that can help.
- 05-10-2011, 12:40 AM #7
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
Thanks for your help, I'm looking it over now, won't let me change it from a double so trying to figure out ways to get around, hopefully someone else will jump in here.
- 05-10-2011, 01:24 AM #8
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
Anyone else have any advice here am at the end of my rope, tried everything I could think of and search for just missing something.
Still won't read 1 line of 1 2 3 4 5 6 7 without the exception.Last edited by Javanooby; 05-10-2011 at 02:05 AM.
-
Check the link that Toll gave you. One method in there that might help is the split(...) method. You would call it on the String and pass in a space " " as the parameter.
- 05-10-2011, 03:03 AM #10
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
Thanks, I've been trying to figure out how to input the split for awhile and its just not clicking for me.
- 05-10-2011, 03:32 AM #11
You read the entire line. Call split on that line and it returns an array of Strings. Loop over that array and parse to a double.
- 05-10-2011, 03:50 AM #12
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
Thanks Junky, I kind of follow what your saying but am just brain dead on this, can you give a example?
-
Come on -- At least try to do it first yourself based on what he said. Then post your attempt here. That's how you learn.
- 05-10-2011, 04:13 AM #14
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
I have been :) this is what i got so far for split.
Java Code:public boolean readInterestRates() { boolean status = false; try { FileReader fis = new FileReader(new File("loanterms.txt")); BufferedReader br = new BufferedReader(fis); String[] line = br.readLine( ).split(" "); for ( int i = 0; i < line.length; i++ ) { rates[i] = Double.parseDouble(line); i++; } fis.close(); status = true; } catch (Exception e) { //start JOptionPane.showMessageDialog(this, "File Read Error!!!"); status = false; } //end return status; } //endLast edited by Fubarable; 05-10-2011 at 04:16 AM. Reason: code tags added
- 05-10-2011, 04:16 AM #15
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
Wait maybe i got it.
public boolean readInterestRates() {
boolean status = false;
try {
FileReader fis = new FileReader(new File("loanterms.txt"));
BufferedReader br = new BufferedReader(fis);
String[] line = br.readLine( ).split(" ");
for ( int i = 0; i < line.length; i++ ) {
rates[i] = Double.parseDouble(line[i].trim());
i++;
}
fis.close();
status = true;
} catch (Exception e) { //start
JOptionPane.showMessageDialog(this, "File Read Error!!!");
status = false;
} //end
return status;
} //end
Now no file errors when on 1 line. But having issues now with the option box, matching up the .txt too the right numbers.Last edited by Javanooby; 05-10-2011 at 04:24 AM.
- 05-10-2011, 04:34 AM #16
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
[SOLVED] read last line
By Azndaddy in forum New To JavaReplies: 22Last Post: 04-22-2012, 09:22 PM -
Read from command line
By sehudson in forum New To JavaReplies: 12Last Post: 02-23-2011, 04:44 AM -
How to read file line by line with fixed number of characters
By trkece in forum New To JavaReplies: 1Last Post: 02-13-2011, 03:09 PM -
Formatting java command line output - Multi line string
By dricco in forum New To JavaReplies: 2Last Post: 07-02-2010, 02:20 PM -
Need to read an .ini and .abook file line by line (both files contain texts)
By ollyworks in forum Java AppletsReplies: 4Last Post: 09-10-2009, 10:18 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks