Results 1 to 19 of 19
  1. #1
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default Having trouble reading external txt file to an array

    I can't figure out why I can't create a new array from a text file read in. My code is:

    public static void main(String[] args) throws java.io.IOException
    {
    FileInputStream fis1 = new FileInputStream("States.txt");
    DataInputStream dis1 = new DataInputStream(fis1);
    BufferedReader br1 = new BufferedReader(new InputStreamReader(dis1));

    State[] myStates;
    int count = 0;
    String inline;
    inline = br1.readLine();

    while (!(inline == null))
    {
    myStates = new String (inline);
    count++;

    } // end while

    The error occurs on myStates = new State (inline);
    I have another class named State but it says incompatible types.
    Wondering if anyone can figure it out, thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    myStates = new State (inline);
    I have another class named State but it says incompatible types.
    The compiler is saying the object to the right of the = is incompatible with the type on the left of the =
    What type is myStates? Remember an array is a type.

    You need to index the array to get an element of type State.

  3. #3
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    My teacher said to use this code here to read in the lines and create an array:

    State [ ] myStates;
    Int count = 0;
    String inline;

    inline = br1.readLine();

    while (inline != null)
    {

    myStates [count++] = new State (inline);

    } // end while

    I put it in and it gave multiple errors. I have all imports needed and everything for the read in.

    I'm still confused on how to get the array started :/
    I would say myStates is of type State but I'm not sure if that's correct
    Last edited by Metastar; 07-20-2010 at 11:29 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    it gave multiple errors
    Please copy and paste them all here.

    how to get the array started
    Three steps:
    Define the array variable:
    State [ ] myStates; // define an array of State objects

    Next you must assign it a value:
    myStates = new State[somenumberhere]; // create the array to hold somenumber of elements

    Last you must assign the elements in the array a value:
    myStates[index] = new State(...); // assign a value to the element at index

    myStates is of type State
    Close. myStates is an array of State objects, not a State object

  5. #5
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    Ok, so I changed some things around and now I have:

    public static void main(String[] args) throws java.io.IOException
    {
    FileInputStream fis1 = new FileInputStream("States.txt");
    DataInputStream dis1 = new DataInputStream(fis1);
    BufferedReader br1 = new BufferedReader(new InputStreamReader(dis1));

    State[] myStates;
    int count = 0;
    String inline;
    inline = br1.readLine();

    while (!(inline == null))
    {
    myStates = new State [34];
    myStates[index] = new State (inline);
    count++;

    } // end while
    System.out.println(myStates[0]);
    }
    }

    The error that comes up is that it can't find variable index. I subbed a 0 in for index and tried to print out what was at 0 and I got the error:

    Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:470)
    at java.lang.Integer.parseInt(Integer.java:499)
    at project4cquast.State.<init>(State.java:19)
    at project4cquast.Main.main(Main.java:29)
    Java Result: 1

    My constructor is this(State.java):

    public class State
    {
    private String stateName, stateCapital, stateAbbrev, stateRegion;
    private int statePop, stateRegionNum;

    public State(String stateInput)
    {
    stateName = stateInput.substring(0, 14).trim();
    stateCapital = stateInput.substring(15, 30).trim();
    stateAbbrev = stateInput.substring(31, 32).trim();
    statePop = Integer.parseInt(stateInput.substring(33,40).trim( ));
    stateRegion = stateInput.substring(41,55).trim();
    stateRegionNum = Integer.parseInt(stateInput.substring(56).trim());

    }//end parseData Constructor()
    }

    The first couple lines of the text file are(all line up in notepad):

    Washington Olympia WA 5689263West 6
    Oregon Salem OR 3281974West 6
    Massachusetts Boston MA 6147132New_England 1
    Connecticut Hartford CT 3274069New_England 1
    Rhode_Island Providence RI 988480New_England 1
    New_York Albany NY18146200Middle_Atlantic2
    Pennsylvania Harrisburg PA12001451Middle_Atlantic2
    New_Jersey Trenton NJ 8115011Middle_Atlantic2

    Still having a bit of trouble figuring out whats going on.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    at java.lang.Integer.parseInt(Integer.java:499)
    at project4cquast.State.<init>(State.java:19)
    at project4cquast.Main.main(Main.java:29)
    Look at the lines in your source. line 19 in State called parseInt with bad data.
    Add a println() before that statement to show what the data was that it was trying to convert to an int.

    Break the chained statement up into single step statements:
    Integer.parseInt(stateInput.substring(33,40).trim( ));
    Do the substring into a String and look at /display it before parsing it.

  7. #7
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    I'm still having trouble with what I should do. I added a print() after the constructor to see what i was parsing. I have all of the correct char positions but I'm still having trouble understanding how to get my text file data into my array and it's confusing me to no end. :/
    Last edited by Metastar; 07-21-2010 at 01:27 AM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    I'm still having trouble
    you'll have to post the error messages for anyone to help.

    I added a print() after the constructor to see what i was parsing.
    What did that show?

  9. #9
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    It never showed anything, my program just kept on running and running. It was only when I ended it that i saw any outputs but they were part of the error message and in the error message only the data from the first line of data was printed and it was printed over and over unending. What I'm still having trouble with is the incompatible types error message. i get this:

    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types
    required: project4cquast.State
    found: java.lang.String
    at project4cquast.Main.main(Main.java:28)
    Java Result: 1

    from this:

    public static void main(String[] args) throws java.io.IOException
    {
    FileInputStream fis1 = new FileInputStream("States.txt");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1));

    State myStates[];
    String inline;
    inline = br1.readLine();
    myStates = new State [34];

    while (!(inline == null))
    {

    myStates[] = new State (inline);
    count++;

    } // end while

    and I'm not getting how to declare myStates as the same type as the other.
    My programming knowledge is still at a low level so I'm thinking it is something small that I'm missing but I'm just not seeing it.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Where is line 28 in the source?
    When you post error messages be sure to find the source line from the number shown in the error message and post it with a comment saying something like: // THIS IS LINE 28

    Its impossible to help you without complete/correct info.

    Your program is looping forever. Look at any loops you have in the program. What condition keeps that loop going? What will stop the looping? Is there any way your code will NOT set the condition that will stop the loop?

    Copy the loop here and explain how the program will exit the loop.

  11. #11
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    I apologize I just registered on the forums today so I'm still learning what I need to post.

    Line 28 is the myStates[] = new State (inline);

    my while loop is supposed to read each line and put it into the array, increment count, and repeat until there are no more lines to read in. The problem I think it could be is: could it be that since it it looping infinitely on the first line I need some way of incrementing the array elements? if that makes sense

    My loop is:

    while (!(inline == null))
    {

    myStates[] = new State (inline);
    count++;

    } // end while

    It should keep going until there are no more lines and exit the loop, but again the problem lies in the myStates[] = new State (inline); line of my code. I'm doing something wrong here that is messing up the rest of my read.
    Last edited by Metastar; 07-21-2010 at 02:24 AM.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Remember this?
    Last you must assign the elements in the array a value:
    myStates[index] = new State(...); // assign a value to the element at index
    Compare to:
    myStates[] = new State (inline);

    until there are no more lines to read in.
    Where in the loop are lines being read? How does the variable: inline ever change?

  13. #13
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    Ok I think I got it, but a new problem arose(go figure). I now have this:

    FileInputStream fis1 = new FileInputStream("States.txt");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1));

    State myStates[];
    String inline;
    inline = br1.readLine();
    myStates = new State [35];
    int index=0;

    while (!(inline == null))
    {
    inline = br1.readLine();
    myStates[index] = new State (inline);
    index++;


    } // end while


    But now I get the error:
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 14
    at java.lang.String.substring(String.java:1934)
    at project4cquast.State.<init>(State.java:14)
    at project4cquast.Main.main(Main.java:29)
    Java Result: 1

    I know that it is in my constructor, but I don't see how it is out of bounds

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    at java.lang.String.substring(String.java:1934)
    at project4cquast.State.<init>(State.java:14)
    at project4cquast.Main.main(Main.java:29)
    Look at the above. You need to be able to read these.
    This is a trace of the calls from one method/constructor to the next.
    At the bottom the main method at line 29 called the State constructor: <init>
    In the State constructor at line 14 it called the substring() method.

    At the point the length of the String was less than 14. >String index out of range: 14
    Add a: println("stateInput=" + stateInput + "<");
    first thing in the constructor to show what it is getting.

    You need to test if there is a long enough String (at least 56 chars) before calling the State constructor because the code requires that many bytes.

  15. #15
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    Ok I did that and apparently it isn't reading any data at all. I have the States.txt in my folder so I don't know why it isn't reading them. Any thoughts?

    It was reading them before in the infinite loop error I stated above, but now when I add the println I get nothing.

    Edit: It's ok, I'll just have to have someone show me during my lecture tomorrow. I appreciate all of your help though. You got me past my initial problem :)

    I also changed my loop conditions and got a good read but it didn't read the first line and started on the second and only read 9 of the 34 lines. I don't know whats going on but one thing just keeps leading to another on this program.
    Last edited by Metastar; 07-21-2010 at 04:20 AM.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Java Code:
    inline = br1.readLine();
    myStates = new State [35];
    int index=0;
    
    while (!(inline == null))
    {
    inline = br1.readLine();
    myStates[index] = new State (inline);
    index++;
    } // end while
    Play computer with the code shown here. Do one line and the next and the next. You should see what records are read and what records are being used by the program

  17. #17
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    Hey Norm, I got it figured out finally lol. Was actually a small thing that had to do with the text file that my teacher uploaded, his char positions were flawed on his file. He uploaded a new one and everything worked like a charm. Thanks again for your help. :)

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    That is a small lesson for you.
    Always verify the data is whatt you expect before trying to use something that doesn't exist. Test for it and if its not there, print out an error message.

  19. #19
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

Similar Threads

  1. Generate and Save Array of Strings to external file
    By thestarncy in forum New To Java
    Replies: 1
    Last Post: 03-05-2010, 04:46 PM
  2. Reading a txt file and then storing it in a 2d array
    By blkshp1990 in forum New To Java
    Replies: 2
    Last Post: 11-05-2009, 12:31 AM
  3. Reading text file into an array
    By Mahesh_ps in forum New To Java
    Replies: 1
    Last Post: 10-09-2009, 03:04 PM
  4. Reading from a file to make an array
    By Bomber_Will in forum New To Java
    Replies: 11
    Last Post: 01-21-2009, 08:19 AM
  5. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-18-2008, 11:51 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
  •