I am trying to use a user-entered number from the scanner in my code in multiple places. Below the line should be the same user-entered number as the first. Is this possible?
Scanner inputHere = new Scanner(system.in);
System.out.println("Please select me");
String inputHereOne = inputHere.nextLine();
System.out.println("----------------------------------");
Yes, it's possible. Use the value stored in inputHereOne. No need to call inputHere.nextLine() until you actually need the next line of input. Like so:
System.out.println(inputHereOne);
System.out.println(inputHereOne);
This will print inputHereOne, twice.
Related
I am trying to use a string splitter to display user input e.g. 1,2 coordinates to display on a console. I don't get any errors when I run my code. However, my attempt to use the splitter does not seem to work.
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a row and column number at which to shoot (e.g., 2,3): ");
String[] coordinates = scanner.nextLine().split(",");
if (coordinates.length != 2) {
System.out.println("Please enter coordinates in the correct format.");
System.out.println("\nPlayer 1 Please take your turn:");
continue;
}
System.out.println("\nEnter Mine location:");
System.out.println("\nPlease Enter x position for your Mine:");
System.in.read(byt);
str = new String(byt);
row = Integer.parseInt(str.trim());
System.out.println("\nPlease Enter y position for your Mine:");
System.in.read(byt);
str = new String(byt);
col = Integer.parseInt(str.trim());
Your use of System.in.read(...) is dangerous code and is not doing what you think it's doing:
System.in.read(byt); // *****
str = new String(byt);
row = Integer.parseInt(str.trim());
Instead use a Scanner, something that you already have, and either call getNextInt() on the Scanner, or get the line and parse it.
Also, you never use the Strings held in the coordinates array -- why get the Strings if you are ignoring them?
You ask about:
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a row and column number at which to shoot (e.g., 2,3): ");
str = scanner.nextInt().split(",");
but then see that the compiler won't allow this since you're trying to call a method on the int primitive that scanner.nextInt() returns.
My recommendation to use Scanner#nextInt() was as a replacement for you misuse of System.in.read(...). If instead you want the user to enter two numbers on one line, separated by a comma, then you're best bet is to use String.split(","), although, I think it might be better to use String.split("\\s*,\\s*") to get rid of any white space such as spaces hanging about. This way the split should work for 1,1 as well as 1, 2 and 1 , 2, and then you can parse the items held in the array via Integer.parseInt(...).
I want to write a code that will allow the user to pick how many scanners he wants to use. First I created a simple scanner and assigned an int to it
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
now the user will enter ANY integer (ex. 7). Then I want the program to create an array of scanners that will then allow a number of lines of input (in this case 7). Any help is appreciated!
To create a specific number of objects and store them somewhere you can easily use arrays:
Scanner[] scanners = new Scanner[num_of_scanners];
At this point you will have an array of null scanner objects. To declare them properly you have to use a loop like this:
for (int i = 0; i < scanners.length; i++)
{
scanners[i] = new Scanner(System.in);
}
Now you succesfully initialized all the scanners. To get your scanner at certain index see the example below:
Scanner first_scanner = scanners[0];
More on arrays here.
I have a program that needs to read lines of input. It needs to be many lines at once. For example:
As I enter my time machine or
maybe not,
I wonder whether free will exists?
I wonder whether free will exists
maybe not
as I enter my time machine or.
That all gets entered at one time by the user. I was trying to use .hasNextLine() method from Scanner class, but it is not returning false.... it waits for input again. Ive been looking around for a solution and it appears that .hasNextLine() waits for input, but i do not know what alternative to use. Any suggestions? The actual code looks like:
while(input.hasNextLine());
{
line += input.nextLine();
}
Thanks for your help
Perhaps you should use some sort of "stop" sequence meaning when the user enters a particular character sequence, it will break out the loop. It might look something like:
public static void main(String args[]){
final String stopSequence = "/stop";
final Scanner reader = new Scanner(System.in);
String input = reader.nextLine();
while(!input.equalsIgnoreCase(stopSequence)){
//process input
input = reader.nextLine();
}
}
I know Scanner scanner = new Scanner(System.in); can take user input but can I ask how to use it, whenever I put them, the netbeans just run through it without asking any user input in console, I just want to ask how to configure for using using user input?
of course, i put nextLine and nextInt, i just want to ask how to make netBeans stop and ask me the input.
You will see something called output (window) in bottom --> There you will see prompt for input.
If output window is not available there, goto Window menu--> Select output--> ouput
(or) Ctrl+4
because you need to give a message before like this.
// this is your input object
Scanner input = new Scanner(System.in);
// give massage
System.out.println("give some in put");
// to get input where you want
String input = input.nextline();
system.out.println(""+input);
It is quite simple. Here is an example for you.
public class Test {
public static void main(String[] args) [
Scanner INPUT = new Scanner(System.in);
int i;
System.out.print("Enter number: "); // This displays to the user to enter a number.
i = INPUT.nextInt(); // Sets i equal to the user input.
}
}
If you are using a string for input try INPUT.nextLine();.
You can find more info on it here: http://download.oracle.com/javase/1,5,0/docs/api/java/util/Scanner.html
I encounter some problem when using useDelimiter from the Scanner class.
Scanner sc = new Scanner(System.in).useDelimiter("-");
while(sc.hasNext())
{
System.out.println(sc.next());
}
if I have this input
A-B-C
the output will be
A B
and wait until I type in another "-" for it to print out the last character
However if I instead of having user input data, and insert a String to the Scanner instead the code will work. What's the reason for it, and how do I fix it? I don't want to use StringTokenzier
If the Scanner didn't wait for you to enter another - then it would erroneously assume that you were done typing input.
What I mean is, the Scanner must wait for you to enter a - because it has no way to know the length of the next input.
So, if a user wanted to type A-B-CDE and you stopped to take a sip of coffee at C, it woud not get the correct input. (You expect [ A, B, CDE ] but it would get [ A, B, C ])
When you pass it in a full String, Scanner knows where the end of the input is, and doesn't need to wait for another delimiter.
How I would do it follows:
Scanner stdin = new Scanner(System.in);
String input = stdin.nextLine();
String[] splitInput = input.split("-", -1);
You will now have an array of Strings that contain the data between all of the -s.
Here is a link to the String.split() documentation for your reading pleasure.
You could use an alternative delimiter string useDelimiter( "-|\n" );
It works with a String argument as well as by reading from System.in.
In case of System.in this requires you to press enter at the end of the line.
How I would do it follows:
Scanner stdin = new Scanner(System.in);
String s = stdin.nextLine();
String[] splitInput = s.split("-", -1);
You will now have an array of Strings that contain the data between all of the -s.