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 3 of 3

Thread: Display Array on another class after extracting from txt file and into array problem

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face Display Array on another class after extracting from txt file and into array problem

    Hi I'm currently trying to extract data out of txt file then place it into a array. I would like to be able to use the array from another class but i cant seems to make it work after 6 hours. Can someone help me?

    public String[] readSFile(int z) throws IOException{
    BufferedReader br;
    String [] anArray = new String[100];
    br = new BufferedReader(new FileReader("students.txt"));
    String s = null;
    while(( s = br.readLine()) != null ){
    anArray = s.split(";");


    for (String str : anArray) {
    System.out.println(str);
    }
    System.out.println();

    }
    br.close();
    return anArray;
    }

    Another class. I'm actually trying to display it so i can actually choose what i want to display. Eg. extract names only, but the problem is when i enter 1? i was expecting anArray[1] to display
    public static void main(String[] args) throws Exception {
    StudentFile test = new StudentFile();
    test.readSFile(1);
    }
    Thanks for reading Any help would be appreciated


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,112
    Thanks
    65
    Thanked 2,718 Times in 2,668 Posts

    Default Re: Display Array on another class after extracting from txt file and into array prob

    Can you explain what is not working in your program? Does it compile? Does it execute?
    What does it do when it executes?
    Have you tried debugging the code by adding print statements to show the contents of variables and the program flow logic?

    The readSFile method returns a value that the calling code doesn't receive in a variable.

    problem is when i enter 1? i was expecting anArray[1] to display
    Where is the code for this?
    Would anArray[0] display when a 0 is entered?
    Last edited by Norm; May 12th, 2011 at 01:31 PM.

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Display Array on another class after extracting from txt file and into array prob

    Maybe the data your trying to display is in anArray[0] as Norm has said, an array starts from [0] not [1]

Similar Threads

  1. In a class create an array list of elements of another class, help!
    By LadyBelka in forum Collections and Generics
    Replies: 3
    Last Post: May 4th, 2011, 05:00 PM
  2. display pdf file into jpanel problem
    By Jhovarie in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 12th, 2011, 03:29 PM
  3. HELP. Passing Array to class.
    By videodrome in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 13th, 2010, 04:35 AM
  4. [SOLVED] Display 2d array aligment
    By Scotty in forum Collections and Generics
    Replies: 0
    Last Post: November 26th, 2010, 03:37 PM
  5. Extracting the BINDING element from WSDL file
    By Sai in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2010, 02:56 AM