Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By jim829

Thread: alignment using tabs "\t"

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

    Exclamation alignment using tabs "\t"

    Hi there,

    as you can see ... in the output pic above
    the right column isn't aligned correctly
    the" number of levels to graduate " and everything below it doesn't follow the alignment
    why does that happen ? and how can I fix it ?
    this is a school assignment and the teacher told us that we should stick to using "\t"
    we can't use any other way to align
    and here's the code :
    Java Code:
     
    
             //Import the needed library:(I imported the library "java.util.*" in order to use the class scanner)
       
       import java.util.*;
       public class RegestrationSystem2    { //Class starts
       
             //Instantiation:(for the object key)
             
       static Scanner key = new Scanner (System.in);
       public static void main (String args[])
       { //Main Starts
         
             //Declaration and Input Section: 
             
          // Domain: 
        
       String domain = ("@ksu.edu.sa");
          
    
       //Declaration section:
       
       String student1First,student2First,student1Last,student2Last,courseCode1,cc2Upper,level1,localPart1,localPart2,domainUp1;
       String academicAdvisor1,academicAdvisor2,student1Full,student2Full,courseCode2,cc1Upper,level2,email1,email2,domainUp2;
       int student1Id,student2Id,remaining1,remaining2;
       char student1Grade,student2Grade;
       double student1Score,student2Score;
       boolean validation1,validation2,freshman1,sophomore1,junior1,senior1;
       boolean freshman2,sophomore2,junior2,senior2;
       
         //First Student:
       System.out.println("Enter student1's course code");
             courseCode1 = key.nextLine();
       System.out.println("Enter student1's ID");
             student1Id = key.nextInt();
       key.nextLine(); 
       System.out.println("Enter student1's first name");
             student1First = key.nextLine();
       System.out.println("Enter student1's last name");
             student1Last = key.nextLine();
       System.out.println("Enter student1's score");
             student1Score = key.nextDouble();
       System.out.println("Enter student1's grade");
             student1Grade = key.next().charAt(0);
       System.out.println("Enter the name of student1's Academic Advisor");
             academicAdvisor1 = key.nextLine();
          
         //Second Student:
       key.nextLine();       
       System.out.println("Enter student2's course code");
             courseCode2 = key.nextLine();
       System.out.println("Enter student2's ID");
             student2Id = key.nextInt();
       key.nextLine(); 
       System.out.println("Enter student2's first name");
             student2First = key.nextLine();
       System.out.println("Enter student2's last name");
             student2Last = key.nextLine();
       System.out.println("Enter student2's score");
             student2Score = key.nextDouble();
       System.out.println("Enter student2's grade");
             student2Grade = key.next().charAt(0);
       System.out.println("Enter the name of student2's Academic Advisor");
             academicAdvisor2 = key.nextLine();
       key.nextLine(); 
                //Processing Section:
                
       student1Full = student1First.concat(" "+student1Last);
       student2Full = student2First.concat(" "+student2Last);
          
       email1 = student1Id+domain;
          localPart1 = email1.substring(0,9);
             domainUp1 = email1.substring(10).toUpperCase();
       email2 = student2Id+domain;
          localPart2 = email2.substring(0,9);
              domainUp2 = email1.substring(10).toUpperCase();
    
       validation1 = Integer.parseInt(localPart1) == student1Id;
       validation2 = Integer.parseInt(localPart2) == student2Id;
       
       cc1Upper = courseCode1.toUpperCase();
       cc2Upper = courseCode2.toUpperCase();
       
       level1 = courseCode1.substring(5);
       remaining1 = 10-(Integer.parseInt(level1));
       level2 = courseCode2.substring(5);
       remaining2 = 10-(Integer.parseInt(level2));
       
       freshman1 = Integer.parseInt(level1) == 1 || Integer.parseInt(level1) == 2;
       sophomore1 = Integer.parseInt(level1)>= 3 && Integer.parseInt(level1) <= 5;
       junior1 = Integer.parseInt(level1)>= 6 && Integer.parseInt(level1) <= 8;
       senior1 =  Integer.parseInt(level1) == 9 || Integer.parseInt(level1) == 10;
       
       freshman2 = Integer.parseInt(level2) == 1 || Integer.parseInt(level2) == 2;
       sophomore2 = Integer.parseInt(level2)>= 3 && Integer.parseInt(level2) <= 5;
       junior2 = Integer.parseInt(level2)>= 6 && Integer.parseInt(level2) <= 8;
       senior2 =  Integer.parseInt(level2) == 9 || Integer.parseInt(level2) == 10;
       
                   //output(program output display):
                
          //First Student:
          
             System.out.println(" \t \t \t \t \t \t  King Saud University (KSU) - Riyadh");
             System.out.println(" \t  \t  \t  \t  \t  \t  \t    FALL 2015/2016");
             System.out.println("\t\t\t\t\t\t       First Student Information\n");
             System.out.println("Course Code:"+cc1Upper+"\t \t \t \t \t \t \t \tAcademic Advisor:\""+academicAdvisor1+"\"");
             System.out.println("Student ID:"+student1Id+"\t \t \t \t \t \t \t \tStudent Name:"+student1Full);
             System.out.printf("student Score: %.1f",student1Score);
             System.out.println("\t \t \t \t \t \t \t \tStudent Grade:"+student1Grade);
             System.out.println("Valid Email:"+validation1+"\t \t \t \t \t \t \t \tStudent email:"+localPart1+"@"+domainUp1);  
             System.out.println("Student Level:"+level1+"\t \t \t \t \t \t \t \tNumber of Levels to Graduate:"+remaining1);
             System.out.println("Freshman:"+freshman1+"\t \t \t \t \t \t \t \tSophomore:"+sophomore1);
             System.out.println("Junior:"+junior1+"\t \t \t \t \t \t \t \tSenior:"+senior1+"\n\n\n");
         
          //Second Student:  
                           
             System.out.println(" \t \t \t \t \t \t  King Saud University (KSU) - Riyadh");
             System.out.println(" \t  \t  \t  \t  \t  \t  \t    FALL 2015/2016");
             System.out.println("\t\t\t\t\t\t      Second Student Information\n");
             System.out.println("Course Code:"+cc2Upper+"\t \t \t \t \t \t \t \tAcademic Advisor:\""+academicAdvisor2+"\"");
             System.out.println("Student ID:"+student2Id+"\t \t \t \t \t \t \t \tStudent Name:"+student2Full);
             System.out.printf("student Score: %.1f",student1Score);
             System.out.println("\t \t \t \t \t \t \t \tStudent Grade:"+student1Grade);
             System.out.println("Valid Email:"+validation2+"\t \t \t \t \t \t \t \tStudent email:"+localPart2+"@"+domainUp2);  
             System.out.println("Student Level:"+level2+"\t \t \t \t \t \t \t \tNumber of Levels to Graduate:"+remaining2);
             System.out.println("Freshman:"+freshman2+"\t \t \t \t \t \t \t \tSophomore:"+sophomore1);
             System.out.println("Junior:"+junior2+"\t \t \t \t \t \t \t \tSenior:"+senior2);
             
          }//End of main 
          }//End of class
    thanks in advance

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

    Default Re: alignment using tabs "\t"

    tabs work IF there are no Strings that are longer than the space between the tabs.
    If the end of a String goes past a tab spot, the text displaying software will move the next String to the next tab location skipping over the one spanned by the String.

    Where do you set the tab stop locations? Can you change the tab locations so no Strings are longer than the space between two tab locations?

    Otherwise use paper and pencil and draw columns where you want the text shown. Then have a method that generates the correct number of tabs to follow the end of each String to position it to the next tab location.
    For example if the tabs are at 3,6,9 and you want the next String to be a column 9
    if the current String length is 2 then add 3 tabs: to columns 3,6,9
    if the current String length is 3 then add 2 tabs: to columns 6,9
    if the current String length is 6 then add 1 tab: to column 9
    Last edited by Norm; 09-29-2015 at 11:22 PM.
    janedoe likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: alignment using tabs "\t"

    This question was already answered here. Why are you asking again?

    http://www.java-forums.org/new-java/...ser-input.html

    Regards
    Jim
    janedoe likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: alignment using tabs "\t"

    It's not exactly the same question + I get logged out whenever I try to reply to that thread!

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

    Default Re: alignment using tabs "\t"

    sorry guys
    but can someone tell me the right way to use "printf" for spacing ?
    I learned the general way at school
    but I couldn't apply it to the code above because it looks a bit different
    how can I use printf to align the code above ?
    thanks in advance

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

    Default Re: alignment using tabs "\t"

    Start by defining where you want each column of text to print. Then use a formatting String that spaces the Strings as per your desired columns.
    For example if the columns are to be at 10 and 25, set the width of the first format to 10 and the second width to 15.

    See the API doc for the Formatter class for how to write format Strings.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: alignment using tabs "\t"

    Quote Originally Posted by janedoe View Post
    sorry guys
    but can someone tell me the right way to use "printf" for spacing ?
    I learned the general way at school
    but I couldn't apply it to the code above because it looks a bit different
    how can I use printf to align the code above ?
    thanks in advance
    But your assignment (your teacher) stated that you should only use tabs; you have to keep track in what column the output is and figure out if you should print another tab; but then again, you should know that tab skip size of your output device ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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