Results 1 to 2 of 2
  1. #1
    erik9920 is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default nextInt() for numbers, nextLine() for text. What if i want both?

    Hey!

    I guess the title says it all. I want to receive license plate number for cars and string it. for example "ST54064"(2numbers+5letters).

    I have tried this:

    import java.util.Scanner;
    class Car{
    public static void main(String args[]){
    Scanner tastatur = new Scanner(System.in);

    System.out.print("write license plate number: ");
    String licenseplate = tastatur.nextLine();
    ...

    But if i try to run this, i get error if i type in any numbers. So my question is what command i can use instead of String kjennetegn = tastatur.nextline();?

    Thanks!
    Last edited by erik9920; 09-30-2011 at 12:13 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: nextInt() for numbers, nextLine() for text. What if i want both?

    Quote Originally Posted by erik9920 View Post
    I want to receive license plate number for cars and string it. for example "ST54064"(2numbers+5letters).
    That looks more like 2 letters and 5 numbers to me.

    I have tried this:

    import java.util.Scanner;
    class Bil{
    public static void main(String args[]){
    Scanner tastatur = new Scanner(System.in);

    System.out.print("Oppgi kjennetegn: ");
    String kjennetegn = tastatur.nextLine();
    ...

    But if i try to run this, i get error if i type in any numbers. So my question is what command i can use instead of String kjennetegn = tastatur.nextline();?

    Thanks!
    Can you tell us more about your precise requirements, show us more code and show us the exact error message as well as indicate which line causes it to occur.

Similar Threads

  1. Reading nextInt() then nextLine() form a file.
    By Mokomi in forum New To Java
    Replies: 9
    Last Post: 09-06-2011, 08:41 AM
  2. nextint() Method !
    By M7MD in forum Threads and Synchronization
    Replies: 6
    Last Post: 10-25-2010, 06:13 AM
  3. Very Bad problem with nextInt()
    By usiyalla in forum New To Java
    Replies: 2
    Last Post: 07-25-2010, 01:44 AM
  4. nextInt(unknown source)
    By babyburger in forum New To Java
    Replies: 3
    Last Post: 03-18-2010, 04:03 PM
  5. How to make a text field to accept numbers only?
    By Robert_85 in forum Advanced Java
    Replies: 15
    Last Post: 04-07-2009, 01:30 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
  •