i created my problem and I don't have any solution, HEEELLPP | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

i created my problem and I don't have any solution, HEEELLPP

Hello, recently i just created my own problem and i cannot solve where i need to put a number to register a specific numbers of people. -> What i am trying to do is... i am going to input a specific number, example 5. So lets say i will register 5 person in the code below. then what i want to do is to printout those 5 names after the loop turns into false. The problem is i can't declare(im not sure for the term) the variable inside the loop to printout those names. Or maybe i need to make variable names[] to arry so that the names will printout? Thank you ❤️ Ps: more exmple arry pls import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int howMany; System.out.print("Input number on how many people will be register: "); howMany = input.nextInt(); String names[] = new String[howMany]; int x = 0; while (x <= howMany) { String name = input.nextLine(); System.out.print("Enter name: "); x++; } } }

5th Jan 2021, 9:47 PM
Kakai
Kakai - avatar
2 Answers
+ 2
1. your loop is correct but it should stop at howMany-1 however it would be better to use a for loop. 2. you must add the name to your array: names[x] = name; or directly with: names[x] = input.nextLine(); //and remove String name = input.nextLine(); 3. you may then use an other loop to print those names out (with names[x] again).
5th Jan 2021, 10:26 PM
John Robotane
John Robotane - avatar
0
Ps: is my loop correct? Or i am using wrong loop? Or do i need to use do while loop or for loop? I'm still confused between those 3 loops 😆
5th Jan 2021, 9:52 PM
Kakai
Kakai - avatar