It is a simple java code.. but Scanner class isn't taking the string as input. why?
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
double y=sc.nextDouble();
String s =sc.nextLine();
System.out.println("String: "+s);
System.out.println("Double: "+y);
System.out.println("Int: "+x);
}
Because the sc.nextInt() and sc.nextDouble() method does not consume the newline character of your input, so that newline is consumed in the next call to sc.nextLine()
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
sc.nextLine();
double y=sc.nextDouble();
sc.nextLine();
String s =sc.nextLine();
System.out.println("String: "+s);
System.out.println("Double: "+y);
System.out.println("Int: "+x);
}
Use nextLine() method to read all values and then parse them into the corresponding type (Integer, Double, etc). See why here: Integer.parseInt(scanner.nextLine()) vs scanner.nextInt()
Related
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
/* Read input */
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine(); // gets rid of the pesky newline
String s = scan.nextLine();
scan.close();
/* Print output */
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
The above code works perfectly fine in hackerrank compiler. While if i run it on IntelliJ, one extra call to scan.nextLine() is required in order to read the actual string.
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
/* Read input */
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine(); // gets rid of the pesky newline
scan.nextLine(); // gets rid of the pesky newline
String s = scan.nextLine();
scan.close();
/* Print output */
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
Can someone help me with why this could happen? I am assuming the call to nextDouble() would ignore the \n left in the buffer by nextInt() and pickup the next double token and leave a new \n in the buffer. So a single call to nextLine() should be enough to clear that \n from buffer, but why 2 calls are needed when I run this on IntelliJ.
Is this a JAVA version issue or is there something really basic which I am missing?
You probably see the effect of this official IDEA issue: Console readLine skips input in 2022.1.1
You could update to 2022.1.2 where this is fixed.
How do I get the following to print the string input? First I insert int and then insert a double and then insert string but the code does not return the whole string.
import java.util.Scanner;
public class TestScanner {
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);
}
}
Here is a test result. As you can see from below it prints the int and double but not the string.
3
2.5
Hello World
String: Hello
Double: 2.5
Int: 3
It's because the scan.nextDouble() method does not consume the last newline character of your input, and thus that newline is consumed in the next call to scan.nextLine().
For this a blank scan.nextLine() call after scan.nextDouble() to consume rest of that line including newline.
This is a sample code which might help you understand the workaround possible :
public class newLineIssue {
public static void main(String args[]) throws InterruptedException {
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);
}
}
I got the output as :
1
22.5
dsfgdsg
String: dsfgdsg
Double: 22.5
Int: 1
This is sample code to help you to print the entire line of String..
package com.practice;
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt(); // Read the Integer data type
double d = scan.nextDouble(); // Read the Double data type
scan.nextLine(); // Read the entire line of String
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
Input
45
56.24
Hi i am java developer!
Output
String: Hi i am java developer!
Double: 56.24
Int: 45
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);
}
import java.util.Scanner;
import java.io.*;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
Double y= sc.nextDouble();
Scanner sc1=new Scanner(System.in);
String name = sc1.nextLine();
System.out.println("String: "+name);
System.out.println("Double: "+y);
System.out.println("Int: "+x);
}
}
input : (before 'ram' have 5 space)
343434343
343.434343
ram sdf
Expected output :(before 'ram' have 5 space)
ram sdf
343.434343
343434343
You don't have to create two scanners. Also, you'll need to do nextLine() once you read the double.
Here is the corrected code snippet:
public static void main (String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
Double y = sc.nextDouble();
/* Note the change here */
sc.nextLine();
String name = sc.nextLine();
System.out.println("String: " + name);
System.out.println("Double: " + y);
System.out.println("Int: " + x);
}
Output:
String: ram sdf
Double: 343.434343
Int: 343
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);
}