Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Saving multiple Strings to an ArrayList


  • Please log in to reply
9 replies to this topic

#1 sidd0123

sidd0123

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:08:56 PM

Posted 08 April 2010 - 02:55 PM

This is going to get compicated for me to explain! I have a class(Roster) that loads a file that contains the following: student ID, last name, first name, grade level and numeric grade.

My String format() takes the information posted above and sends it to my Main, which prints all of the Students information out. There are 4 students. However, I only print 1 student because I am overwriting the Students due to the fact that I don't have a place to save it. To solve this problem I am creating an ArrayList, and this is where the problem lies.

I don't know how to create one and save information to it. The JAVA DOCS are a confusing me.

I'll post my code here. (It gets confusing as to what each class does, please bear with me!)

Roster Class: Takes a String roster name and create an empty Roster, loads a saved Roster, sends loaded file to Main to be printed in the console.

Student Class: Initialize and create a letter grade based on numerical grade of each Student, returns a roster(not the class, but the arraylist) of students with all information printed out neatly via String format(). <<<<<<< That's the problem, I don't know how to make an arraylist and intialize it to save students so that I don't overwrite them.


ALL CODE COMPILES. THERE ARE NO ERRORS AT THIS SPECIFIC MOMENT

Roster Class Code:
package package13;import java.io.File;import java.util.Scanner;import javax.swing.JFileChooser;public class Roster {		static String tempID;	static String Lname;	static String Fname;	static int grade;	static double finalaverage;	static String classname;	public static String getAuthor(){						return "Sid Patel";	}	public Roster(){			}		public void load(){						try {			// use JFileChooser to find the file you want to open			// you can limit files displayed using setFileFilter()			JFileChooser chooser = new JFileChooser(".");						// show the dialog and see if they picked something			int windowbox = chooser.showOpenDialog(null);			// choosing a file results in a File handle and a file name that goes with that			// you can get different versions of the file name using different methods						String file = "";			File folder = null;						if (windowbox == JFileChooser.APPROVE_OPTION) {	// APPROVE_OPTION means they picked something				folder = chooser.getSelectedFile();				file = folder.getName();				classname = file;				System.out.println("You chose to open file: '" + file + "'");			} else {				System.out.println("nothing to open...");				System.exit(0);			}			// a file Scanner can be a more useful way of reading a file			Scanner fscanner = new Scanner(folder);						// we'll read lines into this string, and count the lines as we go			String record;			int line = 0;			String classname = fscanner.nextLine();			// the has***() methods allow you to "peek" at what's coming next to make sure it is there and it is right			while (fscanner.hasNextLine()) {				record = fscanner.nextLine();				String [] array = record.split(",");				tempID = array[0];				Lname = array[1];				Fname = array[2];				grade = Integer.parseInt(array[3]); 				finalaverage = Double.parseDouble(array[4]);								Student Sid = new Student(tempID, Lname, Fname, grade, finalaverage);														}			// close the file when you're done			fscanner.close();		} 		catch (Exception e) {			// Catch exception if any			System.err.println("Error: " + e.getMessage());		}	}				public String format(){				String organize = String.format("%5s check this again \n\n%5s   %5s   %5s   %5d   %5f ", 				classname, tempID, Lname, Fname, grade, finalaverage);				return organize;	}	}

Student Class Code:
		
else if ((average <= 76.99) && (average >= 73))
letteraverage = "C";

else if ((average <= 72.99) && (average >= 70))
letteraverage = "C-";

else if ((average <= 69.99) && (average >= 67))
letteraverage = "D+";

else if ((average <= 66.99) && (average >= 63))
letteraverage = "D";

else if ((average <= 62.99) && (average >= 60))
letteraverage = "D-";

else
letteraverage = "F";

}


public static void ArrayList(){

ArrayList classlist = new ArrayList();

classlist.add("Hello");


}


public String format(){

String organize = String.format("%5d % %5d % %5d % %5d % %5d %",
tempID, Lname, Fname, grade, finalaverage);


return organize;
}
}
_linenums:0'>package package13;import java.util.ArrayList;public class Student { static String tempID; static String Lname; static String Fname; static int grade; static double finalaverage; public static String getAuthor() { return "Sid Patel"; } public Student(String studentID, String last, String first, int gradelevel, double average){ tempID = studentID; Lname = last; Fname = first; grade = gradelevel; finalaverage = average; String letteraverage; if (average >= 97) letteraverage = "A+"; else if ((average <= 96.99) && (average >= 93)) letteraverage = "A"; else if ((average <= 92.99) && (average >= 90)) letteraverage = "A-"; else if ((average <= 89.99) && (average >= 87)) letteraverage = "B+"; else if ((average <= 86.99) && (average >= 83)) letteraverage = "B"; else if ((average <= 82.88) && (average >= 80)) letteraverage = "B-"; else if ((average <= 79.99) && (average >= 77)) letteraverage = "C+'"; else if ((average <= 76.99) && (average >= 73)) letteraverage = "C"; else if ((average <= 72.99) && (average >= 70)) letteraverage = "C-"; else if ((average <= 69.99) && (average >= 67)) letteraverage = "D+"; else if ((average <= 66.99) && (average >= 63)) letteraverage = "D"; else if ((average <= 62.99) && (average >= 60)) letteraverage = "D-"; else letteraverage = "F"; } public static void ArrayList(){ ArrayList classlist = new ArrayList(); classlist.add("Hello"); } public String format(){ String organize = String.format("%5d % %5d % %5d % %5d % %5d %", tempID, Lname, Fname, grade, finalaverage); return organize; }}

Main Class Code:
package package13;public class StudentRosterMain {	/**	 * @param args	 */		public static void main(String[] args) {				// create a new Roster				Roster myClass = new Roster();				// load the roster from a file				myClass.load();				// print out the roster				System.out.println("The class list is \n" + myClass.format());		System.out.println("The Student class brought to you by " + Student.getAuthor());		System.out.println("The Roster  class brought to you by " + Roster.getAuthor());	}}

If you are still confused, I guess you can go to my teacher site where the requirements of each class are listed <http://central.wmrhsd.org/FACULTY_FILES/aherzog/1477/Assignments/CS%20-%20Assignment%2013%20-%20Student%20Roster.pdf>

Sorry, but the link code box didn't open for me:(

Edited by sidd0123, 08 April 2010 - 03:01 PM.


BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:56 PM

Posted 08 April 2010 - 06:02 PM

Just for clarification. Basically you are creating a comma-delimited string. Each string needs to be added to an ArrayList?

#3 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:08:56 PM

Posted 09 April 2010 - 11:31 AM

Yup, that exactly what I would like to do. I'll update my code since I made a few changes to it. Right now, my code only prints one students information because I overwrite the previous students info with the new students info.

#4 strolln

strolln

  • Members
  • 400 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:N. Calif.
  • Local time:04:56 PM

Posted 09 April 2010 - 12:29 PM

I haven't coded in Java in quite awhile but here goes...

Once the record is read in from the file, parsed and new Student record is created, you need to assign that new record to your arrayList. The code would be something along the lines of StudentArr.add(Student).

Here's a link to an example using arraylist: arraylist example code

It appears you also need to add some code to handle the first line of the file since it doesn't contain record info.

Hope this helps. Good luck.

Edited by strolln, 09 April 2010 - 12:57 PM.

To Err is human; to really foul things up requires a Bleeping Computer!

#5 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:08:56 PM

Posted 12 April 2010 - 03:01 PM

It did, thanks.

I'm not done with it though so no relief for me yet. Do you know how to limit name fields?

This is my format method(I'll post what it returns and what I want it to return to below).


public String format(){				String organize = String.format("  %5s     %5s,   %5s       %5d     %5f   %5s %n", 				tempID, Lname, Fname, grade, finalaverage, letter);		return organize;	}

This is what that prints out:
You chose to open file: 'StudentRosterTestData1.txt'The class list is IB Computer Science  C2010001     Surname1,   Givenname1          12     92.000000      A-   C2013379     Last2,   First2           9     87.500000      B+   C2011017     Lastname3,   Given3          11     85.000000       B   C2012099     Reallylongname,   Somethingtobecutoff           8     -24.500000   Error The Student class brought to you by *****The Roster  class brought to you by *****


And this is what I want it to look like:

C2010001 Surname1  , Givenname1 12 92.0  AC2013379 Last2         , First2             9 87.5  B+C2011017 Lastname3 , Given3         11 85.0  BC2012099 Reallylong  , Somethingt    8 -24.5 ERR

It doesn't seem to aligning, but here, go to my school website, and the example it shown.

<http://central.wmrhsd.org/FACULTY_FILES/aherzog/1477/Assignments/CS%20-%20Assignment%2013%20-%20Student%20Roster.pdf> Go to box 7

Hope this helps you understand what I am saying or trying to convey.

Edited by sidd0123, 12 April 2010 - 03:07 PM.


#6 strolln

strolln

  • Members
  • 400 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:N. Calif.
  • Local time:04:56 PM

Posted 12 April 2010 - 09:09 PM

You need to specify a width for the fields. Here's a link to an example: Formatted string example
To Err is human; to really foul things up requires a Bleeping Computer!

#7 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:08:56 PM

Posted 13 April 2010 - 07:56 AM

That's what I want to do, but I can't use that because your example wants me to physcially input each of the students, whereas in my code, the number of students could be random. Does that make sense?

#8 strolln

strolln

  • Members
  • 400 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:N. Calif.
  • Local time:04:56 PM

Posted 13 April 2010 - 06:16 PM

Based on the example I posted, your code would look something like this:

public String format(){

String FormatSpec = "%5$-10s %5$-10s, %5$-10s %5$-10s %5$-10d %5$-10f %5$-10s \n";

String organize = String.format(FormatSpec, tempID, Lname, Fname, grade, finalaverage, letter);

return organize;
}

You need to figure out the right FormatSpec for your output. Like I said, it's been awhile for me so I can't guarantee it's right but it should get you close.
To Err is human; to really foul things up requires a Bleeping Computer!

#9 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:08:56 PM

Posted 14 April 2010 - 09:32 AM

I got it, it wasn't the escape sequences, but your link led me to another page that displayed how to do it. You put a period before the width, which specifies the limitation. Thanks, the program works and i submitted it.

Thanks

Edited by sidd0123, 14 April 2010 - 11:31 AM.


#10 strolln

strolln

  • Members
  • 400 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:N. Calif.
  • Local time:04:56 PM

Posted 14 April 2010 - 04:36 PM

I'm glad you got it working. I'm not sure what you are talking about as far as the period before the width, the link goes right to the page I wanted it to go to and the example is at the bottom of the page.
To Err is human; to really foul things up requires a Bleeping Computer!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users