Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Norm

Thread: alignment problem [ please help ]

  1. #1
    janedoe is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Unhappy alignment problem [ please help ]

    hi everyone,
    so I recently started learning java and I'm already having some struggles
    here's my assignment : https://java1ksu.files.wordpress.com...nment-1v41.pdf


    As you can see I'm required to use a specific format.
    I tried to but for some reason the alignment is totally off
    here's my code:

    XML Code:
     1    
     2    
     3       //Imported library:
     4    
     5    import java.util.*;
     6    public class RegestrationSystem
     7    {
     8    static Scanner key = new Scanner (System.in);
     9    public static void main (String args[])
    10    {
    11      
    12       //input Section:
    13       
    14          //course code:
    15    
    16    System.out.println("Enter the course code");
    17          String courseCode = key.nextLine();
    18    
    19          //First Student:
    20       
    21    System.out.println("Enter the name of student1's Academic Advisor");
    22          String academicAdvisor1 = key.nextLine();
    23    System.out.println("Enter student1's name");
    24          String student1Name = key.nextLine();
    25    System.out.println("Enter student1's ID");
    26          int student1Id = key.nextInt();
    27    System.out.println("Enter student1's Email adress");
    28          String student1Email = key.next();
    29    System.out.println("Enter student1's score");
    30          double student1Score = key.nextDouble();
    31    System.out.println("Enter student1's grade");
    32          char student1Grade = key.next().charAt(0);
    33       
    34          //Second Student:
    35          
    36    key.nextLine();         
    37    System.out.println("Enter the name of student2's Academic Advisor");
    38          String academicAdvisor2 = key.nextLine();
    39    System.out.println("Enter student2's name");
    40          String student2Name = key.nextLine();
    41    System.out.println("Enter student2's ID");
    42          int student2Id = key.nextInt();
    43    System.out.println("Enter student2's Email adress");
    44          String student2Email = key.next();
    45    System.out.println("Enter student2's score");
    46          double student2Score = key.nextDouble();
    47    System.out.println("Enter student2's grade");
    48          char student2Grade = key.next().charAt(0);
    49    
    50       //output: 
    51       
    52       System.out.println("\t\t\t\t\t\tKing Saud University (KSU) - Riyadh\n\t\t\t\t\t\t          FALL 2015/2016\n\n\n\t\t\t\t\t      Academic Advisor:"+academicAdvisor1+"\n\nCourse Code:"+courseCode+"\t\t\t\t\tStudent name:"+student1Name+"\nStudent Id:"+student1Id+"\t\t\t\t\tStudent email:"+student1Email+"\nStudent score:"+student1Score+"\t\t\t\t\tStudent grade:"+student1Grade+"\n\n\n\t\t\t\t\t\tKing Saud University (KSU) - Riyadh\n\t\t\t\t\t\t          FALL 2015/2016\n\n\n\t\t\t\t\t      Academic Advisor:"+academicAdvisor2+"\n\nCourse Code:"+courseCode+"\t\t\t\t\tStudent name:"+student2Name+"\nStudent Id:"+student2Id+"\t\t\t\t\tStudent email:"+student2Email+"\nStudent score:"+student2Score+"\t\t\t\t\tStudent grade:"+student2Grade);
    53 
    54       } 
    55       }
    and here's the output

    http://s18.postimg.org/5e8d7hzl5/algn.jpg

    please help me =,(
    It's due tomorrow and I tried everything but I couldn't fix it.

  2. #2
    janedoe is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Default Re: alignment problem [ please help ]

    ^the last printing statement isn't showing above for somereason so here it is :


    //output:

    System.out.println("\t\t\t\t\t\tKing Saud University (KSU) - Riyadh\n\t\t\t\t\t\t FALL 2015/2016\n\n\n\t\t\t\t\t Academic Advisor:"+academicAdvisor1+"\n\nCourse Code:"+courseCode+"\t\t\t\t\tStudent name:"+student1Name+"\nStudent Id:"+student1Id+"\t\t\t\t\tStudent email:"+student1Email+"\nStudent score:"+student1Score+"\t\t\t\t\tStudent grade:"+student1Grade+"\n\n\n\t\t\t\t\t\tKing Saud University (KSU) - Riyadh\n\t\t\t\t\t\t FALL 2015/2016\n\n\n\t\t\t\t\t Academic Advisor:"+academicAdvisor2+"\n\nCourse Code:"+courseCode+"\t\t\t\t\tStudent name:"+student2Name+"\nStudent Id:"+student2Id+"\t\t\t\t\tStudent email:"+student2Email+"\nStudent score:"+student2Score+"\t\t\t\t\tStudent grade:"+student2Grade);

    }
    }

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: alignment problem [ please help ]

    Using variable length Strings with tabs can be tricky. If the String's length pushes the position past a tab position, the tabbing will go to the next tab position.
    Make sure none of the Strings being displayed are longer than the distance between tabs to prevent an overflow.

    It might be better to use something like the String class's format() method to position Strings instead of tabs.
    Last edited by Norm; 09-12-2015 at 02:38 PM.
    janedoe likes this.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    janedoe is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Default Re: alignment problem [ please help ]

    Quote Originally Posted by Norm View Post
    Using variable length Strings with tabs can be tricky. If the String's length pushes the position past a tab position, the tabbing will go to the next tab position. Make sure none of the Strings being displayed are longer than the distance between tabs to prevent an overflow.It might be better to use something like the String class's format() method to position Strings instead of tabs.
    I really appreciate your response but I didn't get it :(
    I highlighted what I didn't get ( I feel retarded xD )

    and how do I use the string class's format() ?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: alignment problem [ please help ]

    how do I use the string class's format()
    Read the API doc for the method and see also the API doc for the Formatter class

    Re using tabs and long Strings
    If the tab positions are 3,6,9 and the first String's length is 2 then the next tab position is at 3
    If the first String's length is 4, then the next tab position is 6 not 3
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. GridBagLayout alignment problem
    By adwart in forum AWT / Swing
    Replies: 2
    Last Post: 08-15-2011, 04:58 AM
  2. BoxLayout Alignment problem
    By whiteMath in forum AWT / Swing
    Replies: 12
    Last Post: 07-19-2010, 10:04 PM
  3. Spacing/Alignment problem with java 2D printing
    By myselfmayur in forum Java 2D
    Replies: 2
    Last Post: 06-25-2010, 03:37 PM
  4. GridLayout alignment problem
    By q8inq8 in forum New To Java
    Replies: 9
    Last Post: 03-16-2010, 07:54 PM
  5. [SOLVED] alignment problem
    By nanimtech in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-10-2008, 01:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •