9 Replies - 109 Views - Last Post: 12 September 2012 - 01:24 PM Rate Topic: -----

#1 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Getting inputs from Scanner

Posted 12 September 2012 - 11:45 AM

If i have Scanner object that can take int inputs (like sc.nextInt()) separated by whitespaces. How do I get the value after each whitespace? And how do i limit the user inputs? For example, I only want the user to input 5 integers seperated by whitespaces and after reaching 5 inputs, pressing other keys except "Enter" wont do anything?
Is This A Good Question/Topic? 0
  • +

Replies To: Getting inputs from Scanner

#2 WannaJava?  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 27
  • Joined: 11-September 12

Re: Getting inputs from Scanner

Posted 12 September 2012 - 11:50 AM

There is a .split() method that can do that, there is also a String.replaceAll("") method that removes any character and replaces it with what is passed. There are already two other questions like this posted in the past 2 days. If you look through the two of them, you can find your answer in better detail.
Was This Post Helpful? 0
  • +
  • -

#3 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Getting inputs from Scanner

Posted 12 September 2012 - 11:52 AM

If you want all of the inputs on one line: e.g. user input is : 1 2 3 4 this can work:
String input = "1 2 3 4";
Scanner s = new Scanner(input).useDelimiter("\\s*");



Otherwise, if you prompt them 5 times, then you can just use the sc.nextInt() on each input
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2146
  • View blog
  • Posts: 8,920
  • Joined: 20-September 08

Re: Getting inputs from Scanner

Posted 12 September 2012 - 11:53 AM

Scanner ignores whitespace. It'll probably be more convenient to do nextLine().split()
Was This Post Helpful? 0
  • +
  • -

#5 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: Getting inputs from Scanner

Posted 12 September 2012 - 11:54 AM

Thanks but I've already done it before using String. Now i want to do it with int. Is it possible?

This post has been edited by HopelessDev: 12 September 2012 - 11:56 AM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,309
  • Joined: 06-March 08

Re: Getting inputs from Scanner

Posted 12 September 2012 - 11:57 AM

You seem to complicate your life for noting.

View PostHopelessDev, on 12 September 2012 - 02:45 PM, said:

I only want the user to input 5 integers seperated by whitespaces and after reaching 5 inputs, pressing other keys except "Enter" wont do anything?

Just read using scan.nextInt() 5 times then scan.nextLine() and ignore what it returns
Was This Post Helpful? 0
  • +
  • -

#7 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: Getting inputs from Scanner

Posted 12 September 2012 - 12:00 PM

I know but that is how the problem I found is. A line containing a space separated list of N integers.
Was This Post Helpful? 0
  • +
  • -

#8 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Getting inputs from Scanner

Posted 12 September 2012 - 12:00 PM

View PostHopelessDev, on 12 September 2012 - 02:54 PM, said:

Thanks but I've already done it before using String. Now i want to do it with int. Is it possible?

All of the input coming in through scanner will be strings. nextInt will attempt to read the next token in, and convert it to an int for you.
int num1 = sc.nextInt();


Was This Post Helpful? 0
  • +
  • -

#9 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: Getting inputs from Scanner

Posted 12 September 2012 - 12:10 PM

Okay thanks. I will just settle to using Strings. I just wondered if its doable using nextInt(). Thanks for the help.

I forgot to ask something. Whats the best way to not make the user input a number more than N times? Is if statement good enough?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,309
  • Joined: 06-March 08

Re: Getting inputs from Scanner

Posted 12 September 2012 - 01:24 PM

Store the numbers in an array then scan the array with each of its element to see if it appears at more than one place (its slot)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1