Cannot assign a string value from nextLine() in Scanner class - java

I'm trying to understand why inputString is still empty after executing this
public static void main(String[] args) {
// write your code here
int inputInt = 0;
double inputDouble = 0.0;
String inputString = null;
Scanner scanner3 = new Scanner(System.in);
if (scanner3.hasNext()) {
inputInt = scanner3.nextInt();
}
if (scanner3.hasNext()) {
inputDouble = scanner3.nextDouble();
}
if (scanner3.hasNext()) {
inputString = scanner3.nextLine();
} else {
throw new RuntimeException("No entries left");
}
System.out.println("String: " + inputString);
System.out.println("Double: " + inputDouble);
System.out.println("Int: " + inputInt);
}

nextLine() read new line character before read your character. Add a extra nextLine() to read that new line.
if (scanner3.hasNext()) {
scanner3.nextLine();
inputString = scanner3.nextLine();
}

Related

scanner.close() Does not work when I use try/finally

import java.util.Scanner;
public class userInput
{
public static void main(String[]args){
try{
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int age = scanner.nextInt();
scanner.nextLine();
String text = scanner.nextLine();
System.out.println(name + "\n" + age + "\n" + text);
//scanner.close(); //it works here
}
finally{
scanner.close(); // does not work here"scanner cannot be resolvedJava(570425394)"
}
}
}
You have to define scanner before "try", so it's ok, now this is your code:
import java.util.Scanner;
public class userInput {
public static void main(String[]args) {
Scanner scanner = new Scanner(System.in);
try {
String name = scanner.nextLine();
int age = scanner.nextInt();
scanner.nextLine();
String text = scanner.nextLine();
System.out.println(name + "\n" + age + "\n" + text);
}
finally {
scanner.close();
}
}
}

Why character in StringBuild does not change?

The character must be entered from the console to change to lowercase letters on this line. But it displays the same word and the symbol does not change.
public class Task {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(requestString());
char symbol = requestSymbol().charAt(0);
int count = 0;
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == symbol) {
sb.setCharAt(i, sb.charAt(Character.toUpperCase(i)));
count++;
}
}
System.out.println("Number of entries: " + count);
System.out.println("Converted string: " + sb);
}
static String requestString() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter string:");
return scanner.nextLine();
}
static String requestSymbol() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the symbol:");
return scanner.next();
}
}
It seems to be a problem with the line:
sb.setCharAt(i, sb.charAt(Character.toUpperCase(i)));
It should be:
sb.setCharAt(i, Character.toUpperCase(symbol));

How to invoke method to print?

https://its.hvcc.edu/jojo/?p=297
Doing this assignment I need the results of the method to print but I cant figure out how to invoke them.
public static int numWhitespace(String s) {
int count = 0,x;
for(x = 0; x<s.length();x++)
if(Character.isWhitespace(s.charAt(x)))
count++;
return count;
}
public static void main (String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println("Enter a string for character classification: (EOF to end):");
while (kb.hasNext()){
String input = kb.nextLine();
System.out.println("inputLine = "+ input +"");
System.out.println("inputLine is "+ input.length() +" characters long and contains the following:");
System.out.println(); //Where i want all the other stuff
System.out.println("Enter a string for character classification: (EOF to end):");
}
}
You can call the numWhitespace() method like this:
System.out.println("whitespaces = " + numWhitespace(input));
or save it in a variable first:
int whitespaces = numWhitespace(input);
System.out.println("whitespaces = " + whitespaces);

How to input a String in java except using nextLine() and I need to print the whole statement/sentences with spaces

