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

Thread: Basic scanner question

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Basic scanner question

    Hi all, this is my first post here.

    I'm trying to parse an int and a String into a method using the Scanner and I'm not sure if this is possible? This is what the simple code looks like:

     import java.util.Scanner;
    class Actions {
     
    	public void move(String direction, int movement) {
     
    		System.out.println("Moving " + movement + " metres");
    	}
     
     
    }
     
    public class Robot {
     
    public static void main(String[] args) { 
     
    	Actions robot1 = new Actions();
    	Scanner scan = new Scanner(System.in);
     
    	robot1.move(scan.nextLine());
    	System.out.println("Enter direction and distance to move");
     
        }
     
    }

    As you can see I can get user input using scan.nextLine() or nextInt - but is it possible to parse both at the same time? Hope this makes sense.

    Thanks for any replies.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Basic scanner question

    is it possible to parse both at the same time?
    No. One or the other.
    The user's input will be either a number that nextInt() can read or any input can be read by nextLine()

    Can you explain what the program wants the user to enter and in what order it expects that input from the user?

    BTW The program should show the message to the user BEFORE the user is expected to enter any input.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Basic scanner question

    I lazily read your question, so I admit I don't completely understand it, but I wanted to add that you CAN use the SAME Scanner object with both methods, nextInt() and nextLine(). However, you lost me with "parse both at the same time." I'm not sure what that means. Either try it or give an example of what that means.

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Basic scanner question

    Thanks for the replies - I appreciate that my first post wasn't that clear. I'm trying to get the user to enter a distance (int) and a direction (String) for the "robot" to move in. So user input would look like this:

    Attachment 3186

    So the distance(int) would be 10 and the robot would move west. I'm trying to ascertain if its possible to scan both an int and a string in the same line of user input - rather than like this:
     System.out.println("Enter distance:");
    	robot1.move(scan.nextInt());
     
    	System.out.println("Enter direction the robot should move:")
    	robot1.direction(scan.nextLine());
    ^^^ As using the above approach I would need to use two methods rather than one.

    Hope that makes sense!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Basic scanner question

    if its possible to scan both an int and a string in the same line of user input
    The user's input is not sent to the program until the user presses Enter. It is possible for more than one value to be entered before pressing Enter. With your program, enter these two items on one line:
    10 west
    and then press Enter and see what happens

    I would need to use two methods rather than one.
    No necessarily. You can pass more than one item to a method. The code could read the distance and the direction into two variables and pass them in one call to the method that is shown in post#1.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Atlantic2012 (September 21st, 2014)

  7. #6
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Basic scanner question

    Got it.. thanks!

  8. #7

    Default Re: Basic scanner question

    Try this to parse int into string :

    try{
    num2 = Integer.parseInt(num1);
    }
    catch(Exception ex) {
    System.out.println("Something went wrong, the string could not be converted to int.");
    }
    the try-catch is pretty important because the string could contain characters that cannot convert to int, of you need to catch it better than this, but keep it in mind

  9. #8
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: Basic scanner question

    Good question. But I only saw numbers on my library card when they use a bar scanner. Perhaps method nextLine() or hasNext() only reads the next item and not to parse int strings and integers together. Or can it be written to read both objects(data streams) or data(byte streams)?
    Last edited by planetHollywood; January 30th, 2018 at 06:39 PM.

Similar Threads

  1. Rather Basic Question
    By ashl7 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: March 6th, 2013, 03:20 AM
  2. error with "Scanner" class in basic calculator program.
    By wheezebeez in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 2nd, 2012, 09:49 AM
  3. Basic Question Need Help
    By Graser in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 21st, 2012, 10:27 AM
  4. [SOLVED] Question About Using Scanner Object Twice
    By derekeverett in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: July 16th, 2011, 10:36 AM
  5. Basic Java File I/O with Scanner Class Problem
    By miss confused in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 26th, 2010, 08:04 AM

Tags for this Thread