#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2

    Clean up my print


    When I print out a series of numbers, they are very unaligned.

    Is there a quick fix for this?

    this is my code:
    Code:
    import java.io.*;
    import java.text.*;
    import java.util.*;
    
    public class Proj5
    {
    	static char key [] = {'1','1', '2','2','1','1','3','2','4','1','3','5','4','1','2'};
    	
    	public static void main(String[] args) throws Exception
    	{
    		Scanner s = new Scanner(System.in);
    		
    		System.out.print("Please enter file name:  ");
    		String [][] file = readFile(s.nextLine());
    		while (file == null)
    		{
    			System.out.println("Cannot find file.\nPlease try again. ");
                            file = readFile(s.nextLine());
    			//System.exit(-1);
    		}
    		
    		int[] scores = new int[file.length];
    		
    		for(int i = 0; i < scores.length; i++)
    		{
    			scores[i] = gradeQuiz(file[i][1]);
                    	display(scores[i], "This Should be the WID");
                    }
    		
    		System.out.print("");
                    
                   
                    
            }
          
    	
    	static int gradeQuiz(String quiz)
    	{
    		int score = 0;
    		
    		for(int i = 0; i < quiz.length(); i++)
    		{
    			if(quiz.charAt(i)==key[i])
    				score++;
    		}
    		
    		return score;
    	}// end gradeQuiz
    
    	static String[][] readFile(String fileName) throws Exception
    	{
    		File f = new File(fileName);
    		
    		if(f.exists())
    		{
    			Scanner in = new Scanner(f);
    			
    			int count = 0;
    			while(in.hasNext())
    			{
    				if(in.nextLine().trim().length()>15)
    				{
    					count ++;
    				}
                                    else
                                    {
                                            String [] WID = new String [50];
                                    }
    			}
    			
    			String[][] result = new String[count][2];
    			
    			in.close();
    			in = new Scanner(f);
    			
    			count=0;
    			while(in.hasNext())
    			{
    				String line = in.nextLine().trim();
    				if(line.length()>15)
    				{
    					String[] parts = line.split(",");
    					result[count] = parts;
    					count ++;
    				}
    			}
    			
    			in.close();
    			return result;
    		}
    		else
    		{
    			return null;
    		}
    	}// end readFile
       static void display(int a , String b) {
           
              DecimalFormat df = new DecimalFormat("#.0");
              
              
              String grade;
              
              
              double percent = (double)a/15*100;
              
              if (percent > 90) 
                  grade = "A";
              else if (percent >80)
                  grade = "B";
              else if (percent>70)
                  grade = "C";
              else if (percent>60)
                  grade = "D";
              else 
                  grade = "F";
              
              
    
           //System.out.println("   " + b + "\n");   
           System.out.println("Number correct:  Percentage correct:     Grade:     Average: \n     ");
           System.out.print(a + "                  " + df.format(percent) + "                " +  grade + "              " +  average(a,5));
         
            
        }// end display
       
       static double average (double a, int b) {
           
           for (int x = 0; x< b; x++){
                a +=1;
           }
           return (a/b);
       }
    }
    This is the output:

    Code:
    Please enter file name:  QuizScores.txt
    Number correct:  Percentage correct:     Grade:     Average: 
         
    15                  100.0                A              4.0Number correct:  Percentage correct:     Grade:     Average: 
         
    9                  60.0                F              2.8Number correct:  Percentage correct:     Grade:     Average: 
         
    13                  86.7                B              3.6Number correct:  Percentage correct:     Grade:     Average: 
         
    9                  60.0                F              2.8Number correct:  Percentage correct:     Grade:     Average: 
         
    6                  40.0                F              2.2
    I don't want it to put all the headers on the right side either.

    I was trying to make it so there was one header (number correct, percent correct, grade, average) and all the values were put underneath.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    I don't want it to put all the headers on the right side either.
    Only print the header one time before the loop that prints the columns that are to go underneath the header.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2
    Originally Posted by NormR
    Only print the header one time before the loop that prints the columns that are to go underneath the header.
    It seems like you and me are the only ones on this site :P

    I did as you said, but for some reason average still prints multiple times and I want to print it only once.

    I put average down in it's own method. I don't see why it is printing once for every a.
    My outputFile has printed correctly, but the console doesn't.
    Do you see what's wrong?

    This is the code:
    Code:
    //Darris
    
    import java.io.*;
    import java.text.*;
    import java.util.*;
    
    public class Proj5
    {
    	static char key [] = {'1','1', '2','2','1','1','3','2','4','1','3','5','4','1','2'};
    	
    	public static void main(String[] args) throws Exception
    	{
    		Scanner s = new Scanner(System.in);
    		
    		System.out.print("Please enter file name:  ");
    		String [][] file = readFile(s.nextLine());
                    
    		while (file == null)
    		{
    			System.out.println("Cannot find file.\nPlease try again. ");
                            file = readFile(s.nextLine());
    			//System.exit(-1);
    		}
                   
                    System.out.println("Number correct:  Percentage correct:     Grade:   \n     ");
    		
    		
                    int[] scores = new int[file.length];
    		
    		for(int i = 0; i < scores.length; i++)
    		{
    			scores[i] = gradeQuiz(file[i][1]);
                    	display(scores[i], "This Should be the WID", scores);
                    }
    		
    		System.out.print("");
                    
                   
                    
            }
          
    	
    	static int gradeQuiz(String quiz)
    	{
    		int score = 0;
    		
    		for(int i = 0; i < quiz.length(); i++)
    		{
    			if(quiz.charAt(i)==key[i])
    				score++;
    		}
    		
    		return score;
    	}// end gradeQuiz
    
    	static String[][] readFile(String fileName) throws Exception
    	{
    		File f = new File(fileName);
    		
    		if(f.exists())
    		{
    			Scanner in = new Scanner(f);
    			
    			int count = 0;
    			while(in.hasNext())
    			{
    				if(in.nextLine().trim().length()>15)
    				{
    					count ++;
    				}
                                    else
                                    {
                                            String [] WID = new String [50];
                                    }
    			}
    			
    			String[][] result = new String[count][2];
    			
    			in.close();
    			in = new Scanner(f);
    			
    			count=0;
    			while(in.hasNext())
    			{
    				String line = in.nextLine().trim();
    				if(line.length()>15)
    				{
    					String[] parts = line.split(",");
    					result[count] = parts;
    					count ++;
    				}
    			}
    			
    			in.close();
    			return result;
    		}
    		else
    		{
    			return null;
    		}
    	}// end readFile
       static void display(int a , String b, int [] scores) throws FileNotFoundException, IOException {
           
              DecimalFormat df = new DecimalFormat("#.0");
              
              
              String grade;
              
              
              double percent = (double)a/15*100;
              
              if (percent > 90) 
                  grade = "A";
              else if (percent >80)
                  grade = "B";
              else if (percent>70)
                  grade = "C";
              else if (percent>60)
                  grade = "D";
              else 
                  grade = "F";
              
              
    
           System.out.print(a + "                  " + df.format(percent) + "                " +  grade + "              " );
        
         
            
           
           
           
           
    
           Scanner inFile = new Scanner (new File ("QuizScores.txt") );
           File results = new File ("results.txt");
           FileWriter fw;
           fw = new FileWriter(results, true);
           PrintWriter outFile = new PrintWriter ("results.txt");
           PrintWriter outputFile = new PrintWriter(fw);
           
           //here
           
           File fileName = new File ("QuizScores.txt");
           Scanner quizScores = new Scanner(fileName);
            outputFile.println("Number correct:  Percentage correct:     Grade: \n");
               int i = -1; 
        while (quizScores.hasNext()){
            if (quizScores.hasNext()){
                i++;
            }
            String WID [] = new String [51];
            
         
                String check = quizScores.nextLine();
    
                String [] pieces = check.split(",");
    
                WID[0]=pieces[0];
                WID[1]=pieces[1];
               
                
    
           
            
            outputFile.print(a + "                  " + df.format(percent) + "                    " +  grade + "                       \n");
    
          
            
       }
        outputFile.println("\n\nAverage: " + average(scores));
        System.out.println("\n\nAverage: " + average(scores));
        quizScores.close();
        outFile.close();
        outputFile.close();
    
        }// end display
       
       static double average (int a[]) {
           double total= 0;
           for (int x = 0; x< a.length; x++){
               total += a[x] ;
           }
           
    
        
           return (total/a.length);
       }
    }

    This is the console output:
    Code:
    Please enter file name:  QuizScores.txt
    Number correct:  Percentage correct:     Grade:   
         
    15                  100.0                A              
    
    Average: 3.0
    9                  60.0                F              
    
    Average: 4.8
    13                  86.7                B              
    
    Average: 7.4
    9                  60.0                F              
    
    Average: 9.2
    6                  40.0                F              
    
    Average: 10.4

    And this is the file output:
    Code:
    Number correct:  Percentage correct:     Grade: 
    
    6                  40.0                    F                       
    6                  40.0                    F                       
    6                  40.0                    F                       
    6                  40.0                    F                       
    6                  40.0                    F                       
    
    
    Average: 10.4
    I want the a little of both outputs to be in each other, but I don't see what's happening.

    I want average to print only once with the average of all the scores on the console, and I want it to list the number correct for all the students not just one on the file output.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    want average to print only once
    Common report programs:
    print a header before the loop
    loop through the details and print the details
    after the details print out a footer

    Your code must print what you want to be in the footer inside the loop with the details.
    Move the print statement for the footer outside the loop.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2
    Code:
    import java.io.*;
    import java.text.*;
    import java.util.*;
    
    public class Proj5
    {
    	static char key [] = {'1','1', '2','2','1','1','3','2','4','1','3','5','4','1','2'};
    	
    	public static void main(String[] args) throws Exception
    	{
    		Scanner s = new Scanner(System.in);
    		
    		System.out.print("Please enter file name:  ");
    		String [][] file = readFile(s.nextLine());
                    
    		while (file == null)
    		{
    			System.out.println("Cannot find file.\nPlease try again. ");
                            file = readFile(s.nextLine());
    			//System.exit(-1);
    		}
                   
                    System.out.println("Number correct:  Percentage correct:     Grade:   \n     ");
    		
    		
                    int[] scores = new int[file.length];
    	
                    
                    for(int i = 0; i < scores.length; i++)
    		{
    			scores[i] = gradeQuiz(file[i][1]);
                    	display(scores[i], "This Should be the WID", scores);
                    }
    		
    		System.out.print("");
                    
                   
                    
            }
          
    	
    	static int gradeQuiz(String quiz)
    	{
    		int score = 0;
    		
    		for(int i = 0; i < quiz.length(); i++)
    		{
    			if(quiz.charAt(i)==key[i])
    				score++;
    		}
    		
    		return score;
    	}// end gradeQuiz
    
    	static String[][] readFile(String fileName) throws Exception
    	{
    		File f = new File(fileName);
    		
    		if(f.exists())
    		{
    			Scanner in = new Scanner(f);
    			
    			int count = 0;
    			while(in.hasNext())
    			{
    				if(in.nextLine().trim().length()>15)
    				{
    					count ++;
    				}
                                    else
                                    {
                                            
                                    }
    			}
    			
    			String[][] result = new String[count][2];
    			
    			in.close();
    			in = new Scanner(f);
    			
    			count=0;
    			while(in.hasNext())
    			{       
                                    String line = in.nextLine().trim();
    				if(line.length()>15)
    				{
    					String[] parts = line.split(",");
    					result[count] = parts;
    					count ++;
    				}
    			}
                          
    			
    			in.close();
    			return result;
    		}
    		else
    		{
    			return null;
    		}
    	}// end readFile
       static void display(int a , String b, int [] scores) throws FileNotFoundException, IOException {
           
              DecimalFormat df = new DecimalFormat("#.0");
              
              Scanner in = new Scanner("QuizScores.txt");
              in = new Scanner("QuizScores.txt");
              String line = in.nextLine().trim();                     
              String WID [] = new String [51];
              String [] pieces = line.split(",");      
              for (int x = 0; x <pieces.length-1; x++ )                     
              {
                  if (pieces.length<=7)
                  {
                      WID[x] = pieces[x];
                  }// end if   
              }
              String grade;
              
              
    
           
               Scanner inFile = new Scanner (new File ("QuizScores.txt") );
               File results = new File ("results.txt");
               FileWriter fw;
               fw = new FileWriter(results, true);
               PrintWriter outFile = new PrintWriter ("results.txt");
               PrintWriter outputFile = new PrintWriter(fw);
           
           //here
           
           File fileName = new File ("QuizScores.txt");
           Scanner quizScores = new Scanner(fileName);
            outputFile.println("Number correct:  Percentage correct:     Grade: \n");
            
      //  while (quizScores.hasNext())
            for (int x = 0; x<scores.length; x++){
            if (quizScores.hasNext()){
                
              double percent = (double)a/15*100;
    
              if (percent > 90) 
                  grade = "A";
              else if (percent >80)
                  grade = "B";
              else if (percent>70)
                  grade = "C";
              else if (percent>60)
                  grade = "D";
              else 
                  grade = "F";
                
              
              System.out.print(a + "                  " + df.format(percent) + "                " +  grade + "              \n" );  
              outputFile.print(a + "                  " + df.format(percent) + "                    " +  grade + "                       \n");
              String end = quizScores.nextLine();  
            }// end if
        }
        outputFile.println("\n\nAverage: " + average(scores));
        System.out.println("\n\nAverage: " + average(scores));
        quizScores.close();
        outFile.close();
        outputFile.close();
    
        }// end display
       
       static double average (int a[]) {
           double total= 0;
           for (int x = 0; x< a.length; x++){
               total += a[x] ;
           }
           
    
        
           return (total/a.length);
       }
    }
    I have fixed it to this:

    file output:
    Code:
    Number correct:  Percentage correct:     Grade: 
    
    6                  40.0                    F                       
    6                  40.0                    F                       
    6                  40.0                    F                       
    6                  40.0                    F                       
    6                  40.0                    F                       
    
    
    Average: 10.4

    console output:
    Code:
    run:
    Please enter file name:  QuizScores.txt
    Number correct:  Percentage correct:     Grade:   
         
    15                  100.0                A              
    15                  100.0                A              
    15                  100.0                A              
    15                  100.0                A              
    15                  100.0                A              
    
    
    Average: 3.0
    9                  60.0                F              
    9                  60.0                F              
    9                  60.0                F              
    9                  60.0                F              
    9                  60.0                F              
    
    
    Average: 4.8
    13                  86.7                B              
    13                  86.7                B              
    13                  86.7                B              
    13                  86.7                B              
    13                  86.7                B              
    
    
    Average: 7.4
    9                  60.0                F              
    9                  60.0                F              
    9                  60.0                F              
    9                  60.0                F              
    9                  60.0                F              
    
    
    Average: 9.2
    6                  40.0                F              
    6                  40.0                F              
    6                  40.0                F              
    6                  40.0                F              
    6                  40.0                F              
    
    
    Average: 10.4
    BUILD SUCCESSFUL (total time: 5 seconds)
    Why is it doing it 5 times each?
    And notice that my average print line is not even in a loop. I don't know how that gets repeated.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Why is it doing it 5 times each?
    Look at the change you made to the previous version. The problem with that version was the average was being printed inside the loop instead of at the end.

    Now the loop is printing each line and the average inside of some loop.

    Change back to the previous version.
    Move the code that printed the average inside the loop so that it is after the loop just like the header is before the loop.

IMN logo majestic logo threadwatch logo seochat tools logo