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 6 of 6

Thread: Can't seem to align the numbers

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can't seem to align the numbers

    I have tried numerous ways of trying to align the numbers, but so far no luck. I am sure its something simple, if I could have a little help that would be appreciated.
    Here is the output
    and code below


    Student Quiz 1 Quiz 2 MidTerm Final Final% Grade
    1
    2
    3
    4
    8 7 89 94 88 B
    9 6 77 90 83 B
    10 10 65 88 85 B
    7 5 80 81 75 C



     
     
    import java.util.*;
    import java.io.*;
     
    public class Main
    {
        public static void main(String args[])
        {
            Scanner grades = null; 
            try
            {
                grades = new Scanner(new File("prog349f.dat"));
            }
            catch(Exception e)
            {
                System.err.println("Error: " + e.getMessage());
            }
     
            System.out.println("Student  Quiz 1  Quiz 2  MidTerm  Final  Final%  Grade");
            System.out.println("   1 ");
            System.out.println("   2 ");
            System.out.println("   3 ");
            System.out.println("   4 ");
            while(grades.hasNextInt())
            {
     
                int quiz1 = grades.nextInt();
                int quiz2 = grades.nextInt();
                int midterm = grades.nextInt();
                int finals = grades.nextInt();
     
                Grader gradeThis = new Grader(quiz1, quiz2, midterm, finals);
     
                double Grade1 = (gradeThis.getQuizAvg());
                double Grade2 = (gradeThis.getMidAvg());
                double Grade3 = (gradeThis.getFinalAvg());
                int finalPer = (int)(Grade1 + Grade2 + Grade3);  
     
     
                System.out.format("%12d", quiz1);
                System.out.format("%8d", quiz2);
                System.out.format("%8d", midterm);
                System.out.format("%8d", finals);
                System.out.format("%8d", finalPer);
     
                if(finalPer < 60)
                {
                    System.out.println("      F");
                }
                if(finalPer > 60 && finalPer < 70)
                {
                    System.out.println("      D");
                }
                if(finalPer > 70 && finalPer < 80)
                {
                    System.out.println("      C");
                }
                if(finalPer > 80 && finalPer < 90)
                {
                    System.out.println("      B");
                }
                if(finalPer >= 90)
                {
                    System.out.println("      A");
                }
            }
        }     
    }
     
    public class Grader
    {
        public Grader(int q1, int q2, int mid, int fin)
        {
            firstquiz = q1;
            secquiz = q2;
            midTerm = mid;
            theFinal = fin;
        }
     
        private double findQuizAvg()
        {
           double quizAvg = (((firstquiz * 10 + secquiz * 10 ) /2) * .25);
           return(quizAvg);
        }
     
        private double findMidAvg()
        {
            double midAvg =  (midTerm * .25);
            return(midAvg);
        }
     
        private double findFinalAvg()
        {
            double finalAvg = (theFinal * .50);
            return(finalAvg);
        }
     
        public double getQuizAvg()
        {
            double gradeOne = findQuizAvg();
            return(gradeOne);
        }
     
        public double getMidAvg()
        {
            double gradeTwo = findMidAvg();
            return(gradeTwo);
        }
     
        public double getFinalAvg()
        {
            double gradeThree = findFinalAvg();
            return(gradeThree);
        }
     
        public int firstquiz;
        public int secquiz; 
        public int midTerm; 
        public int theFinal;
    }


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Can't seem to align the numbers

    What would your desired output be? What is different from the output you are getting? Are you expecting this output, or is there an insidious bug in your program that is causing this output?

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't seem to align the numbers

    Here is what I want the output to look like:
    Student Quiz 1 Quiz2 MidTerm Final Final % Grade
    1 8 7 89 94 88 B
    2 9 6 77 90 83 B
    3 10 10 65 88 85 B
    4 7 5 80 81 75.5 C

    but it looks like this:
    Student Quiz 1 Quiz 2 MidTerm Final Final% Grade
    1
    2
    3
    4
    8 7 89 94 88 B
    9 6 77 90 83 B
    10 10 65 88 85 B
    7 5 80 81 75 C

  4. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Can't seem to align the numbers

    System.out.println(" 1 ");
    System.out.println(" 2 ");
    System.out.println(" 3 ");
    System.out.println(" 4 ");
    You print out all of your student numbers on different lines before writing their data by putting these four lines of code right next to each other. Think about your program, and try to determine the correct order in which you need to print the information to get the display you want.

    In other words, you are doing something like this right now:

    Student name
    Student name
    Student info
    Student info

    But what you want is:

    Student name Student info
    Student name Student info

    Can you think of a way to rearrange your code to get that result?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't seem to align the numbers

    I was able to figure it out thanks for the advice.

  6. #6
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Can't seem to align the numbers

    You're welcome! Good luck with the rest of your program!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. GridBagLayout problems with align
    By lugaru in forum AWT / Java Swing
    Replies: 2
    Last Post: December 14th, 2011, 11:51 PM
  2. How do you align?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 18th, 2011, 05:33 PM
  3. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 02:30 PM
  4. How to align the items in a form (grid layout)?
    By onlybarca in forum AWT / Java Swing
    Replies: 4
    Last Post: November 27th, 2010, 11:38 PM
  5. System.out.print -align
    By juwan in forum Object Oriented Programming
    Replies: 3
    Last Post: November 17th, 2010, 04:36 PM