Results 1 to 16 of 16
  1. #1
    Javanooby is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default 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.

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    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.

  3. #3
    Javanooby is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    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

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    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.

  5. #5
    Javanooby is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    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.

  6. #6
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    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.

  7. #7
    Javanooby is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    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.

  8. #8
    Javanooby is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    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.

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

    Default

    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.

  10. #10
    Javanooby is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    Thanks, I've been trying to figure out how to input the split for awhile and its just not clicking for me.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    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.

  12. #12
    Javanooby is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    Thanks Junky, I kind of follow what your saying but am just brain dead on this, can you give a example?

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

    Default

    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.

  14. #14
    Javanooby is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    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;
        }  //end
    Last edited by Fubarable; 05-10-2011 at 04:16 AM. Reason: code tags added

  15. #15
    Javanooby is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    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.

  16. #16
    Javanooby is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    Editing this message, I got it simply forgot to remove a I++ as I was recoding. Thanks for the guidance :) I was working on this a long time.
    Last edited by Javanooby; 05-10-2011 at 04:52 AM.

Similar Threads

  1. [SOLVED] read last line
    By Azndaddy in forum New To Java
    Replies: 22
    Last Post: 04-22-2012, 09:22 PM
  2. Read from command line
    By sehudson in forum New To Java
    Replies: 12
    Last Post: 02-23-2011, 04:44 AM
  3. Replies: 1
    Last Post: 02-13-2011, 03:09 PM
  4. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  5. Replies: 4
    Last Post: 09-10-2009, 10:18 AM

Posting Permissions

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