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")
Related
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);
}
How do I read a sentence (string) from a line of input, and print whether it represents a declarative sentence (i.e., ending in a period), interrogatory (ending in a question mark), or an exclamation (ending in exclamation point) or is not a sentence (anything else)?
import java.util.*;
public class StringDemo {
public static void main(String[] args){
Scanner console = new Scanner(System.in);
// enter a string:
System.out.println("Enter a string: ");
String dog = console.nextLine();
dog = dog.toUpperCase();
System.out.println(dog + " has " + dog.length() +
" letters and starts with " + dog.substring(0, 1));
// enter another string:
System.out.println("Enter another string: ");
String cat = console.nextLine();
cat = cat.toUpperCase();
System.out.println(cat + " has " + cat.length() +
" letters and starts with " + cat.substring(0, 1));
// check contents here
if(dog.equals(cat))
System.out.println("Input Strings are matching ");
else
System.out.println("Input Strings are not matching ");
}
}
Need to use .equals() method to check content equality as below.
import java.util.*;
public class StringDemo {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
// enter a string:
System.out.println("Enter a string: ");
String dog = console.nextLine();
dog = dog.toUpperCase();
System.out.println(dog + " has " + dog.length() +
" letters and starts with " + dog.substring(0, 1));
// enter another string:
System.out.println("Enter another string: ");
String cat = console.nextLine();
cat = cat.toUpperCase();
System.out.println(cat + " has " + cat.length() +
" letters and starts with " + cat.substring(0, 1));
// check contents here
if(dog.endsWith("?"))
System.out.println("Input Strings ends in a question mark ");
}
}
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);
}