So, below in my code, I want to input a string but without using the function nextLine() and I need to print the whole statement with statement. And I have already tried the alternative keyword next() it only prints the first word and because of a space it stops. But I need to print the whole statement.
SO, in that case what should be the solution?
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
String s = scan.next();
// Write your code here.
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
you need to put an extra scan.nextLine before your string input scanner to consume the extra \n like
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.next();
import java.io.*;
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double d=scan.nextDouble();
int i = scan.nextInt();
scan.nextLine();
String s=scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
Scanner.next() method finds and returns the next complete token.
It is hard to do without nextLine() and arrays. But this what I tired to do without arrays and nextLine(). Somehow have to enter a value(in this case I used -1) to end the loop.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
boolean flag = true;
String s="";
System.out.println("enter -1 to exit");
while(flag){
s += scan.next() + " ";
if(s.contains("-1")){
flag = false;
}
}
System.out.println("String: " + s.replace("-1", ""));
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
You can either use an array to store the input from Scanner#next or a List (preferable) then simply display the contents of the list.
Scanner scan = new Scanner(System.in);
List<String> tempList = new ArrayList<>();
int i = scan.nextInt();
double d = scan.nextDouble();
String s = scan.next();
while(!s.equalsIgnoreCase("exit")){
tempList.add(s);
s = scan.next();
}
System.out.println("String: " + tempList.toString().replace("[","")
.replace("]","").replace(",",""));
System.out.println("Double: " + d);
System.out.println("Int: " + i);
So simple just use another reference variable of Scanner variable ,
before the .nextLine() method reads the nextLine tokens of .nextInt();
hence , by creating new object it clears the temporary memory.
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double d=scan.nextDouble();
int i = scan.nextInt();
Scanner newscan=new Scanner(System.in);
String s=newscan.nextLine();
// Write your code here.
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
The problem is with the scan.nextInt() method - it only reads the int value. So when you continue reading with scan.nextLine() you receive the "\n" Enter key. So to skip this you have to add the scan.nextLine().
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
we can use parse() for taking both the input of int and double
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i=Integer.parseInt(scan.nextLine());
double d=Double.parseDouble(scan.nextLine());
String s=scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}

Error in reading double and string value from standard input?

Here is my code:
public class Solution {
public static void main(String[] args) {
/*
* Enter your code here. Read input from STDIN.
* Print output to STDOUT.
* Your class should be named Solution.
*/
int num = 0;
double dou = 0.0;
String s = null;
Scanner in = new Scanner(System.in);
if (in.hasNextInt()) {
num = in.nextInt();
}
Scanner d = new Scanner(System.in);
if (d.hasNextDouble()) {
dou = d.nextDouble();
}
Scanner str = new Scanner(System.in);
if (str.hasNextLine()) {
s = str.nextLine();
}
System.out.println("String:" + s);
System.out.println("Double:" + dou);
System.out.println("Int:" + num);
}
}
I am getting this output:
String:null
Double:0.0
Int:42
But it should should look like this:
String: Welcome to Hackerrank Java tutorials!
Double: 3.1415
Int: 42
Can anyone explain me why I'm getting a null value for the string and 0.0 for the double?
//your answer
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int i = in.nextInt();
double d = in.nextDouble();
in.nextLine(); // finishes the previous line
String s = in.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
You should not use 3 scanners, one is enough. see Scanner method opened and closed twice.
Apart from that, when using only one, there can still be a problem like in this question:
Java String Scanner input does not wait for info, moves directly to next statement. How to wait for info?
It works fine when i test it. How do you enter your float ? 3.1415 or 3,1415 ? Depending on your local one will be red and the other not.
And if want the result to be in one line:
String chaine= String.format("String: %s. Double: %.5f. Int: %d", s,dou,num);
System.out.println(chaine);
This solution works
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
double d = scanner.nextDouble();
scanner.nextLine();
String s = scanner.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
scanner.close();
}
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
String s = scan.next();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
You can do something like this. for every new line scanner.nextLine() should be called.
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
String s="";
Double d=0d;
if(scan.hasNextLine()){
scan.nextLine();
if(scan.hasNextDouble()){
d=scan.nextDouble();
scan.nextLine();
s=scan.nextLine();
}else{
s=scan.nextLine();
scan.nextLine();
d=scan.nextDouble();
}
}
Solution
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = Integer.parseInt(scan.nextLine());
Double d = Double.parseDouble(scan.nextLine());
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
hey this error is because the java does not have fflush() in like C hence when you press enter after double or integer value the buffer contain enter key which is empty hence when you print using nextLine() it prints that line is empty hence error occurs so you have to call it to the next line using sc.nextLine() function hence the correct output will be displayed
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
String s=scan.next();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}

Categories

Resources