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

    Default scanner class doesn't print the user input.

    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
    here's the output:

    there is no coding errors but for some reason the student advisor's name doesn't get printed.
    + you can see a spaising problem in the output area so can someone please tell me how to fix this ?
    thanks in advance

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

    Default Re: scanner class doesn't print the user input.

    It has to do with the line terminator and next() vs nextLine(). If you are done with any next() input, *then* do a nextLine to clear the rest of the line.
    And then do a nextLine() to get the advisor. Presently, you are doing it the other way around and essentially ignoring your input.

    And yes, the print statement in the tile messes things up. The mods should take care of that for you.

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

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

    Default Re: scanner class doesn't print the user input.

    Mixing the Scanner class's nextLine() method with other next methods is a problem. The other next methods leave the lineend character in Scanner's buffer which is all that will be read when a nextLine() method is used after another type of next method. Solution: use an extra nextLine() to clear the buffer.
    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: scanner class doesn't print the user input.

    thank you guys !
    It worked
    there is still one problem
    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 ?

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

    Default Re: scanner class doesn't print the user input.

    The problem has to do with your tabs. Easiest might be to add more tabs. A better approach would be to use System.out.printf and provide field widths for your variables on output. Check out the System.out.printf and associated formt class for more information.

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

  6. #6
    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: scanner class doesn't print the user input.

    Quote Originally Posted by jim829 View Post
    The problem has to do with your tabs. Easiest might be to add more tabs. A better approach would be to use System.out.printf and provide field widths for your variables on output. Check out the System.out.printf and associated formt class for more information.
    Adding more tabs doesn't help: the number of tabs depend on the length of the field(s) before you print the tabs; formatting the output using System.printf( ... ) is indeed the way to go.

    kind regards,

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

Similar Threads

  1. How to involve user input not using a Scanner?
    By PixelatedIshy in forum New To Java
    Replies: 3
    Last Post: 12-12-2013, 08:43 PM
  2. Help with user input using the scanner class
    By Lach in forum New To Java
    Replies: 2
    Last Post: 10-09-2013, 04:35 PM
  3. Scanner Issues (User Input: Very Simple)
    By carlodelmundo in forum New To Java
    Replies: 8
    Last Post: 10-31-2008, 02:44 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
  •