String concat error using Scanner class? [duplicate] - java

This question already has answers here:
What's the difference between next() and nextLine() methods from Scanner class?
(15 answers)
Closed 6 years ago.
I have a error when i give a input for string t= "is my favorite language";
it shows output java is . please tell what i made a mistake.
public class DataTypes {
public static void main(String[] args) {
String s = "Java ";
Scanner scan = new Scanner(System.in);
String t = scan.next();
String u = s.concat(t);
System.out.println(u);
}
}

scan.next() will get the next token from the input. The default delimiter for this is whitespace, so it will get the first word from your input.
To get all of the input up until the newline (when the user presses enter) use scan.nextLine() instead.
String s = "Java ";
Scanner scan = new Scanner(System.in);
String t = scan.nextLine();
String u = s + t;
System.out.println(u);

Use:
scanner.nextLine()
This will read a whole line till the system defined line seperator (usually \n)
scanner.next() only reads the next token till space.

Related

java Scanner doesn't pop up [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 1 year ago.
I'm new to java and I wrote this code
import java.util.Scanner;
public class GamingJava {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String Name;
int password;
String yEs;
System.out.print("hello sir what is your name? ");
Name = input.nextLine();
System.out.print("what is your password? ");
password = input.nextInt();
System.out.println("your name was "+Name+" and your password was "+password);
System.out.print("are you sure? ");
yEs = input.nextLine();
System.out.println(yEs);
}
}
It only ask the name and the password, but Java doesn't ask the last one how did that happen?
It asks for the input and immediately takes it as an empty string i.e. "".
Root Cause : The issue is because of the nextLine() method. Since while providing an input, user has to press the Enter key, the cursor/prompt on the console moves to next line. This line is taken as an empty line by the nextLine() method.
Solution : You must use the next() method as it looks out for the presence of space character for taking the input.
FYR the following line of code
yEs = input.nextLine();
should be changed to
yEs = input.next();

how to take multiple input using NextLine() in java [duplicate]

This question already has answers here:
What's the difference between next() and nextLine() methods from Scanner class?
(15 answers)
Closed 5 years ago.
how to take more inputs from user using Nextline() in java
if i use scn.Nextline() after reading one input it will work but giving a null in answer.
public class Mapdemo {
public static void main(String[] args) {
Scanner scn =new Scanner(System.in);
System.out.println("enter the number of phoneaddress you want to store");
int n=scn.nextInt();
int j=0;
System.out.println("enter the phonenumber first and then name of the person phoneaddress");
Map<String, Long> m1=new HashMap<String,Long>();
for(int i=0;i<n;i++)
{
Long l=scn.nextLong();
String s=scn.nextLine();
m1.put(s,l);
}
System.out.println("enter the name to be checked");
String s2=scn.next();
System.out.println(m1.get(s2));
After using .nextInt() for the first time do:
scanner.nextLine();
after that to clear the line, nextLine() stops at the newline character (OS dependant).
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()

Java string input not working correctly [duplicate]

This question already has answers here:
What's the difference between next() and nextLine() methods from Scanner class?
(15 answers)
Closed 5 years ago.
I have made this program and I have gotten stuck. When I run it and I have inputs without using spaces it works fine, for example just Bob input in customer input. However, when I enter Bob White, it merges the next two string input directions (as Shown in pic attached) . What am I doing wrong here?
import java.util.*;
public class Blah{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String customerName;
System.out.println("Enter customer name: ");
customerName = in.next();
String customerAddress;
System.out.println("Enter customer address: ");
customerAddress = in.next();
String customerPhoneNumber;
System.out.println("Enter customer phone number: ");
customerPhoneNumber = in.next();
in.close();
}
}
Thats because the Scanner#next method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner#nextLine
Try using nextLine() method instead with trem() method
customerName = in.nextLine().trim();
You need to read the entire line using nextLine() method then trim it from leading and trailing whitespace using trim() method.
customerName = in.nextLine().trim();
customerAddress = in.next().trim();
customerPhoneNumber = in.next().trim();

Concat() in Java [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 6 years ago.
I've been trying this program. But I'm not getting the required output.
import java.util.*;
class MiscTest {
public static void main(String[] args) {
int i = 10;
double d = 4.0;
String s = "Lavante ";
Scanner scanner = new Scanner(System.in);
int j;
double dd;
String ss;
j = scanner.nextInt();
dd = scanner.nextDouble();
ss = scanner.next();
System.out.println("\n" + (i + j));
System.out.println(d + dd);
System.out.println(s.concat(ss));
}
}
The input I've given:
The Output I got:
I need the whole sentence "Lavante is from Maserati" as output. But, I'm getting only one word from the Second String. Help me.
Thanks in advance.
Scanner#next() reads up to the first whitespace. If you want to read up to the end of the line, you should use nextLine() instead:
ss = scanner.nextLine();
You need to use nextLine() otherwise, Scanner stops at the whitespace delimiter using next()
Also, after having used scanner.nextDouble(), use scanner.next() to consume the rest of the characters not read by the previous call, just like below.
dd = scanner.nextDouble();
scanner.next();
ss = scanner.nextLine();
I guess you are hitting enter button after providing input of double variable. try code snippet.
dd = scan.nextDouble();
ss = scan.nextLine().trim();
if (ss.isEmpty()) {
ss = scan.nextLine();
}
You are currently using the next() Function which is using to take only a String before white spaces
Suppose Input : Hello World
Then If User using next()
Output: Hello
To take the string till the End of line Use nextLine()
Input: Hello World
If user using nextLine()
Output: Hello World

What is the difference between .nextLine() and .next()? [duplicate]

This question already has answers here:
Understanding Scanner's nextLine(), next(), and nextInt() methods
(2 answers)
Closed 6 years ago.
I am making a program and am using user input. When I am getting a String I have always used .nextLine(), but I have also used .next() and it does the same thing. What is the difference?
Scanner input = new Scanner(System.in);
String h = input.nextLine();
String n = input.next();
What is the difference?
To make long story short:
nextLine - reads the whole line.
next - reads until the first space.
As an example:
// Input : Barack Obama
String st = in.next(); // st holds the String "Barack"
String st2 = in.nextLine(); //st2 holds the String "Barack Obama"

Categories

Resources