Results 1 to 4 of 4
Thread: help with project and arrays
- 05-08-2011, 01:29 PM #1
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
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
-
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.
- 05-08-2011, 02:18 PM #3
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
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 02:31 PM. Reason: quote tags changed to code tags
-
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:
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.Java Code:file = new Scanner(new File("C:/Users/answers.txt")); while (file.hasNext()){ count++; line = file.nextLine(); System.out.println(+count+" "+line); }
Next, does your code read the file? Are you seeing output from your println statements?
Next:
I told you in the first post how to use the array. Your code above has no array code in it.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 :/
Similar Threads
-
Project with Arrays and Grades
By rochla16 in forum New To JavaReplies: 13Last Post: 03-29-2011, 12:40 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
New project, need some help :( (arrays)
By Isshin in forum New To JavaReplies: 0Last Post: 03-21-2010, 10:51 PM -
open existing project project ..
By itaipee in forum EclipseReplies: 1Last Post: 12-28-2008, 08:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks