Forum Stats

  • 3,837,901 Users
  • 2,262,306 Discussions
  • 7,900,429 Comments

Discussions

Scanner class

User_WL6FP
User_WL6FP Member Posts: 6 Red Ribbon
edited Oct 17, 2018 8:06PM in Java Programming

How do I read these inputs in Scanner class ?   Input :   12 15 18 // 3 integers input in one line

my doubt here is, should I use nextInt()  3  times Or should I use nextLine()  to read the entire line and tokenize to get these three integers?

rpc1

Answers

  • Unknown
    edited Oct 12, 2018 3:11PM
    How do I read these inputs in Scanner class ? Input : 12 15 18 // 3 integers input in one line my doubt here is, should I use nextInt() 3 times Or should I use nextLine() to read the entire line and tokenize to get these three integers?

    What you should do is read the trails in The Java Tutorials to learn how to use the Scanner class.

    https://docs.oracle.com/javase/tutorial/essential/io/scanning.html

    That tutorial has WORKING CODE you can try and work with to understand how to use the functionality.

    Load and debug the code in a GUI like NetBeans so you can step through it one line at a time and examine the variables.

    rpc1
  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Oct 12, 2018 11:26PM
    3640159 wrote:my doubt here is, should I use nextInt() 3 times Or should I use nextLine() to read the entire line and tokenize to get these three integers?

    I would go for the most specific available there and leave it to the API to handle things than re-inventing the wheel.

  • morgalr
    morgalr Member Posts: 457
    edited Oct 17, 2018 8:06PM

    read the line,

    use string split

    then you have the 3 you need.

    if you need to convert to integer, then do so.