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?
Getting inputs from Scanner
Page 1 of 19 Replies - 106 Views - Last Post: 12 September 2012 - 01:24 PM
Replies To: Getting inputs from Scanner
#2
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.
#3
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:
Otherwise, if you prompt them 5 times, then you can just use the sc.nextInt() on each input
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
#4
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()
#5
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
#6
Re: Getting inputs from Scanner
Posted 12 September 2012 - 11:57 AM
You seem to complicate your life for noting.
Just read using scan.nextInt() 5 times then scan.nextLine() and ignore what it returns
HopelessDev, 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
#7
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.
#8
Re: Getting inputs from Scanner
Posted 12 September 2012 - 12:00 PM
HopelessDev, 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();
#9
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?
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?
#10
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)
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|