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

Thread: Output Alignment

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Output Alignment

    Hello, here I am again. The program is already working but I don't know how to align the output because it is so messed up. Hope you can help me again with this.

    PROGRAM:
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package javaapplication1;


    import java.io.*;
    public class MultiDimensionalArray {
    public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
    String answer;
    String name[] = new String[4];
    int grade[][] = new int[4][3];
    double tg[] = new double[4];

    do{
    for(int x=0;x<name.length;x++)
    {
    System.out.println("Student "+(x+1));
    System.out.print(" Name: ");
    name[x]=br.readLine();

    System.out.print(" PG : ");
    grade[x][0]=Integer.parseInt(br.readLine());
    System.out.print(" MG : ");
    grade[x][1]=Integer.parseInt(br.readLine());
    System.out.print(" FG : ");
    grade[x][2]=Integer.parseInt(br.readLine());

    tg[x]=grade[x][0]*.3+grade[x][1]*.3+grade[x][2]*.4;
    }

    System.out.println("\nSTUDENTS\tPG\tMG\tFG\tTG");
    for(int x=0;x<name.length;x++)
    {
    System.out.print(name[x]+"\t");
    for(int y=0;y<name.length-1;y++)
    {
    System.out.print(grade[x][y]+"\t");
    }
    System.out.println(tg[x]);
    }

    System.out.print("\nWould you like to try again? [Y/N]: ");
    answer=br.readLine();
    }
    while(answer.equalsIgnoreCase("y"));
    }
    }
    OUTPUT:
    Student 1
    Name: Alpha
    PG : 78
    MG : 79
    FG : 70
    Student 2
    Name: Beta
    PG : 87
    MG : 89
    FG : 80
    Student 3
    Name: Charlie
    PG : 97
    MG : 98
    FG : 90
    Student 4
    Name: Delta
    PG : 77
    MG : 88
    FG : 99

    STUDENTS PG MG FG TG
    Alpha 78 79 70 75.1
    Beta 87 89 80 84.8
    Charlie 97 98 90 94.5
    Delta 77 88 99 89.1

    Would you like to try again? [Y/N]:


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,619
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: Output Alignment

    Ok, there is a problem with what you posted, and that problem is the forum doesnt like indenting. Try to post the output as a picture or something so we can see it.


    EDIT:
    I had a look at it. If you mean that crazy wave thing that it does, replace your code with this in the correct place:
    System.out.println("Student "+(x+1));
    				System.out.print(" Name: ");
    				name[x]=br.readLine();
     
    				System.out.print(" PG : ");
    				grade[x][0]=Integer.parseInt(br.readLine());
    				System.out.print("MG : ");
    				grade[x][1]=Integer.parseInt(br.readLine());
    				System.out.print("FG : ");
    				grade[x][2]=Integer.parseInt(br.readLine());
     
    				tg[x]=grade[x][0]*.3+grade[x][1]*.3+grade[x][2]*.4;
    				System.out.println();

    It will unfortunately add an extra line between students, but its the best you could hope for if you want a quick-fix.

    As far as the Table on the bottom, you'll have to double to tab between name and and the start of your grade array. Thats the best I can think of.
    Last edited by aussiemcgr; August 18th, 2010 at 10:45 AM.

Similar Threads

  1. Why am I getting this output?
    By ptabatt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 6th, 2010, 07:36 PM
  2. need help with output.
    By VictorCampudoni in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 24th, 2010, 11:25 PM
  3. The positioning and alignment of the text on the paper to be printed
    By java_fledgeling in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 8th, 2010, 08:54 PM
  4. Alignment
    By angelaffxmaniac in forum Java Theory & Questions
    Replies: 1
    Last Post: May 13th, 2010, 08:55 AM
  5. Replies: 1
    Last Post: March 31st, 2009, 02:49 PM