Results 1 to 6 of 6
  1. #1
    guitar-maniac is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default Can i use Java Scanner with String?

    Hey!

    I couldn't get this code working:
    import java.util.*;

    public class scan
    {
    public static void main (String args[])
    {

    String testi;
    Scanner scan = new Scanner(System.in);

    System.out.println("Testing Scanner, write something: ");
    testi = scan.nextString();
    System.out.println(testi);
    }
    }

    Did i write something wrong or can't Scanner be used with String?

  2. #2
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    Theres nothing as nextString instead

    Java Code:
    import java.util.*;
    
    public class scan
    {
    public static void main (String args[])
    {
    
    String testi;
    Scanner scan = new Scanner(System.in);
    
    System.out.println("Testing Scanner, write something: ");
    testi = scan.nextString(); 
    System.out.println(testi);
    }
    }
    try this
    Java Code:
    Scanner scan = new Scanner(System.in);
    
    System.out.println("Testing Scanner, write something: ");
    testi = scan.nextLine();
    System.out.println(testi);

  3. #3
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    the command is nameofyourscanner.nextLine()

    maybe you can use an IDE for example netbeans or eclipse.
    The IDE suggest the commands with a short description of them.
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

  4. #4
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    I think the problem with the first line

    Java Code:
    import java.util.Scanner;

  5. #5
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    Quote Originally Posted by ŖΫ ỏ Ңόρę View Post
    I think the problem with the first line

    Java Code:
    import java.util.Scanner;
    no theres no problem

    when you type
    Java Code:
    import java.util.Scanner;
    it just specifies your importing from scanner class in the util package only.

    Java Code:
    import java.util.*;
    Its more general point of view.where you can import any class from util package

    So theres no error

  6. #6
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    Quote Originally Posted by [RaIdEn] View Post
    no theres no problem

    when you type
    Java Code:
    import java.util.Scanner;
    it just specifies your importing from scanner class in the util package only.

    Java Code:
    import java.util.*;
    Its more general point of view.where you can import any class from util package

    So theres no error
    Thank you very much for your explanation

    kind regards

Similar Threads

  1. Scanner, while loop and sorting arrays/string?
    By RSYR in forum New To Java
    Replies: 10
    Last Post: 04-20-2011, 06:13 PM
  2. String tokenizing with Scanner
    By vijaygk in forum Advanced Java
    Replies: 2
    Last Post: 07-15-2008, 04:44 AM
  3. java.util.Scanner not recognised
    By pjm35@st-and.ac.uk in forum New To Java
    Replies: 4
    Last Post: 06-07-2008, 03:32 PM
  4. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  5. The import java.util.Scanner cannot be resolved
    By Heather in forum Advanced Java
    Replies: 1
    Last Post: 07-08-2007, 01:05 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
  •