Results 1 to 8 of 8
  1. #1
    Mythic Rare is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Need help getting the scanner class to accept strings

    Basically, I'm just trying to let the user enter the name. It crashes whenever the input isn't an int. Also, here's the bit of code I'm using that has to do with that. I don't feel like putting the entire class, so here's that bit of code:

    PHP Code:
    String yourName = user.nextLine();
    Anyway, I just haven't figured out how. Oh, also, the scanner is called user in this class.

  2. #2
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    161
    Rep Power
    0

    Default Re: Need help getting the scanner class to accept strings

    Here is what it should be.

    Java Code:
    Scanner scan = new Scanner ( System.in );
    System.out.println("Enter your name:");
    String yourName = scan.nextLine();
    I'd help you more, but I can't since you "don't feel like posting your entire class"

  3. #3
    Mythic Rare is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Re: Need help getting the scanner class to accept strings

    I already did all that. Look, here's the class:

    Java Code:
    import java.util.Scanner;
    
    public class whatName {
    
    	public static void main(String[] args) {
    		System.out.println("Hello there! What is your name?");
    		Scanner user = new Scanner(System.in);
    		
    		String yourName = user.nextLine();
    		//TODO Figure out how to have the scanner receive a string instead of just an int.
    		
    		System.out.println("Wow, really? That's my cousin's name! Well, anyway " + yourName + " I've gotta be at a meeting, so, bye!");
    		
    
    	}
    
    }
    It's pretty simple. I was just testing out the scanner.

  4. #4
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    161
    Rep Power
    0

    Default Re: Need help getting the scanner class to accept strings

    Oh! I seem to remember something about this error.

    I think Jos posted about this on some other post? Something about the scanner needing to be cleared or something? I've encountered this error myself.

    As far as I remember, adding a
    Java Code:
    user.nextLine();
    to line 8. I remember that working in my code - but I'm not quite sure why that error occurs.

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    262
    Rep Power
    2

    Default Re: Need help getting the scanner class to accept strings

    Quote Originally Posted by Mythic Rare View Post
    I already did all that. Look, here's the class:

    Java Code:
    import java.util.Scanner;
    
    public class whatName {
    
    	public static void main(String[] args) {
    		System.out.println("Hello there! What is your name?");
    		Scanner user = new Scanner(System.in);
    		
    		String yourName = user.nextLine();
    		//TODO Figure out how to have the scanner receive a string instead of just an int.
    		
    		System.out.println("Wow, really? That's my cousin's name! Well, anyway " + yourName + " I've gotta be at a meeting, so, bye!");
    		
    
    	}
    
    }
    It's pretty simple. I was just testing out the scanner.


    This runs fine. What is the problem?

  6. #6
    Mythic Rare is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Re: Need help getting the scanner class to accept strings

    Quote Originally Posted by superhaNds View Post
    This runs fine. What is the problem?
    That's weird. Last time I ran the program there was an error. I must have fixed it at some point in time by accident.

  7. #7
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    262
    Rep Power
    2

    Default Re: Need help getting the scanner class to accept strings

    Quote Originally Posted by Mythic Rare View Post
    That's weird. Last time I ran the program there was an error. I must have fixed it at some point in time by accident.
    "It crashes whenever the input isn't an int" you said. Therefore, I assume that before you were calling user.nextInt() which excepts an int. Anything else throws an exception and crashes the program (if you don't handle it).

  8. #8
    Mythic Rare is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Re: Need help getting the scanner class to accept strings

    Quote Originally Posted by superhaNds View Post
    "It crashes whenever the input isn't an int" you said. Therefore, I assume that before you were calling user.nextInt() which excepts an int. Anything else throws an exception and crashes the program (if you don't handle it).
    No, I was using nextLine. Still, everything but ints crashed it, and even when you entered one, it didn't say it as yourName (Because yourName was a string) therefore, I changed yourname to an int, and changed it to nextInt, then it worked for some reason after changing it back.

Similar Threads

  1. While loop won't accept value from another class
    By zlloyd1 in forum New To Java
    Replies: 2
    Last Post: 11-25-2012, 06:23 PM
  2. For loop with scanner and strings
    By jwl in forum New To Java
    Replies: 20
    Last Post: 09-15-2012, 07:05 PM
  3. Replies: 3
    Last Post: 03-23-2011, 11:16 AM
  4. Replies: 1
    Last Post: 09-24-2010, 07:08 PM
  5. Replies: 13
    Last Post: 05-16-2010, 12:09 AM

Tags for this Thread

Posting Permissions

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