Reputation: 11
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);
}
}
Upvotes: 1
Views: 4306
Reputation: 1
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);
}
Upvotes: 0
Reputation: 11
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);
}
}
Upvotes: 1
Reputation: 11
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);
}
}
Upvotes: 1
Reputation: 1
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);
}
}
Upvotes: 0
Reputation: 11
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();
Upvotes: 1
Reputation: 56489
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);
Upvotes: 0
Reputation: 22477
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);
}
Upvotes: 0