All Questions
Tagged with java.util.scanner string
462 questions
-1
votes
1
answer
109
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
63
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
113
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 ...
-1
votes
2
answers
77
views
Scanning the next line, but only to a certain length
I have an existing code, where I have to scan the next sentence that the user types into the console. This sentence can be no longer than 99 characters. My current code takes the .nextLine() method, ...
1
vote
1
answer
66
views
Is it possible to make an existing string editable for the user using Scanner? If so, how?
My program has String A = "hello"; and I want it to paste "A" into the user's input space so the user can modify it (e.g. backspace, add more text). Then once the user is done, ...
0
votes
3
answers
93
views
ow to separate a string from Scanner and store each word in an ArrayList?
How to add string from Scanner to ArrayList in split way?
First I tried this:
Scanner sc = new Scanner(System.in);
ArrayList<String> yourList = new ArrayList<>();
while (sc.hasNext()) {
...
0
votes
1
answer
47
views
So I'm trying to see if there is a way to count correct inputs and allow multiple inputs with boolean match. Am I able to do this?
So I've tried adding count ++ in multiple places in my code along with researching some way to allow multiple inputs to no avail. Am I missing something on placement or would I need to rewrite the ...
0
votes
2
answers
169
views
How to split integers from a String in seperate variables in Java?
I am trying to get the following to work:
Imagine the input via the scanner class is this:
new 10 32
I want to store these values into two seperate variables. But I struggle with the conversion from ...
0
votes
2
answers
359
views
Picking a word from a scanner input
How do you pick a word from a string or scanner input, say for instance a search engine search?
I looked up indexOf() for a string but I’m not finding how to make a public void word class from finding ...
0
votes
2
answers
197
views
How to use the hasNext() and next() methods
My goal is to read and print the words separately from standard input, without having to store them in an array or list.
I want this:
input: sun moon cloud
output:
sun moon cloud (each word in a ...
-1
votes
1
answer
113
views
The code works in some cases but gives different outputs based on the sequence of the input
Expected Output should be as such:
Download
String: Welcome to HackerRank's Java tutorials!
Double: 3.1415
Int: 42
My code gives different outputs based on what input I put in at first.
If possible ...
1
vote
1
answer
768
views
Incompatible operand types Scanner and String?
I keep getting the following errors:
Incompatible operand types Scanner
and String
Incompatible operand types int and
String
before I added the int op = Integer.valueOf(operator) line it kept giving ...
1
vote
1
answer
1k
views
Why do I have to input two duplicate nextLine method in order for spaces to work on printing a String instead of just one duplicate nextLine method?
I was taking a coding challenge in which I was to take an integer, a double, and a string from stdin and have this print in stdout. The String had to be a String in which had to include more than a ...
0
votes
1
answer
605
views
How do I write a Java program that reads two words representing passwords from the keyboard and outputs the number of characters in the smaller one?
I am new to learning Java and I am currently writing a short program to take the input of two words from the keyboard and output the length of the smaller word.
I am unsure of how to do this, since I ...
0
votes
2
answers
1k
views
How do I read strings from a file that already contain double quotes?
I have a list of names in a .txt file which are in the format:
"Tim", "Dave", "Simon"
The input will always be single value names in quotes, comma separated and on a ...
-1
votes
1
answer
43
views
User input string to array with no max line needed
I'm trying to get the user's input, then store the input into an array but there is a problem... i want the for cycle to end once the user type ' ' (space). Any help would be appreciated!
public ...
1
vote
2
answers
149
views
How to get my scanner to recognize the words i have for it
I'm pretty new to java and coding in general and am trying to figure out how to get this game to work for a project at school. It is meant for you to type in a month and it will then ask you to choose ...
0
votes
0
answers
35
views
String value not printing [duplicate]
I'm trying to generated a salted password on Java, and I'm having issues trying to print out the string "password" when I run the program and print password is just empty.
import java.util....
-1
votes
1
answer
86
views
How to take only the integer input from a Sentence?
I want to get the integers or float or double values from a sentence. As an example - "John has 20 balls, from which 4 are red and 5 are green balls". this will be my input . I want to get ...
0
votes
2
answers
1k
views
Java program that asks for user's name and prints it - issue with error message
I have a program that asks for the user's name and prints it back out. It can do that part fine, but it currently has the issue of not printing the proper error message when the user leaves the prompt ...
-1
votes
1
answer
32
views
Read characters from console, ignoring whitespaces and new lines
I have a question which is pretty easy to solve, yet not so.I am given an input for a game in which you have to read form the console.There are input commands: UP(U), DOWN(D), LEFT(L), RIGHT(R) coming ...
0
votes
0
answers
49
views
Not printing lines until after completion Java
I created a mortgage calculator which works just fine but I don't want to use println() when asking for user input. I want the user to be able to type on the same line; however, everytime i use print()...
0
votes
2
answers
557
views
The Java Scanner is not taking my String Input
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
String s = scan.nextLine();
System.out....
0
votes
2
answers
482
views
Replacing a specific string from a text file in Java
I've got a text file like this: Image
The first column represents the user ID and the last column represents balance. I want to iterate through the elements to find a specific user ID and then update ...
0
votes
2
answers
345
views
Why does my code about Bracket Fixing hangs?
My code makes an incomplete combination of expressions with closing brackets")" to a complete combination of expressions with the right placement of Opening Brackets "(". If ...
-1
votes
2
answers
467
views
Java Date of Birth Input and Output
I'm trying to use Scanner method to let me input a date of birth in this format - mm/dd/yy
and the output that I'd like to get would be something like this.
Input - 02/06/12
Output - February 6, 2012
...
0
votes
0
answers
32
views
Java skipping Console.nextLine() [duplicate]
I'm making a simple program in Java, in which ask the user:
initialBalance, double
rate, integer
client, String
I have a problem: the first two inputs work, but I cannot make the third to function, ...
0
votes
1
answer
26
views
file reading in java- converting every line to an existing function
I have an input text file that contains different commands that I need to do,
the commands have to be done one by one, and I don't know-how.
I thought of just reading the text file--->putting the ...
2
votes
2
answers
94
views
Why is my String Variable empty after assigning something to it for a second time? [duplicate]
I am doing a project for school, and I am trying to make it to where you can set up a name for yourself while going through a series of questions asked by the computer. I want the user to be able to ...
0
votes
0
answers
29
views
Why this java code always print "Unknown City" for any value input by the user? [duplicate]
This Java program is successfully compiled and run and accepting input from the user but for every city value(Meerut, Noida, Agra), it is showing "Unknown City" Why?? Why it is always ...
1
vote
1
answer
99
views
How to input values or phrases on a window?
I'm trying to write a program that solves an equation for a school project, but I can't figure out how to create a space for writing in the screen instead of eclipse's console. Like, creating a space ...
0
votes
2
answers
378
views
Scanner nextLine() assigns empty value to a String [duplicate]
For some reason query = sc.nextLine(); code returns query "" for the Delete part.
So that the second code block does not execute and outputs a wrong list (First index of the list is not ...
-1
votes
2
answers
139
views
!string.equals(string) turns false result
In a part of my program, I put a while loop to repeatedly ask for inputs. There is an option to type in the letter "F" and break the loop.
This is my program:
public class Example {
...
2
votes
1
answer
213
views
Find the alphabetically first String using Java
My professor asked me to create a solution for this code, and I cannot quite get a good output.
He asked me a couple things:
Ask the user to input a String value, the value "END", ...
0
votes
1
answer
633
views
I just wanted to know the functionality of readInput() in the code below
I want to know the purpose of the readInput() function
import java.util.Scanner;
public class Rainbow {
public static void main(String[] args) {
String rainbowColors[] = {"Red",...
1
vote
2
answers
3k
views
How do I check for special characters in a String[Java]?
I am creating a console game.. and I want the players to be able to choose their own usernames. Having usernames like .-|\12b-}| can be really weird. I want to be able to check if the username has &...
-2
votes
1
answer
45
views
Scanner not reading Next() or NextLine() immediately
I have the following code
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter A name");
String aString = keyboard.nextLine();
System.out.print(aString);
System.out.print("...
0
votes
2
answers
39
views
Error verifying scanner input contains two words with one space
Trying to verify the user just enters psuedo code: word1 space word2. This seems like it should be simple logic, if string doesn't equal word1 space word2, ask again. I've built a version with if/else ...
0
votes
1
answer
278
views
How to use java scanner with string validation?
I have the following code:
String f_name = "";
System.out.println(ANSI_PURPLE + "What is your first name?");
System.out.print(ANSI_RESET + " Type your name here (use only ...
0
votes
3
answers
2k
views
Using loops to reverse a string
I am having trouble doing a task whereby someone enters a multiple line string, and I read the string and output each word in reverse in the exact same spot (whitespace and line breaks the same). ...
1
vote
1
answer
170
views
How to parse (via loop) through a string until the end of the string?
Given String "5 6 7 8 9 2 3 " how do you use a while loop or for loop to cycle through each number without repeating the string (as a while loop would)?
String myString = "5 6 7 8 9 2 ...
0
votes
1
answer
109
views
I'm getting Exception in thread "main" java.util.NoSuchElementException: No line found
This is my code which throws Runtime(NZEC) exception. It gives correct output in my vs code ide but when i run it in codechef's ide it throws Runtime exception.
Here is my code
/* package codechef; // ...
1
vote
2
answers
101
views
Including spaces in Scanner in java
I am writing a simple code that takes number,name,surname from the user with scanner.
But when user enters name with spaces in it(two or more names) the code thinks string after the first space is ...
-1
votes
1
answer
880
views
How to implement Body Mass Index (BMI)in Java
My problem is how can I implement without the Scanner method and with Math.round and Math.pow?
Here is my code:
import java.util.Scanner;
public class BMI{
public static void main(String ...
1
vote
1
answer
84
views
How to use Scanner.useDelimiter() to match two characters next to each other followed by a word?
I am trying to parse a plain .txt file with the general structure
[[Title]]
CATEGORIES: text, text, text
some text etc...
[[Next Title]]
CATEGORIES: text, text, text
Next other text etc ...
In my ...
0
votes
1
answer
79
views
Reading in a text file efficiently [duplicate]
How would you make a method to read a text file in java, given that the text file has 20,000 rows. The first element of the row is a word and the rest 50 values are the corresponding word vector
0
votes
1
answer
116
views
How could I separate this string by 2 character increments?
There are no delimiters and the string itself comes from a file formatted like the following:
BB
GB
GB
BG
GG
GB
GB
GB
GB
GG
After using the following code I'm left with BBGBGBBGGGGBGBGBGBGG, as ...
1
vote
1
answer
780
views
Accept empty string or string with length of 0 as input using Scanner in Java
I use Scanner in Java to get user entries such as name, surname, email and tel. The name and surname are required but the email and tel are not required and they can be leaved as empty by user just by ...
-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
2
answers
44
views
I am trying to take the userInput and double any numbers found by using the split method [closed]
I am trying to take the user input using a scanner, and double any number within the userInput. For example: I have no clue what to do 22
should outprint as: I have no clue what to do 44
When I try to ...