Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 23rd, 2013, 10:10 AM
darris darris is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 32 darris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 48 m 51 sec
Reputation Power: 1
Line up string output (delete spaces ahead according to length of a string)

SO here's my output
Code:
Please enter the name of student 1: 
Khader Adnan
Please enter score for Exam 1: 30
Please enter score for Exam 2: 45
Please enter score fox Exam 3: 50
Please enter score for Final Exam: 90
Do you wish to enter another? <y/n> 
y



Please enter the name of student 2: 
Drik Scott
Please enter score for Exam 1: 40
Please enter score for Exam 2: 50
Please enter score fox Exam 3: 30
Please enter score for Final Exam: 90 
Do you wish to enter another? <y/n> 

y



Please enter the name of student 3: 
Lisa Simpsonianmasterofnameexamples
Please enter score for Exam 1: 40
Please enter score for Exam 2: 50
Please enter score fox Exam 3: 30
Please enter score for Final Exam: 20
Do you wish to enter another? <y/n> 
n


*********CLASS RESULTS*********
 ADNAN, KHADER		Exam percentage: 86.0%		Final grade: B
 SCOTT, DRIK		Exam percentage: 84.0%		Final grade: B
MASTEROFNAMEEXAMPLES, LISA		Exam percentage: 56.0%		Final grade: F
Number of students: 3

Number of A's: 0 (0.0%)
Number of B's: 2 (66.7%)
Number of C's: 0 (0.0%)
Number of D's: 0 (0.0%)
Number of F's: 1 (33.3%)

Average: 75.33% 


Here is the code:
Code:
import java.util.*;
import java.text.*;


public class Proj4 {
public static void main (String[] args){
Scanner s = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#0.0");

    final int MIN = 0;
    final int MAX = 50;
    final int FINAL_MAX = 100;
    
   
    int indexStudents= 101;
    int indexScores = 4;
    int[][] exams = new int[indexStudents][indexScores];
    String output = "";
    int count =0;
    String[] name= new String [202];
    double percent = 0;
    
    //Extra Credit 
    int A = 0;
    int B = 0;
    int C = 0;
    int D = 0;
    int F = 0;
    double average =0;
    DecimalFormat df1 = new DecimalFormat("#0.00");
    
    
do{
        System.out.println("\n\n");
        System.out.println("Please enter the name of student " + (count+1) + ": ");
        name [count]= s.next(); name[count+2] = s.nextLine();
        name [count] = name[count].toUpperCase();
        name [count+2] = name[count+2].toUpperCase();
      do{
       System.out.print("Please enter score for Exam 1: ");
        exams[count][0] = s.nextInt();
       if(exams[count][0]>MAX || exams[count][0]<MIN)
           System.out.println("Invalid entry. Enter number 0-50. ");
      }while (exams[count][0]>MAX || exams[count][0]<MIN);
      do{
       System.out.print("Please enter score for Exam 2: ");
        exams[count][1] = s.nextInt();
      if(exams[count][1]>MAX || exams[count][1]<MIN)
           System.out.println("Invalid entry. Enter number 0-50. ");
      }while (exams[count][1]>MAX || exams[count][1]<MIN);
      do{
       System.out.println("Please enter score fox Exam 3: ");
        exams[count][2] = s.nextInt();
      if(exams[count][2]>MAX || exams[count][2]<MIN)
           System.out.println("Invalid entry. Enter number 0-50. ");
      }while (exams[count][2]>MAX || exams[count][2]<MIN);
      do{
       System.out.println("Please enter score for Final Exam: ");
        exams[count][3] = s.nextInt();
       if(exams[count][3]>100 ||exams[count][3]<0)
           System.out.println("Invalid entry. Enter number 0-100. ");
       
      }while (exams[count][3]>FINAL_MAX ||exams[count][3]<MIN);
      
  percent = ((exams[count][0] + exams[count][1] + exams[count][2] + exams[count][3])/(double)(FINAL_MAX+(MAX*3))) * 100;
  average += percent;
      
               if (percent >= 90.0){
                output += name[count+2] + ", " + name[count] + "\t\tExam percentage: "+ df.format(percent) + "%\t\tFinal grade: " + "A";
               A++; 
               }
            else if (percent >=80.0){
                output += name[count+2] + ", " + name[count] + "\t\tExam percentage: "+ df.format(percent) + "%\t\tFinal grade: " + "B";
               B++;
            }
            else if (percent >=70.0){
                output += name[count+2] + ", " + name[count] + "\t\tExam percentage: "+ df.format(percent) + "%\t\tFinal grade: " + "C";
            C++;
            }
            else if (percent >=60.0){
                output += name[count+2] + ", " + name[count] + "\t\tExam percentage: "+ df.format(percent) + "%\t\tFinal grade: " + "D";
            D++;
            }
            else {
                output += name[count+2] + ", " + name[count] + "\t\tExam percentage: "+ df.format(percent) + "%\t\tFinal grade: " + "F";
                F++;}
            output +=  "\n";
            
            count++;
System.out.println("Do you wish to enter another? <y/n> ");
} while ("Y".equals(s.nextLine()) || "y".equals(s.nextLine()));



    System.out.print("\n\n*********CLASS RESULTS*********\n"+output);
    
 System.out.println("Number of students: " + count + "\n\nNumber of A's: " + A + " ("+ df.format(((double)A/count)*100) + "%)" + "\nNumber of B's: " + B + " ("+ df.format(((double)B/count)*100) + "%)" +"\nNumber of C's: " + C +" ("+ df.format(((double)C/count)*100) + "%)" + "\nNumber of D's: " + D +" ("+ df.format(((double)D/count)*100) + "%)" +"\nNumber of F's: " + F + " ("+ df.format(((double)F/count)*100) + "%)");   
 System.out.println("\nAverage: " + df1.format((average/count)) + "% ");   
    }//end main
} //end class


I'm trying to figure out how I can make it so long names (Lisa Masterofnameexamples) don't throw the whole output off kilter.
Is there a way I can delete spaces according to the length of the string?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Line up string output (delete spaces ahead according to length of a string)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap