Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Help with my code please!

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Output to a 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.

    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 as well as a count of all the invalid votes e.g. any vote that was above 7 and/or 0.

    Help please!
    Last edited by halfwaygone; April 17th, 2010 at 11:47 AM. Reason: Did some more work on my own :D