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);
}
This question already has answers here:
Scanner class skips over whitespace
(5 answers)
Closed 6 years ago.
I want to print
String: fsdfsdf sdf
Double: 2.3534534534523453E11
Int: 2147483647
Using java if i give the input as
2147483647
235345345345.234534
fsdfsdf sdf
but my output is
String: fsdfsdf sdf
Double: 2.3534534534523453E11
Int: 2147483647
help me how can i include those spaces at the front in the output
my code:
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();
String sn = scan.nextLine();
System.out.println("String: " + s + sn);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
A Scanner uses whitespace to break input into tokens by default. And Scanner.next() returns the next found token, which in your case is "fsdfsdf".
Simply set a different delimiter, e.g. the line separator:
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
scan.useDelimiter(System.lineSeparator());
int i = scan.nextInt();
double d = scan.nextDouble();
String s = scan.next();
String sn = scan.nextLine();
System.out.println("String: " + s + sn);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
Prints:
String: fsdfsdf sdf
Double: 2.3534534534523453E11
Int: 2147483647
(note that this also makes String sn = scan.nextLine(); redundant, because next() will already give you " fsdfsdf sdf")
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
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();
}