All Questions
Tagged with java.util.scanner while-loop
285 questions
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....
0
votes
0
answers
42
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
63
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
14
views
What can I do differently to get my loop function to work? [duplicate]
A user inputs a website which only stops when 'stop' is typed in the input then the total of websites are totaled together.
I'm just wondering what I'm doing wrong with my code.
public class Project60
...
0
votes
2
answers
217
views
Java: Trying to use a while loop to check scanner input for an int variable, not sure how to initialize the variable?
This is one of the first programs I've written on my own. I'm writing a program to tell you what your zodiac sign is, and I got the program to run with a scanner method that I imported, but the ...
0
votes
1
answer
51
views
For loop terminates earlier than expected when iterating over a text file
I've been reading this book, Algorithms by Sedgewick and Wayne, and this is one of the exercises given in chapter 1:
1.3.4 Write a stack client Parentheses that reads in a text stream from standard ...
-2
votes
1
answer
72
views
I asked a question on here earlier and am not sure what the answerer meant? [duplicate]
I want each line to split off after 20 characters have gone by, but I want it to split off at the nearest space so the sentence has only whole words.
The answerer said to do this:
str="some long ...
0
votes
2
answers
136
views
Scanner outside Java while loop
I'm creating a method that makes the user choose what he wants to do through 2 choices associated with numbers. In case the user inserts any string in input, my code prints infinite:
Choose an ...
-1
votes
2
answers
27
views
How to check if value is entered, and if nothing is entered reprompt them
I want to use a while loop to check if the user entered their guess and if nothing was entered reprompt them to enter it. I am new to Java sorry if this is really obvious.
System.out....
0
votes
0
answers
122
views
Program freezes during run at While loop
The program I am writing needs to read 4 lines of data from a text file containing an ID, Name, Level, and Salary for an employee. It then needs to create a formatted email address and print all of ...
0
votes
1
answer
164
views
trying to save multiple inputs from the same scanner in a while loop
I'm trying to get multiple integer values from the same scanner inside a while loop. I need to store all the values so I can use them later. However I can only get the last value from the scanner when ...
0
votes
2
answers
474
views
How to set up a loop condition that ends when a particular string input is entered
This is the input part of my problem.
Write a program that will continuously allow the user to enter the following:
*Temperature at 3 different instances
Unit of Temperature for the 3 inputs (Celsius, ...
-1
votes
4
answers
761
views
How do I stop if statement after the first true return value?
This code should print out a reply based on user input, user is given 3 tries to enter input after invalid tries. It works when they return all invalid values, but code still asks for user input after ...
0
votes
2
answers
77
views
While loops continues until specific user input is triggered
This program takes a String followed by an integer as input and then outputs a sentence using the inputs. The program repeats until the string "quit" is typed. It appears the problem is with ...
1
vote
2
answers
72
views
How to run scanner in a while loop
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("1 - login");
System.out.println("2 - regrister");
int ...
0
votes
1
answer
83
views
Switch cases are not getting executed properly
case 1 and default cases are not working properly print statement is not getting executed. I'm unable to identify the error. I don't know if there is problem with the scanner implementation it seems ...
-1
votes
2
answers
443
views
i want to display every number entered in my while loop.so how can i do this
must create a java application that will determine and display sum of numbers as entered by the user.The summation must take place so long the user wants to.when program ends the summation must be ...
0
votes
3
answers
126
views
Java Scanner ask for input in while loop
So I have this program:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String name = "&...
1
vote
1
answer
877
views
Java: How to use a Scanner to check that input is an integer and is within a specified range
I'm having trouble with my code.
What it should do:
Check if Scanner "myScanner" is an Integer
If it is an Integer, if it is between 1 and 200 (both included)
Problem:
I need to input an ...
0
votes
2
answers
132
views
Simple Password Check
Just beginning to learn java and I am trying to code a simple password check that gives you tries if you type the incorrect password. The problem is when I type the incorrect password and followed by ...
0
votes
4
answers
522
views
Nested while loops with scanner ints & sentinel values
So I've been a little stuck with this for a bit now.
I have a program where I'm trying to run two loops, the first loop I'm running will try something until I enter a "ending" character ...
0
votes
1
answer
948
views
How to terminate a Scanner while loop that takes input seperated by both whitespace and linebreaks
I'm trying to terminate this Scanner while loop when there is no more input for it to read. The input is two numbers per line separated by a space. The next line contains the same, and so on, for an ...
2
votes
1
answer
74
views
Skipping a line when it doesn't have the correct name with the scanner class
public void populateFromFile(String filename)
{
try
{
Scanner sc = new Scanner(new File(filename));
//variables
String firstName;
String lastName;
...
-2
votes
1
answer
225
views
While loop not incrementing java [closed]
i'm just starting to learn about java. My while loop does not seem to increment. Here's the snippet of my while loop inside a try and catch:
File file = new File("Reservation.txt");
Scanner ...
-1
votes
1
answer
361
views
How can I find x0 in Newton's method
Newton method
x1 = x0 - (f(x0)/f'(x0))
x2 = x1 - (f(x1)/f'(x1))
.
.
.
xn = xn-1 - (f(xn-1)/f'(xn-1))
Here x0 shows initial root prediction. f'(x) represents the derivative of the f(x) function.
The ...
2
votes
2
answers
70
views
NoSuchElementException infinite loop vs. pmd DD-anomaly
I'm kinda new to programming and very new to gradle and the pmd-plugin, so please have mercy.
If the user enters a number, scanner.nextLine() will throw a NoSuchElementException in every iteration ...
0
votes
3
answers
1k
views
How to find max number and occurrences
So I'm learn java for the first time and can't seem to figure how to set up a while loop properly .
my assignment is Write a program that reads integers, finds the largest of them, and counts its ...
0
votes
1
answer
59
views
While loop skippping my Scanners after 1 input [duplicate]
Hey there I was trying to make a phone contact project and I started to create a addContact() void that takes 2 inputs name and phone number in an infinite while loop. My problem is it doesn't take ...
0
votes
1
answer
121
views
Looping a .next() in a while() loop gives NoSuchElementException in one compiler but not on another
I am a novice at coding but cannot understand why it runs fine on my machine, but when I upload my code I get a "NoSuchElementException" on line 19, "String command = keyboar.next();&...
0
votes
1
answer
244
views
Why is my validation function not working (to validate if user input is an integer within range)
I'm trying to make a validation function, but as I tried to make the code "neater" by putting everything inside the function, the code stops working.
This is my original code that works :
...
-1
votes
3
answers
726
views
Java scanner input loop throwing No Such Element exception after first loop
I want a program that repeatedly prompts the user to input a number and then prints whether that number is prime or not. This works once, then on the next iteration it gives a No Such Element ...
1
vote
2
answers
136
views
What does .next(); do in this case?
This is the code which expects the integer input. If the input is integer the loop ends else the input is asked again. But if I do not include sc.next(); it will go into infinite loop when non integer ...
1
vote
5
answers
1k
views
How can I make the user just enter numbers and try again in Java?
The program is supposed to work as follows, the program asks for a number and the user must enter it, in case the user enters a letter, a number less than 0 or does not enter anything, an error will ...
0
votes
1
answer
172
views
How do I use a string array and scanner in a while loop?
I was told to turn a functional auto for loop that goes through all the rooms in the house into a while loop that allows the user to type what room they go into next.
The issue is you need to use the ...
-1
votes
1
answer
136
views
Difficulties in Understating for loop
I Don't Understand what should I do ? The Question is
Read from the user the number of cities (minimum 6 cities)
For each city: city name and the number of persons (minimum 10 persons) are entered.
...
1
vote
3
answers
181
views
Java loop scanner input until enter or "e" without .split
public class test {
private static Scanner userpress = new Scanner(System.in);
public static void main(String[] args) {
int r = 0;
int h = 0;
System.out.println("-------...
0
votes
3
answers
182
views
Scanner function in while loop's statement causes runtime error
Through using the debugger I've managed to find the problem but I don't know how to fix it.
import java.util.*;
public class CreateString {
public static void main(String[] args) {
...
0
votes
2
answers
85
views
Program does not run continuously
I'm writing a ChatBot program where, if the ChatBot doesn't have information to answer a question by the user, it asks the user how to answer the question, and then stores it in a txt file. Later, ...
0
votes
2
answers
77
views
My objects in my array are going to null (java)
I have this while loop that is pulling information from a file and making new objects and then placing the objects into an array and for some reason the while loop iterates once for each if statement ...
0
votes
2
answers
275
views
While loop only runs through the body once
My While loop is only executing once and it appears it moves back to the top to run through the loop again but only completes the System.out.println("Title"); and then moves on to execute ...
0
votes
1
answer
793
views
Stop Scanner while reading Integers or String
I'm trying to read a String and then Integers or Strings using Scanner:
public class Main {
public static void main (String[] args){
String[] StringList;
Integer[] IntegerList;
...
0
votes
1
answer
31
views
Multiple Scanner Inputs: Iterative validation of current selection blocks
I am attempting to validate multiple scanner inputs with paired If-Else blocks in a single while loop. The behavior that I am interested in achieving is to the current validation If-Statement request ...
-1
votes
2
answers
84
views
Java doesnt recognize scanner input [duplicate]
import java.util.Scanner;
public class SimCompanies {
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
boolean x=true;
String ...
0
votes
1
answer
818
views
Can't get my scanner nextLine() to work properly within a while loop [duplicate]
I am currently trying to have a recurring loop where the user inputs values to add to a linked list. But every time there is a space within one of the input values it causes the loop to skip the flag ...
0
votes
0
answers
57
views
While loop not working properly with scanner
Input:
2
Insert
5 23
Delete
0
Code:
int q = scan.nextInt();
while (q-- > 0) {
String s = scan.next();
if (s.equals("Delete")) {
list.remove(scan.nextInt());
} else {
...
1
vote
2
answers
301
views
Reading a series of numbers from the user until -1 is inputted, Java
I am trying to write a program to read a series of numbers from the user until -1 is entered. It should then print the average (with decimals) of those numbers (not including the -1). This is what I ...
2
votes
2
answers
149
views
Java while loop error printing user input requires enter key to be pressed twice
i'm trying to print out the invalid input entered by a user, but am running in to problems if they enter some long text containing spaces a second time ..... it requires enter key to be pressed twice ...
1
vote
3
answers
310
views
Is it OK to assign the input from Java Scanner to a variable inside the while loop condition?
For example, like this.
while ((input = kb.nextInt()) != 0) {
if (input % 2 == 0) {
System.out.println("even");
} else {
System.out.println("...
0
votes
1
answer
161
views
Method is printing out the wrong percentage integer
From this program I am creating an array and finding the total number of letter characters and finding a percentages of the total letters from A-D. I created a method that enables me to find the ...
0
votes
1
answer
532
views
How to exit from the scanner.hasNextLine() loop in java
I want to read the following input (as a single stream) from the terminal which contains multiple lines.
INSERT KEY adam key
TURN KEY adam
ENTER HOUSE adam
INSERT KEY pat foobar
TURN KEY pat
ENTER ...