Results 1 to 7 of 7
Thread: Output to results file help!
- 04-17-2010, 06:07 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
Output to results file help!
Hi,
I'm using the following code that takes input from the user for votes that are counted on a scale of 1-7, the programme terminates taking input once 0 is entered.
When I compile this program, and check the file results.txt it only says '[I@14318bb', I want it to list the count of each number.Java Code:import java.io.*; import java.util.*; public class Arrays1 { /** * @param args */ public static void main(String[] args) throws IOException{ Scanner a = new Scanner(new File("C:\\temp_Name\\votes.txt")); int maxIndx = -1; String name[] = new String[8]; name[0] = ""; while(a.hasNext()){ maxIndx++; name[maxIndx] = a.next(); } a.close(); int results[] = new int[name.length]; Scanner kbb = new Scanner(System.in); System.out.println("Enter your vote number, 1. Victor Taylor 2. Denise Duncan 3. Kamal Ramdhan 4. Michael Ali 5. Anisa Shah 6. Carol Khan 7. Gary Owen"); int v = kbb.nextInt(); while((v !=0) && (v<=7)){ { Scanner kb = new Scanner(System.in); System.out.println("Enter your vote number, 1. Victor Taylor 2. Denise Duncan 3. Kamal Ramdhan 4. Michael Ali 5. Anisa Shah 6. Carol Khan 7. Gary Owen"); v = kb.nextInt(); if(v==1){ results[0] = results[0]+1; } if(v==2){ results[1] = results[1]+1; } if(v==3){ results[2] = results[2]+1; } if(v==4){ results[3] = results[3]+1; } if(v==5){ results[4] = results[4]+1; } if(v==6){ results[5] = results[5]+1; } if(v==7){ results[6] = results[6]+1; } } FileWriter f = new FileWriter("C:\\temp_name\\results.txt"); PrintWriter p = new PrintWriter(f); p.print(results); p.close(); f.close(); } } }
Help please?
- 04-17-2010, 06:13 PM #2
Member
- Join Date
- Mar 2010
- Posts
- 88
- Rep Power
- 0
results is an array.
When you print an array it prints out it's memory location, not it's values.
Print all of the values individually.
- 04-17-2010, 06:23 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Try to traverse through the array and write each element to the file. Instead,
try this,Java Code:p.print(results[index]);
Java Code:for(int index = 0; index < results.length; index++) { p.print(results[index]); }
- 04-17-2010, 06:58 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
Oh ok that works! Thanks a lot :D
Just one more thing.
I'm trying to keep a count as well of all invalid votes, e.g. all votes that were above 7 or equal to 0, and all valid votes and the number of all votes. I'm trying but it doesn't seem to work :/.
I added this to the while loop
results2 is a new array I made to hold this information.Java Code:if((v>7)||(v==0)){ results2[0] = results2[0]+1; if((v!=0) || (v<=7)){ results2[1] = results2[1]+1; } }
And I added this to the print section
Java Code:p.println("Number of invalid votes: " + results[0]); p.println("Number of valid votes: " + results[1]);Last edited by javanator; 04-17-2010 at 07:02 PM. Reason: forgot to add the code :P
- 04-17-2010, 07:01 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 88
- Rep Power
- 0
you'll need 2 variables, one for invalid and 1 for total, (don't need 3 because valid votes is total - invalid)Java Code:if(v==1){ results[0] = results[0]+1; } if(v==2){ results[1] = results[1]+1; } if(v==3){ results[2] = results[2]+1; } if(v==4){ results[3] = results[3]+1; } if(v==5){ results[4] = results[4]+1; } if(v==6){ results[5] = results[5]+1; } if(v==7){ results[6] = results[6]+1; }
inside of you if statement add 1 more entry for > 7 || < 1
add 1 to invalid inside of that if, and then add 1 to total inside of your while loop.
Cheers :)
Cruncher
EDIT: you should actually make that an if...else if...else statement, then the last condition for an invalid vote would be easy. you might also want to consider using a switch statement
- 04-17-2010, 08:26 PM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
You have:
The second comparison doesn't help you, because it comes too early to make sure it's valid for your choices. You really may as well just do while(true) here, and break out of the loop if you get your sentinel value (0) or show an error if the value is out of range after you read it.Java Code:while((v !=0) && (v<=7)){
What is this extra curly brace for? The rest of your code is really not indented properly, and is probably not doing what you think it should.Java Code:{
So here is where you really need to validate the input. Maybe something like this:Java Code:Scanner kb = new Scanner(System.in); System.out.println("Enter your vote number, 1. Victor Taylor 2. Denise Duncan 3. Kamal Ramdhan 4. Michael Ali 5. Anisa Shah 6. Carol Khan 7. Gary Owen"); v = kb.nextInt();
Then you have...Java Code:[COLOR="RoyalBlue"] if (v == 0) break; // done entering votes if (v < 0 || v > 7) { // TODO: print an error message and increment invalidVotes continue; } [/COLOR]
Now that you have validated your input, you can replace all of this with:Java Code:if(v==1){ results[0] = results[0]+1; } if(v==2){ results[1] = results[1]+1; } if(v==3){ results[2] = results[2]+1; } if(v==4){ results[3] = results[3]+1; } if(v==5){ results[4] = results[4]+1; } if(v==6){ results[5] = results[5]+1; } if(v==7){ results[6] = results[6]+1; } }
And this code is still within your while() loop because of that extra curly brace above.Java Code:[COLOR="RoyalBlue"] results[v - 1]++; [/COLOR]
-Gary-Java Code:FileWriter f = new FileWriter("C:\\temp_name\\results.txt"); PrintWriter p = new PrintWriter(f); p.print(results); p.close(); f.close(); } }Last edited by gcalvin; 04-17-2010 at 08:30 PM. Reason: missed handling of invalid votes first time
- 04-18-2010, 01:59 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 88
- Rep Power
- 0
Similar Threads
-
writing results of recursion into file
By sara12345 in forum New To JavaReplies: 2Last Post: 04-12-2010, 01:22 PM -
Need help formatting File Output
By aaroncarpet in forum New To JavaReplies: 2Last Post: 11-26-2009, 05:26 PM -
problem with output of jar file
By nishant.4545 in forum Advanced JavaReplies: 2Last Post: 07-04-2009, 04:00 PM -
No output to file
By shiva in forum Java ServletReplies: 0Last Post: 04-24-2009, 02:06 PM -
Search a word(taken from one file) in another file and give the line as the output
By SwapnaNaidu in forum New To JavaReplies: 7Last Post: 11-19-2008, 02:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks