Results 1 to 9 of 9
  1. #1
    Galway is offline Member
    Join Date
    Sep 2016
    Posts
    15
    Rep Power
    0

    Default Trouble with Scanner (I think)

    It seems to fail in the populate Array() method as the scanner nextLine() method does not wait for input(I think)

    Java Code:
    import java.util.Scanner;
    public class ST {
    	static S[] studlist;
    	public static void main(String[] args)
    	{
    		studlist=createArray();
    		populateArray(studlist);
    	}
    	public static S[] createArray()
    	{
    		Scanner input=new Scanner(System.in);
    		System.out.print("Enter size of array: ");
    		int x;
    		x=input.nextInt();
    		input.nextLine();
    		studlist=new S[x];
    		input.close();
    		return studlist;
    	}
    	public static void populateArray(S[] array)
    	{
    		Scanner input=new Scanner(System.in);
    		
    		for(int i=0;i< array.length;i++)
    			
    		{
    			System.out.print("Name: ");
    		        String name=input.nextLine();
    			System.out.print("ID: ");
    			long ID=input.nextLong();
    			input.nextLine();
    			array[i]=new S(name,ID);	
    		}
    		input.close();
    	}
    }
    
    
    public class S {
    	private String name;
    	private long idNumber;
    	
    	public S(){
    		this("Not Given",0);
    	}
    	public S(String name,long idNumber){
    		this.name=name;
    		this.idNumber=idNumber;
    	}
    	//Getters+setters
    	@Override
    	public String toString(){
    		return name+ idNumber;
    	}
    
    }
    Last edited by Galway; 11-14-2016 at 08:20 PM.

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

    Default Re: Trouble with Scanner (I think)

    the scanner nextLine() method does not wait for input
    The Scanner object will save the line-end character from the last call to a next method and return it in the call to nextLine.
    Before calling nextLine to get the user's input, make a special call to nextLine to read and clear the line-end that was left in the Scanner's buffer.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Galway is offline Member
    Join Date
    Sep 2016
    Posts
    15
    Rep Power
    0

    Default Re: Trouble with Scanner (I think)

    Quote Originally Posted by Norm View Post
    The Scanner object will save the line-end character from the last call to a next method and return it in the call to nextLine.
    Before calling nextLine to get the user's input, make a special call to nextLine to read and clear the line-end that was left in the Scanner's buffer.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    The nextLine() method seems to fail on the first input to the scanner though. I already had a input.nextLine() at the end of each loop to clear the buffer.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Trouble with Scanner (I think)

    Don't close System.in. Once closed, it can't be used again in that program.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Galway is offline Member
    Join Date
    Sep 2016
    Posts
    15
    Rep Power
    0

    Default Re: Trouble with Scanner (I think)

    Quote Originally Posted by Norm View Post
    Don't close System.in. Once closed, it can't be used again in that program.
    Do you mean when I closed the scanner object on line 17? Wouldn't it just close by itself when it went outside the method anyway?

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

    Default Re: Trouble with Scanner (I think)

    What happened when you removed the calls to the close method?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Galway is offline Member
    Join Date
    Sep 2016
    Posts
    15
    Rep Power
    0

    Default Re: Trouble with Scanner (I think)

    Quote Originally Posted by Norm View Post
    What happened when you removed the calls to the close method?
    I can't check as I wrote and ran the code when I was on a computer at college.I don't have java set up on my home laptop so can't check. Thanks for the replies I will need to check if that works in college tomorrow.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Trouble with Scanner (I think)

    Ok, we'll wait until you are able to do it.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Galway is offline Member
    Join Date
    Sep 2016
    Posts
    15
    Rep Power
    0

    Default Re: Trouble with Scanner (I think)

    Quote Originally Posted by Norm View Post
    Ok, we'll wait until you are able to do it.
    Managed to install java on my home laptop (looked up a guide on youtube to set java environment variables)
    Yeah removing the close statement on line 17 solved it.
    I have spend around 4+ hours this past week trying to figure out why my code was not working(I originally had a lot more to the code but toned it down trying to solve what was going wrong). None of my college tutors for the course could see what was going wrong either(I showed 3 of them and all of them were stumped)

Similar Threads

  1. Scanner and if/else trouble
    By LasseA in forum New To Java
    Replies: 3
    Last Post: 11-12-2012, 11:37 AM
  2. Replies: 1
    Last Post: 09-20-2012, 09:54 AM
  3. Scanner/Locale Trouble
    By plm-pusik in forum New To Java
    Replies: 1
    Last Post: 10-15-2010, 07:07 PM
  4. Scanner Trouble
    By Snoman in forum New To Java
    Replies: 3
    Last Post: 09-28-2010, 05:29 AM
  5. Replies: 0
    Last Post: 12-01-2008, 09:50 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
  •