Results 1 to 4 of 4
  1. #1
    lamboss is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default help with project and arrays

    hey guys i'm using netbeans and i cant get my head around my assigment , its really confusing me for days ive tried everything and nothing seems to work for me .. basically this is what i am meant to do ..

    1. Design and develop a Graphical User Interface (GUI) for this application. The interface should comprise two buttons. A load button that loads the information from the text file and a display button, which when pressed displays the studentsí ids of those students who answered all questions correctly.
    (20 marks) .... i have this done

    2. Provide the code to declare and create a two-dimensional String array to store data from answers.txt. Also, develop the actionPerformed method for the load button. When the user presses the load button the information should be read from answers.txt into this array.
    (25 marks) this part is really confusing me , as i am unsure how to create an array that would link the answers.txt file to it and then load it ? :/

    3. Develop the actionPerformed method for the display button. When the user presses the display button the studentsí ids of those students who answered all questions correctly are displayed. The correct solution is TTFFTTFFTT. (Hint use JOptionPane to display)


    really would appreciate your help and be very thankful
    Attached Files Attached Files

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

    Default

    In pseudo-code, I think that the steps could be:

    Java Code:
    In the class itself declare a 2-D String array variable, String[][] myStringArray and  <continued>
                      initialize it for number of rows of data, and number of columns
    In your load button's actionPerformed method:
       Create a File object associated with the answers.txt disk file.  <continued>
                     File has a constructor that will allow you to do this.
       Create a Scanner object using this File object
       create an int index variable called i and set = 0
       while the Scanner object has nextLine() (while loop checking Scanner's hasNextLine() method)
          Read the next line into a String variable called line using Scanner's nextLine() method.
          Call the split(",") method on the line variable and put into a String[] variable, say called splitLine
          Use a for loop that uses int j as an index, and goes from 0 to splitLine.length
             place splitLine[j] into the 2D array, myStringArray, at the [i][j] position
          end for loop
          increment the i variable by calling i++;
       end while loop
    end actionPerformed for the load button

    Note that there are other ways to solve this problem.

  3. #3
    lamboss is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    how do i create and intialise the array to load the file answers.txt though?

    i am using a different approach in the load button which is this: , but i dont think its correct since there is no array the answers.txt is loading from :/
    Java Code:
    try{
    			file = new Scanner(new File("C:/Users/answers.txt"));
    			while (file.hasNext()){
    				count++;
    				line = file.nextLine();
                                  
    				System.out.println(+count+" "+line);
    			}
    		}catch (Exception e){
    			System.out.println(e.getMessage());
    		}finally{
    			if (file != null)
    				file.close();
    		}
    Last edited by Fubarable; 05-08-2011 at 03:31 PM. Reason: quote tags changed to code tags

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

    Default

    First off, I changed your quote tags in your post above to code tags so that the code is more easily readable. Next, you're using Scanner a bit wrong by doing this:
    Java Code:
       file = new Scanner(new File("C:/Users/answers.txt"));
       while (file.hasNext()){
        count++;
        line = file.nextLine();
                                  
        System.out.println(+count+" "+line);
       }
    since you're checking if the Scanner hasNext but then calling nextLine. These two methods should correlate, and yours don't. If you use hasNext, then you should call Scanner's next method once inside the loop, and if you need to call nextLine in the loop (which I think you should do), you need to check hasNextLine.

    Next, does your code read the file? Are you seeing output from your println statements?

    Next:
    i am using a different approach in the load button which is this: , but i dont think its correct since there is no array the answers.txt is loading from :/
    I told you in the first post how to use the array. Your code above has no array code in it.

Similar Threads

  1. Project with Arrays and Grades
    By rochla16 in forum New To Java
    Replies: 13
    Last Post: 03-29-2011, 01:40 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  3. New project, need some help :( (arrays)
    By Isshin in forum New To Java
    Replies: 0
    Last Post: 03-21-2010, 11:51 PM
  4. open existing project project ..
    By itaipee in forum Eclipse
    Replies: 1
    Last Post: 12-28-2008, 09:12 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
  •