Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Reading a file, then putting it in an array

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Reading a file, then putting it in an array

    For some reason, and I have no idea why. This code doesn't work. I am obviously a newbie to java, and programing in general. But this seems like it would just work. It does so in C++.

    public void Read()  //Just Reads the File, does not pass anything back
        {
        String test[];
        int x;
        try {
            BufferedReader readout = new BufferedReader(new FileReader("ClientNames.txt"));
        String names;
        while ((names = readout.readLine()) != null) 
        {
            x++;
            test[x] = names;
            System.out.print(" " + names + ", ");
        }
        readout.close();
        } catch (IOException e) {System.out.println("Could Not Read out file"); }
     
     
        }//End of Read


    And i get this error.

    x variable might not have been initialized. 
    test variable might not have been initialized.

    Off to work now, so I wont be able to respond right away. Thanks in advance =)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,343
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Reading a file, then putting it in an array

    int x;
    try {
    BufferedReader readout = new BufferedReader(new FileReader("ClientNames.txt"));
    String names;
    while ((names = readout.readLine()) != null)
    {
    x++;
    Where is x given a value? The compiler wants to see an assignment statement before you try to use it.

    Creating and using arrays is a two step process. First you define the array. Then you define the elements in the array.
    String[] anArray; // define an array
    anArray = new String[4]; // give the array 4 elements
    or
    String anArray = new String[5];

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Reading a file, then putting it in an array

    public void Read()  //Just Reads the File, does not pass anything back
        {
        String test = new String[100];
        int x =0;
        try {
            BufferedReader readout = new BufferedReader(new FileReader("ClientNames.txt"));
        String names = new String();
        while ((names = readout.readLine()) != null) 
        {
            x++;
            test[x] = names;
            System.out.print(" " + names + ", ");
        }
        readout.close();
        } catch (IOException e) {System.out.println("Could Not Read out file"); }
     
     
        }//End of Read

    I did that, and it still gets 2 errors, which are related.

    Says

    Found java.lang.String;

    Required java.lang.String[];

    What does this mean? I have the entire java.lang.*; imported

  4. #4
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Reading a file, then putting it in an array

    Try

    String[] test = new String[100];

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,343
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Reading a file, then putting it in an array

    When you get errors, please copy and paste full text of the error message here.

  6. #6
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Reading a file, then putting it in an array

    Quote Originally Posted by Faz View Post
    Try

    String[] test = new String[100];
    That did it. I thought i tried that, but i guess not =)

    Thanks for the help guys!

Similar Threads

  1. Reading file
    By nasi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 3rd, 2010, 04:14 AM
  2. Anyone Help me in XML file Reading??????????
    By goplrao in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 2nd, 2010, 12:04 PM
  3. Putting text fields in an array
    By Movies32 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 10th, 2010, 07:46 PM
  4. Reading .txt and storing into an array
    By vluong in forum Collections and Generics
    Replies: 1
    Last Post: January 4th, 2010, 02:07 PM
  5. [SOLVED] Problem in reading a file from java class
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: March 23rd, 2009, 10:31 AM