0

I have a fraction of code which is working. This is basically a scanner input so that it can save String with space. This is my code:

public class TestScannerString {
    
    static Scanner sc = new Scanner(System.in);
    
    public static void main(String[] args) {
        System.out.println("Enter strings: "); 
        String[] a = sc.nextLine().split(" ");
        String name = "";
 
        for (String a0 : a) {
            name += a0 + " ";
        }
        
        System.out.println(name);

    }
}

This is the output of that code: Output 1

Then i try to implement this code into mini-project. This is the block that contain previous code:

for (int i=0; i<jumlahData; i++) {
            System.out.println("Masukkan Data Bayi ke " +(i+1));
            System.out.println("Nama : ");
            String[] a = sc.nextLine().split(" ");
            String name = "";
            for (String a0 : a) {
                name += a0 + " ";
            }
            bayi[i].nama = name;
            sc.nextLine();
            System.out.print("Usia dalam Bulan : ");
            bayi[i].usiaDalamBulan = sc.nextInt();
            System.out.print("Berat (kg) : ");
            bayi[i].beratDalamKg = sc.nextFloat();
            System.out.print("Panjang (cm) : ");
            bayi[i].panjangDalamCm = sc.nextInt();
            System.out.println("Name :" +name);
            System.out.println();
            System.out.println("Nama bayi ke " + (i+1) + ": " +bayi[i].nama);
}

After i input "example of name" and print the name, it returns empty string like this:

Output Empty String

Any suggestion why this happens and what should i do? Any help would be appreciated. Thanks before.

wiryadev
  • 322
  • 2
  • 15
  • So you are tokenizing a string input on every iteration just to reconstruct the same string? Why not just take the string as the user inputs it? *bayi[i].nama = sc.nextLine()* – Omar Abdel Bari Sep 24 '20 at 15:26
  • Why do you even want to do the whole ```name``` code? Your ```name``` variable and input are same. Why split and then re-join? – Swati Srivastava Sep 24 '20 at 15:40
  • A suggestion. In your ```System.out.println("Nama : ");```, rather use ```System.out.print()``` so that the ```example of name``` comes right after ```Nama```, and not in the next line. – Swati Srivastava Sep 24 '20 at 15:43
  • The bayi[i].nama is a string not string array, so thats why i reconstruct the string array to be a string – wiryadev Sep 25 '20 at 03:23

2 Answers2

1

Okay so the fix is by putting nextLine() in the beginning of loop before the scanner input. I think it is because of previous input that has newLine in it. So the final code is like this:

for (int i=0; i<jumlahData; i++) {
            sc.nextLine();
            System.out.println("Masukkan Data Bayi ke " +(i+1));
            System.out.print("Nama : ");
            String nameTest = sc.nextLine();
            bayi[i].nama = nameTest;
            System.out.print("Usia dalam Bulan : ");
            bayi[i].usiaDalamBulan = sc.nextInt();
            System.out.print("Berat (kg) : ");
            bayi[i].beratDalamKg = sc.nextFloat();
            System.out.print("Panjang (cm) : ");
            bayi[i].panjangDalamCm = sc.nextInt();
            System.out.println();
}
wiryadev
  • 322
  • 2
  • 15
0

I don't have the complete code to test this but it seems like you have two lines of code for taking a line of string as an input.

String[] a = sc.nextLine().split(" ");

and

sc.nextLine();

My guess is you are actually entering the name for the second one, which doesn't save the input to any reference. For the former line of code you probably are mistakenly responding with an empty string.

Omar Abdel Bari
  • 682
  • 7
  • 17
  • The ```sc.nextLine()``` after ```bayi[i].nama = name;``` is written because Scanner class does not take Integer input after taking a String input. It considers the integer input to be a part of String input. To solve this problem, either input integer first and then String at the last, or add a new ```sc.nextLine()``` after String input, as @abw1904 did. – Swati Srivastava Sep 24 '20 at 15:51
  • Then he can apply one of the answer given in https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo. These fuzzy kinds of hacks lead to side effects like a confusing experience when users are providing input. He should try a better option from one of these other questions. One that I found amusing was having two separate scanners to avoid the issue. – Omar Abdel Bari Sep 24 '20 at 16:49
  • sc.nextLine is used so that it not skip to the next input. Any other suggestion? – wiryadev Sep 25 '20 at 03:26
  • In my above comment there is a lot of options available in the linked question. I mentioned one in the options presented but have not tried it myself to see if it doesn't have side effects.. – Omar Abdel Bari Sep 25 '20 at 03:27
  • I tried the solution there but it only can be applied if "integer first, string later", no otherwise, it will skip the name too. I tried using the scanner.skip too but doesnt work. – wiryadev Sep 25 '20 at 03:51
  • In the worst case the one that will always work is to use .nextLine instead of .nextInt or .nextFloat and then to use the appropriate parsing method. That was one of the solutions mentioned in that question. The only caveat with that solution is exception handling might be verbose but you can write a reusable method to work around that. – Omar Abdel Bari Sep 25 '20 at 04:00