6,471 questions
0
votes
0
answers
33
views
How to handle non-numeric input when using scanner.nextInt() in Java [duplicate]
I’m learning Java and testing basic input handling.
I wrote the following simple program:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner ...
1
vote
2
answers
96
views
Java - Deleting previous console inputs out of System.in - Why does 1 version of my Code work and not the other version?
The diffrence in the code is in the deleteInput()-Method. Everything else is the same.
The question is at the end, because it's about the console output.
The code:
public class DeletePreviousInputs {
...
0
votes
1
answer
60
views
How to enter double and string alternately in an infinite loop? [duplicate]
I'm trying to write a calculator In Java, but I don't understand how to provide infinite input of double and string in turn. It keeps throwing InputMismatchException, I don't know what to do.
public ...
1
vote
1
answer
71
views
Why is my console not immediately printing the previous line when using a java scanner?
I have the following simple Java program, which asks for a username (via the Scanner) and a password (via the Console). Note I am NOT running this in an IDE such as eclipse, so that bug does not apply ...
0
votes
1
answer
74
views
Scanner.readLine() with custom line delimeter
I have a string that needs to be read using the Scanner class from the beginning to the \n character. The problem is that the source stream in my case may contain the character \u2028. I know that the ...
-1
votes
1
answer
62
views
How to get Java console scan 2 strings separated by TAB key
I cannot type the TAB key into the console for 2 strings separated by the tab key, i used the split method but it didn't work. The Java console does not accept the TAB key.
Here's a snippet of my code
...
0
votes
1
answer
139
views
Java Scanner throws IndexOutOfBoundsException if an asynchronous thread is interruped while waiting on nextLine() and another nextLine() is called
What title says.
Java Scanner throws IndexOutOfBoundsException if an asynchronous thread is interrupted while waiting on nextLine() and another nextLine() is called.
Here is some code that reproduces ...
-1
votes
1
answer
241
views
How to populate an array automatic with an user` input?
i m writing a code as homework, the task is to create a code, where the user give a number (for example 4 ) and the output will be all the number from 4 ( the users input ) till the length of array (...
-1
votes
1
answer
213
views
Why isn't "\n" interpreted as newline character when a String like, "abc\nabc" is given as input via Scanner class in Java?
In Java, when we try to print a Hardcoded String like, "abc\nabc", we observe a line change after the first abc but when we take same input via sc.next() or sc.nextLine() it prints out '\n' ...
0
votes
1
answer
551
views
SIGHUP error handling, exception throws in Scanner class how can I fix this problem?
import java.util.*;
class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String name=sc.next();
System.out.println("No ...
0
votes
1
answer
68
views
Scanner.next() doesn't read the first character in every second line
The following inputs are supplied to this piece of code:
5
0 4 15
1 0 14 2 7 3 23
2 0 7
3 1 23 4 16
4 2 15 3 9
The problem is that whenever the line number is odd, the first integer will not be read ...
0
votes
2
answers
77
views
how to sort input from java scanner? [closed]
I am working on a simple project in Java which need to take in a string through a Scanner object and then sort out the inputs which can be integers.
These then need to be fed in pairs to run the ...
-2
votes
1
answer
116
views
Why does the scanner still run even though expression equates to false [duplicate]
I've come across an issue with my scanner functionality. Even after entering "exit" in the terminal, the scanner keeps running. My suspicion is that this behavior is linked to the ...
-1
votes
1
answer
65
views
My program recurses. Is there a way to fix this?
My problem is that whenever I try to catch the exception on "scannerGettingStarted", I always get the try catch exception from runProgram. I know that there is a recursion that is happening, ...
0
votes
0
answers
25
views
How to reiterate while loop containing scanner in order to restart code in java? [duplicate]
Basically the issue is that once the main method completes, it prompts to restart the code or not, except it terminates after prompting, no matter what I enter. How do I fix this?
import java.awt....
1
vote
0
answers
26
views
Why is Scanner.nextLine not running the second and third times? [duplicate]
I have a program that is supposed to give a user three chances to get a password before it locks them out, but the code is only correctly getting the password on the first attempt.
import java.util....
4
votes
1
answer
103
views
It gives error since I closed the scanner in main method but I created a new scanner in the method [duplicate]
i tried to creating a new scanner after i closed the former in the main. but it gives error.
i thought that i created a new scanner in the method than i closed it so it would read the new inputs ...
0
votes
2
answers
96
views
File not found when trying to pass user given file to new scanner object
public void readItemData()
{
try {
Frame frame = null;
FileDialog fileBox = new FileDialog(frame ,"Open", FileDialog.LOAD);
fileBox.setVisible(...
0
votes
2
answers
123
views
How to get a String and an int vs. just a String with Scanner?
I have some code that runs an addition or subtraction of two numbers based on a user's input, wherein they can either type a String followed by an integer to perform an operation, OR just a String to ...
0
votes
0
answers
24
views
Correct reading from console in the same line with request [duplicate]
I'm trying to write request, which user can see in console and in the same line he can give response, but I have some issues with that.
import java.util.Scanner;
public class Main {
final static ...
0
votes
0
answers
47
views
NoSuchElementException when using Java.Util.Scanner in console app
I am new to Java and I am doing hospital software in console app. I got a class to select options.
The exception is thrown only when option 2 is selected. I don't want the code to stop, but continuing ...
0
votes
1
answer
65
views
Java while loop not breaking when using HasNextLine()
I know that there's similar questions, but my while loop times out when using HasNextLine() and none of the solutions online have fixed it so far.
public static main(String [] args) {
Scanner s = ...
0
votes
0
answers
37
views
Why does hasNext() wait for input? [duplicate]
In this simple code, I'm checking if a Scanner has something in the input buffer or not. So I used hasNext() to check whether it has something or not. But instead of returning "false" (in ...
0
votes
2
answers
51
views
How do I add ">" where I want the user to input text? (Java)
If I have a System.out.println("what's your name"), I want the next line to say ">" so it makes it more obvious that it's the users turn to type something.
So far I've just put &...
1
vote
3
answers
145
views
Scanner delimter to only take new line outside of double quotes in java
I have requirement to read from the scanner based on new line only if its out side the double quotes.
Input :
"Content1 \r\n block" \r\n Contentn2 \r\n New Content " \r\n Conetent3&...