Results 1 to 20 of 20
- 08-09-2010, 03:35 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
Printing contents of Arraylist -- strange issue
First of all, let me say yes this is for a class at school. Sorry if that is an issue for you, I am only trying to learn what I have done wrong. I do not expect you to do any work for me, and really appreciate any time you can give.
This is the first time I have used an Arraylist, so you may look at some of the things I have done, and wonder wtf is going on, sorry about that. 1 requirement is that I have an external file with a class that defines 3 variables. Here is that class.
"external class with variables"
Java Code:public class Student { String name; double gpa; int hrsPassed; }
Java Code:import java.util.*; import java.io.*; import java.text.*; import java.lang.*; public class Program12 { public static void main(String[] args) throws Exception { Scanner inFile = new Scanner (new FileReader("H:/Metro/NetBeansProjects" + "/CS1050/data/12in.dat")); PrintWriter outFile = new PrintWriter("H:/Metro/NetBeansProjects" + "/CS1050/data/12out.dat"); ArrayList list = new ArrayList(); int count=0; count = fillArray(list, count, inFile, outFile); gpaMethod(list, count, outFile); outFile.close(); } //****************************************************************************** public static int fillArray(ArrayList list, int count, Scanner inFile, PrintWriter outFile) { String tempData=null, tempName, line, s; char letter; StringTokenizer st, str; int i=0,number, gpaSwitch=0, credits=0; double pairsLength=0, gpaCalc; outFile.println("Data Echo:"); outFile.println("=========="); while(inFile.hasNext()) { gpaCalc=0; line = inFile.nextLine(); st = new StringTokenizer(line); tempName = st.nextToken("#"); Student studMuffin = new Student(); tempData = st.nextToken("#"); //data echo outFile.println(tempName + "#" + tempData + "#"); str = new StringTokenizer(tempData); //determine that there is at least one pair of data pairsLength = trimRight(trimLeft(tempData)).length(); //data process and store in ArrayList while(str.hasMoreTokens() && pairsLength>=3) { number = Integer.parseInt(str.nextToken()); letter = str.nextToken().charAt(0); gpaSwitch=Character.getNumericValue(letter); switch (gpaSwitch) { case 10: credits += number; //credit for A; gpaCalc += 4 * number; //4 points for A break; case 11: credits += number; //credit for B; gpaCalc += 3 * number; //3 points for B break; case 12: credits += number; //credit for C; gpaCalc += 2 * number; //2 points for C break; case 13: credits += number; //credit for D; gpaCalc += 1 * number; //1 point for D break; case 15: credits += 0; //no credit for F gpaCalc += 0; //no points for F break; case 28: credits += number; //credit for S; break; default: credits += 0; //grade not recognized gpaCalc += 0; //grade not recognized } } studMuffin.name = trimLeft(trimRight(tempName)); studMuffin.gpa = gpaCalc/credits; studMuffin.hrsPassed = credits; System.out.println("Name: " + studMuffin.name); System.out.println("GPA: " + studMuffin.gpa); System.out.println("Credits: " + studMuffin.hrsPassed); list.add(studMuffin); i++; } outFile.println("==================================================="); return i; } //****************************************************************************** public static void gpaMethod(ArrayList list, int count, PrintWriter outFile) { Iterator i1 = list.iterator(); while (i1.hasNext()) { System.out.print(i1.next() + " , "); } } //****************************************************************************** private static String trimRight(String data) { int index = data.length()-1; if (index >= 0) { while (data.charAt(index) == ' ') { index--; } if (index < 0) { return ""; } else { return data.substring(0,index+1); } } else { return data; } }//end of trimRight(String name) //****************************************************************************** private static String trimLeft(String data) { int index = 0; if (data == null) return ""; while (data.charAt(index) == ' ') { data = data.substring(1); } return data; }//end of trimLeft(String name) }
Java Code:System.out.println("Name: " + studMuffin.name); System.out.println("GPA: " + studMuffin.gpa); System.out.println("Credits: " + studMuffin.hrsPassed);
Java Code:Iterator i1 = list.iterator(); while (i1.hasNext()) { System.out.print(i1.next() + " , "); }
Output(System.out ONLY):
Java Code:Name: Van Zell David GPA: 2.0 Credits: 8 Name: Toole Nancy GPA: 2.1578947368421053 Credits: 19 Name: Tucker Henry GPA: 0.20833333333333334 Credits: 24 Name: Van Zell Sally GPA: 0.4117647058823529 Credits: 34 Name: Tucker Becky GPA: 1.2244897959183674 Credits: 49 Name: Stratton Charles GPA: 0.2857142857142857 Credits: 63 Name: Toole Charles GPA: 0.0 Credits: 63 Name: Thomas William GPA: 0.5066666666666667 Credits: 75 Student@14318bb , Student@ca0b6 , Student@10b30a7 , Student@1a758cb , Student@1b67f74 , Student@69b332 , Student@173a10f , Student@530daa ,
What is going on with this "Student@..." That is not what I thought I put into the Arraylist...
Input file (if you care)
Java Code:Van Zell David # 4 D 4 B # Toole Nancy # 3 A 3 B 5 A 4 X # Tucker Henry # 5 D 3 F 2 U 4 F # Van Zell Sally # 4 C 6 D # Tucker Becky # 3 A 3 A 2 A 4 A 3 A # Stratton Charles # 2 S 2 D 5 C 4 D 1 C # Toole Charles # 3 U # Thomas William # 4 A 3 B 2 C 3 B #
- 08-09-2010, 03:55 PM #2
Hi there, no problem if this is a school assignment, as long as you do the work and post a specific question as you did. In your sysouts you print the attributes of your Student class
Java Code:System.out.println("Name: " + studMuffin.name);
Java Code:System.out.println(studMuffin);
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 08-09-2010, 04:34 PM #3
Further explanation.
Student@ca0b6 is what is returned by the Object toString() method. Its the class name followed by its memory location.
As PhHein said: If you want to see info about your class you need to override the toString() method to return what you'd like to see.
- 08-09-2010, 04:54 PM #4
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
Thank you! I just wrote a HUGE response and must have timed out. I am working on it on my machine and will post in just a second.
- 08-09-2010, 05:17 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
Now, wouldnt I need to put this into some kind of for or while loop and reference index numbers or something so I can bring out the entire Arraylist? Or is the way I am filling it going to overwrite it self every time?
If I stick a: System.out.println (list.size()); in the main method right after the call to the fillArray method, it will return the proper amount, 8 "students".
I keep wanting to print it like a regular array... studMuffin.name[1] and so on.
- 08-09-2010, 05:25 PM #6
Not sure what all your questions are.
The ArrayList class has methods that allow you to retrieve elements based on an index, like you can do with an array. You use a method call vs using the array reference: []
- 08-09-2010, 05:29 PM #7
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
Well, I guess my thought is, by printing only studMuffin.name, you are really just printing a string from another class, not something that is stored in an Arraylist.
Is that correct, or incorrect?
Also, If I have multiple studMuffins in the Arraylist "list" how do I roll though and print each out?
System.out.println(studMuffin); only gets me Class name with memory address.
Edit: maybe this will help. I have to have the student name, gpa, and total credits in the array list (variables in the Student class)
- 08-09-2010, 05:31 PM #8by printing only studMuffin.name, you are really just printing a string from another class
If I have multiple studMuffins in the Arraylist "list" how do I roll though and print each out?
System.out.println(studMuffin); only gets me Class name with memory address.Last edited by Norm; 08-09-2010 at 05:33 PM.
- 08-09-2010, 05:34 PM #9
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
"I see" said the blind man. Working on this now.
- 08-09-2010, 05:36 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
- 08-09-2010, 05:45 PM #11
The API doc says:
This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required ...
- 08-09-2010, 05:52 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
"not required".
On top of which, hashCode() is more often overridden than toString().
I did say <pedant>...:)
- 08-09-2010, 06:03 PM #13
Ok, why pick at nits.
BTW FWIT I've overridden toString() 100 times more than hashCode()
- 08-09-2010, 06:07 PM #14
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
I suppose I could rewrite toString(). However, I would be really surprised if that is what she is wanting us to do. Is that the only way to pull data from the Arraylist?
- 08-09-2010, 06:09 PM #15
No for god's sake, you've successfully pulled the data from the arraylist. You just don't do the same things to print the information. Read my previous post.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 08-09-2010, 06:15 PM #16
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
- 08-09-2010, 06:22 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 08-09-2010, 06:23 PM #18
There was no hostility whatsoever, but you've never posted a different attempt which considers the replies you've been given.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 08-09-2010, 08:40 PM #19
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
You are right, I did try what I thought you where saying and I was doing it wrong. I did not want to post everything I was doing and waste your time. I am sorry.
Originally Posted by PhHein
Java Code:public static void gpaMethod(ArrayList list, int count, PrintWriter outFile) { for(int i=0; i<list.size();i++) { Student S = (Student)list.get(i); System.out.println("gpaMethod"); System.out.println("=========="); System.out.println("Name: " + S.name); System.out.println("GPA: " + S.gpa); System.out.println("Credits: " + S.hrsPassed); } }
Java Code:System.out.println("fillArray"); System.out.println("========="); System.out.println("Name: " + studMuffin.name); System.out.println("GPA: " + studMuffin.gpa); System.out.println("Credits: " + studMuffin.hrsPassed);
Java Code:fillArray ========= Name: Van Zell David GPA: 2.0 Credits: 8 fillArray ========= Name: Toole Nancy GPA: 2.1578947368421053 Credits: 19 fillArray ========= Name: Tucker Henry GPA: 0.20833333333333334 Credits: 24 fillArray ========= Name: Van Zell Sally GPA: 0.4117647058823529 Credits: 34 fillArray ========= Name: Tucker Becky GPA: 1.2244897959183674 Credits: 49 fillArray ========= Name: Stratton Charles GPA: 0.2857142857142857 Credits: 63 fillArray ========= Name: Toole Charles GPA: 0.0 Credits: 63 fillArray ========= Name: Thomas William GPA: 0.5066666666666667 Credits: 75 gpaMethod ========== Name: Van Zell David GPA: 2.0 Credits: 8 gpaMethod ========== Name: Toole Nancy GPA: 2.1578947368421053 Credits: 19 gpaMethod ========== Name: Tucker Henry GPA: 0.20833333333333334 Credits: 24 gpaMethod ========== Name: Van Zell Sally GPA: 0.4117647058823529 Credits: 34 gpaMethod ========== Name: Tucker Becky GPA: 1.2244897959183674 Credits: 49 gpaMethod ========== Name: Stratton Charles GPA: 0.2857142857142857 Credits: 63 gpaMethod ========== Name: Toole Charles GPA: 0.0 Credits: 63 gpaMethod ========== Name: Thomas William GPA: 0.5066666666666667 Credits: 75
- 08-10-2010, 08:36 AM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
This is the internet, isn't that what it's for? Pointless discussion about minutiae?
:D
Ah, the stuff I tend to work on usually involves object comparisons, so equals() is almost always written, and consequently so is hashCode(). But output is less of a problem.
Similar Threads
-
Problems saving a file and printing its contents.
By gth05 in forum New To JavaReplies: 3Last Post: 12-03-2009, 10:05 PM -
ArrayList printing
By tommyyyy in forum New To JavaReplies: 4Last Post: 03-20-2009, 04:33 AM -
Printing the contents of an array of objects
By Mr.Paplu in forum New To JavaReplies: 1Last Post: 03-19-2009, 04:49 PM -
Issue with printing line
By Azndaddy in forum Advanced JavaReplies: 1Last Post: 04-04-2008, 07:37 PM -
Printing contents of a web page
By Java Tip in forum Java TipReplies: 0Last Post: 11-26-2007, 12:37 PM
Bookmarks