Concat() in Java [duplicate] - java

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

Related

Failure trying to read String using Scanner.nextLine() [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 27 days ago.
I am trying to read a String and output it to the terminal, as well as a few primitives, but it does not seem to work. The code is below:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
String s = scan.nextLine();
scan.close();
System.out.println("String: " + s + "\n");
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
I have tried to find other ways to parse the string in the Scanner class, but none of them seemed to work. What I except is for the String to be properly read and outputed to the terminal, along with the few other values.
Thanks in advance.
The problem is that when you press enter (when you digit the number) you are inserting a \n character inside the buffer, so when you will call nextLine() you will read the \n (because nextLine reads the input until the \n) and you will not be able to write anything because the function returns when found the \n.
You can clean it calling scanner.nextLine() one more time
int i = scan.nextInt();
double d = scan.nextDouble();
// Remove \n from buffer
scan.nextLine();
String s = scan.nextLine();

Scanner next methods in Java [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 still confused on how the scanner next methods actually work. I have an example program right here:
import java.util.Scanner;
public class HelloJava{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int varInt;
String varString;
String varStringTwo;
System.out.print("Insert int value: ");
varInt = sc.nextInt();
System.out.print("Insert string value: ");
varString = sc.nextLine();
System.out.print("Insert another string value: ");
varStringTwo = sc.nextLine();
sc.close();
}
}
When i executed the program and entered an integer on the first prompt, the terminal looked like this:
Insert int value: 15
Insert string value: Insert another string value: // i can input any value here //
// ^ but the program doesn't allow me to input anything here
I know that one of the solution is to put "sc.nextLine();" between varInt = "sc.nextInt();" and "System.out.print("Insert a string value: ");", but I don't understand why or how.
When you scan a String values with spaces the scanner.next() method will get you the string up to space.
For example,
your input is This is a String
so when the first time you run scanner.next() it will return the value This. If you run it a second time it will return you is and so on.

Difficulty in displaying the input string in java [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 4 years ago.
I was solving a problem on Hackerrank in which I have to input an integer , a double and a string and print the result using stdout.
Input
42
3.1415
Welcome to HackerRank's Java tutorials!
Output
String: Welcome to HackerRank's Java tutorials!
Double: 3.1415
Int: 42
I wrote the following code and i'm not able to pass my test cases:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String ss = s.nextLine();
int i = s.nextInt();
double d = s.nextDouble();
System.out.println("String: "+ss);
System.out.println("Double: "+d);
System.out.println("Int: "+i);
}
}
is there anything wrong in the code?
The question also states that :-
Note: If you use the nextLine() method immediately following the nextInt() method, recall that nextInt() reads integer tokens; because of this, the last newline character for that line of integer input is still queued in the input buffer and the next nextLine() will be reading the remainder of the integer line (which is empty).
Following was my output:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Solution.main(Solution.java:8)
Can anyone please explain this ?
the trick is to add s.nextLine(); before getting the String because it will reads the remainder of the line with the number on it (with nothing after the number I suspect)
Try placing a s.nextLine(); after each nextInt() if you intend to ignore the rest of the line.
so the full code will be like :
Scanner s = new Scanner(System.in);
int i = s.nextInt();
double d = s.nextDouble();
s.nextLine();
String ss = s.nextLine(
System.out.println("String: "+ss);
System.out.println("Double: "+d);
System.out.println("Int: "+i);

Better way to read nextLine [duplicate]

This question already has answers here:
Using scanner.nextLine() [duplicate]
(5 answers)
Closed 6 years ago.
Working through a HackerRank tutorial, and I was wondering -- is there a better way to strip off the newline character that comes after reading in the double? It feels really manual and "hacky" to just repeat a nextLine()
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 b = scan.nextLine();
String s = scan.nextLine();
}
}
Note: code works as-is, just asking if there is a less hacky way to go about this
Operate on lines at a time, then you don't need to worry about the nextDouble() (or nextInt()) calls leaving a trailing newline. Like,
Scanner scan = new Scanner(System.in);
int i = Integer.parseInt(scan.nextLine());
double d = Double.parseDouble(scan.nextLine());
String s = scan.nextLine();
If, you want to allow the int and double to be on the same line then you could do
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = Double.parseDouble(scan.nextLine());
String s = scan.nextLine();
But without knowing your input format, that may or not be helpful. I would prefer the version most readable for the problem at hand (parsing the input).

String concat error using Scanner class? [duplicate]

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.

Categories

Resources