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:
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:
Any suggestion why this happens and what should i do? Any help would be appreciated. Thanks before.