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

Thread: Hi everyone i stuck at something and i need a little help please

  1. #1
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hi everyone i stuck at something and i need a little help please

    We started to learn classes and i created this method :
     public static Student[] getStudentArray(Student[] arr){
    		 Scanner s=new Scanner(System.in);
    		 String Name;float Avg;
     
    		 for (int i=0;i<arr.length;i++){
    			 System.out.println("name?");
    			 Name=s.nextLine();
     
    			 System.out.println("avg?");
    			 Avg=s.nextFloat();
    			 Student temp=new Student(Name,Avg); 
    			 arr[i]=temp;
    		 }
     
     
    		return arr; 
     
     
    	 }
    Student is my class.the problem is that the output looks like that:"console":
    How many students?
    2
    name?
    john
    avg?
    20
    name?
    avg?

    then as you can see it goes name?avg? and stuck.what have i done wrong?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,100
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Hi everyone i stuck at something and i need a little help please

    The problem is with the way the Scanner class buffers the keyboard input. The lineend is not removed by the nextFloat() method. The next nextLine() reads just that lineend.
    Solution: call nextLine() after nextFloat() to clear the lineend.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hi everyone i stuck at something and i need a little help please

    Quote Originally Posted by Norm View Post
    The problem is with the way the Scanner class buffers the keyboard input. The lineend is not removed by the nextFloat() method. The next nextLine() reads just that lineend.
    Solution: call nextLine() after nextFloat() to clear the lineend.
    Thanks but it still gives me trouble.i need to create an array that contains String(needed nextline) and integers.
    but the momonet i want to print my array the data conataine string is empty!
    something like this (from console):
    Please enter how many Employees:
    1
    Please enter Employee's #1 id:
    1
    Please enter Employee's #1 name:
    john
    Please enter Employee's #1 salary:
    1000
    Original array from user:
    1  1000
    As you can see the data of the string return empty!

    --- Update ---

    this what my array's method contain :
      Name=s.nextLine();
     
    		   temp.setname(Name);
    		   s.nextLine();
    		   System.out.println("Please enter Employee's #"+(i+1)+" salary:");
    		   Salary=s.nextInt();
    		   temp.setsalary(Salary);
     
    		   arr[i]=temp;

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,100
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Hi everyone i stuck at something and i need a little help please

    Does the code call nextLine() to clear the endline character left in the Scanner class's buffer by any of the next methods like nextInt()?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hi everyone i stuck at something and i need a little help please

    I need to create array and input value in this order:int id(integer),String name(must be nextline),int salary(integer).
    i fount that if i do s.nextline after the name=s.nextline it fix this.
    however when it comes to print my array it consider the String argument as a blanket area.
    example :"id:2333 salary:23"without Name(string)in the middle.

    could anyone know how can i solve this?:[

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,100
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Hi everyone i stuck at something and i need a little help please

    Does the code read the endline character out of Scanner's buffer by calling the nextLine() method after calls to the other next methods?
    Can you Post the complete code that shows when each of the Scanner's methods are called?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hi everyone i stuck at something and i need a little help please

    Quote Originally Posted by Norm View Post
    Does the code read the endline character out of Scanner's buffer by calling the nextLine() method after calls to the other next methods?
    Can you Post the complete code that shows when each of the Scanner's methods are called?

    This my code:
    Scanner s=new Scanner(System.in);
    	   int Id=0;String Name="";int Salary=0;
     
     
    	   for (int i=0;i<arr.length;i++){
     
    		   Employee temp=new Employee(Id,Name,Salary);
    		   System.out.println("Please enter Employee's #"+(i+1)+" id:");
    		   Id=s.nextInt();
    		   temp.setid(Id);
    		   System.out.println("Please enter Employee's #"+(i+1)+" name:");
    		   Name=s.nextLine();
    		   temp.setname(Name);
     
    		   System.out.println("Please enter Employee's #"+(i+1)+" salary:");
    		   Salary=s.nextInt();
    		   temp.setsalary(Salary);
     
    		   arr[i]=temp;
    	   }
    	   return arr;


    --- Update ---

    Quote Originally Posted by eyalfish View Post
    This my code:
    Scanner s=new Scanner(System.in);
    	   int Id=0;String Name="";int Salary=0;
     
     
    	   for (int i=0;i<arr.length;i++){
     
    		   Employee temp=new Employee(Id,Name,Salary);
    		   System.out.println("Please enter Employee's #"+(i+1)+" id:");
    		   Id=s.nextInt();
    		   temp.setid(Id);
    		   System.out.println("Please enter Employee's #"+(i+1)+" name:");
    		   Name=s.nextLine();
    		   temp.setname(Name);
     
    		   System.out.println("Please enter Employee's #"+(i+1)+" salary:");
    		   Salary=s.nextInt();
    		   temp.setsalary(Salary);
     
    		   arr[i]=temp;
    	   }
    	   return arr;
    P.s i did try to make s.nextline; after Name=s.nextline. however as i said when i wanted to print my array the console get blanket area when the string should output.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,100
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Hi everyone i stuck at something and i need a little help please

    There should be a call to nextLine() to clear the buffer here:
       Id=s.nextInt();     //<<<<<<<<<<< this leaves endline char in buffer
       temp.setid(Id);
       System.out.println("Please enter Employee's #"+(i+1)+" name:");
       Name=s.nextLine();   //  this reads the endline left by the above nextInt()

    Add call to nextLine() immediately after the call to nextInt()
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hi everyone i stuck at something and i need a little help please

    You are a king!!!!!!!!!!!!!!!
    thank you thank you:]

Similar Threads

  1. Help me please, im stuck!
    By warbie118 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 15th, 2011, 08:09 AM
  2. Please Help I'm stuck
    By mael331 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 6th, 2011, 09:02 AM
  3. Help! im stuck!
    By aznguy92 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 15th, 2011, 09:16 PM
  4. Im stuck, please help
    By bigsmoke101 in forum Loops & Control Statements
    Replies: 3
    Last Post: April 12th, 2011, 04:34 PM
  5. PLEASE PLEASE I AM STUCK...
    By ThejavaBUM in forum Loops & Control Statements
    Replies: 2
    Last Post: April 5th, 2011, 10:16 PM