Results 1 to 7 of 7
  1. #1
    javanator is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default 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.

    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();
    	}
    }
    }
    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.

    Help please?

  2. #2
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Try to traverse through the array and write each element to the file. Instead,

    Java Code:
    p.print(results[index]);
    try this,

    Java Code:
    for(int index = 0; index < results.length; index++) {
        p.print(results[index]);
    }

  4. #4
    javanator is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    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

    Java Code:
     if((v>7)||(v==0)){
    				results2[0] = results2[0]+1; 
    			if((v!=0) || (v<=7)){
    				results2[1] = results2[1]+1; 
    			}
    			}
    results2 is a new array I made to hold this information.
    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 08:02 PM. Reason: forgot to add the code :P

  5. #5
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    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; 
    			}
    you'll need 2 variables, one for invalid and 1 for total, (don't need 3 because valid votes is total - invalid)

    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

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    You have:
    Java Code:
    		while((v !=0) && (v<=7)){
    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:
    		{
    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:
    			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();
    So here is where you really need to validate the input. Maybe something like this:
    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]
    Then you have...
    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; 
    			}
    		}
    Now that you have validated your input, you can replace all of this with:
    Java Code:
    [COLOR="RoyalBlue"]                        results[v - 1]++;
    [/COLOR]
    And this code is still within your while() loop because of that extra curly brace above.
    Java Code:
    		FileWriter f = new FileWriter("C:\\temp_name\\results.txt");
    		PrintWriter p = new PrintWriter(f);
    		
    		p.print(results); 
    		
    		p.close();
    		f.close();
    	}
    }
    -Gary-
    Last edited by gcalvin; 04-17-2010 at 09:30 PM. Reason: missed handling of invalid votes first time

  7. #7
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    oops cal.

    I didn't even look at the condition in his while loop, i assumed that it was looping a set number of times as if it knew how many votes were being inputted.

Similar Threads

  1. writing results of recursion into file
    By sara12345 in forum New To Java
    Replies: 2
    Last Post: 04-12-2010, 02:22 PM
  2. Need help formatting File Output
    By aaroncarpet in forum New To Java
    Replies: 2
    Last Post: 11-26-2009, 06:26 PM
  3. problem with output of jar file
    By nishant.4545 in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2009, 05:00 PM
  4. No output to file
    By shiva in forum Java Servlet
    Replies: 0
    Last Post: 04-24-2009, 03:06 PM
  5. Replies: 7
    Last Post: 11-19-2008, 03:09 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
  •