0

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

3
1,1,bugs@bunny.com,123 Sesame St.,New York,NY,10011,12345689010
1,2,bugs@bunny.com,123 Sesame St.,New York,NY,10011,12345689010
1,3,bugs@bunny.com,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,bugs@bunny.com,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

Community
  • 1
  • 1
Kona Ahmed
  • 65
  • 2
  • 8

4 Answers4

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();
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • I tried with nextLine() 3 1,1,bugs@bunny.com,123 Sesame St.,New York,NY,10011,12345689010 1,1,bugs@bunny.com,123 Sesame St.,New York,NY,10011,12345689010 it only take 2 inputs from me – Kona Ahmed Feb 09 '14 at 16:55
  • could you explain what happened? I mean the output. – JavaTechnical Feb 09 '14 at 17:02
  • 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 – Kona Ahmed Feb 09 '14 at 17:05
  • I think an enter was pressed accidentally? Are you seeing the same output always. – JavaTechnical Feb 09 '14 at 17:07
0

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

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
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]);
        }
    }
}
Lefteris Bab
  • 787
  • 9
  • 19
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].

JavaTechnical
  • 8,846
  • 8
  • 61
  • 97