0

I was trying to take string input in java. My input should be like this

3
1,1,[email protected],123 Sesame St.,New York,NY,10011,12345689010
1,2,[email protected],123 Sesame St.,New York,NY,10011,12345689010
1,3,[email protected],123 Sesame St.,New York,NY,10011,12345689010

So, I tried this

Scanner in = new Scanner(System.in);
 int TotalNumber = in.nextInt();
 String[] Data = new String[TotalNumber];
 for (int Counter = 0; Counter < TotalNumber; Counter++) {
 Data[Counter] = in.next();
 }
 in.close();
 for (int counter = 0; counter < Data.length; counter++) {
 System.out.println(Data[counter]);
 }

My output is showing this

1,1,[email protected],123
Sesame
St.,New

What is my problem ? How take input string line properly ?

Update

I found my solution at here Scanner issue when using nextLine after nextXXX

asked Feb 9, 2014 at 16:43

4 Answers 4

1

next() breaks at a whitespace. Instead, you should use nextLine() to input the entire line to your string:

int TotalNumber = in.nextInt();
String[] Data = new String[TotalNumber];
for (int Counter = 0; Counter < TotalNumber; Counter++) {
 Data[Counter] = in.nextLine();
}
answered Feb 9, 2014 at 16:46
Sign up to request clarification or add additional context in comments.

4 Comments

I tried with nextLine() 3 1,1,[email protected],123 Sesame St.,New York,NY,10011,12345689010 1,1,[email protected],123 Sesame St.,New York,NY,10011,12345689010 it only take 2 inputs from me
could you explain what happened? I mean the output.
First I gave 3(It should take 3 lines of string right ?) then hit entered first line then again hit enter and the second line then I hit enter and only two lines are showing. But, It should be take 3 lines and show 3 lines
I think an enter was pressed accidentally? Are you seeing the same output always.
0

Try with Data[Counter] = in.nextLine();

answered Feb 9, 2014 at 16:47

Comments

0

What about:

import java.util.Scanner;
import java.lang.String;
public class Test
{
 public static void main(String[] args)
 {
 char[] sArray;
 Scanner scan = new Scanner(System.in);
 System.out.print("Enter a Palindrome : ");
 String s = scan.nextLine();
 s = s.replaceAll("\\s+", "");
 sArray = new char[s.length()];
 for(int i = 0; i < s.length(); i++)
 {
 sArray[i] = s.charAt(i);
 System.out.print(sArray[i]);
 }
 }
}
answered Feb 9, 2014 at 17:00

Comments

0

Try this (Mureinik modified code)..

int TotalNumber = in.nextInt();
in.nextLine();
String[] Data = new String[TotalNumber];
for (int Counter = 0; Counter < TotalNumber; Counter++) {
 Data[Counter] = in.nextLine();
}

You need a nextLine() after taking the int because you will press enter after taking int and that enter is read by nextLine() in the Data[0].

answered Feb 9, 2014 at 17:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.