I am trying to have the user set the name for how many numbers they set. for example if they set 3 the program asks for name 3 times and then sets those names to a different varible inside an array.
public static void main(String[] args) {
Scanner Num = new Scanner(System.in);
Scanner Name = new Scanner(System.in);
System.out.println("How many names do you want to enter: ");
int number = Num.nextInt();
int[] numbs = new int[number];
for (int i = 0; i < numbs.length; i ) {
System.out.println("What is your name");
String [] nameArray = Name.nextLine();
CodePudding user response:
Starting point
Here are several things happening in your posted code:
- You have two different Scanners, each reading from System.in
- After prompting for user input (
int number = Num.nextInt()
), you're using that number to create an array of size "number" - When you call nextLine(), you are assigning the result to a string array (
String []
).
Working solution
Here's a variation which addresses those issues, with a few additions, too:
- Use one Scanner, and give it a general name ("scanner", not "Num") since a scanner has nothing to do with one specific data type. Any scanner can read strings, integers, booleans, bytes, etc. Look in Javadoc for the full set of supported data types.
- Check if user input is valid before trying to create an array – if user enters "-1" that's a valid integer value, but not valid for allocating an array –
new int[-1]
) would throw a java.lang.NegativeArraySizeException at runtime. - Use "next()" instead of "nextLine()" – with the rest of your example, using "nextLine()" results in skipping one line without direct interaction from the user (so the first "name" is always an empty string)
- Assign the result of "next()" to a String (not
String[]
), matching the return type from the method - Use "System.out.print()" instead of "println()", a little tidier program output
- Use lowercase names to follow Java naming conventions ("scanner", not "Scanner")
Scanner scanner = new Scanner(System.in);
System.out.print("How many names do you want to enter: ");
int times = scanner.nextInt();
if (times < 0) {
System.out.println("negative numbers not allowed");
} else {
String[] names = new String[times];
for (int i = 0; i < times; i ) {
System.out.print("What is your name: ");
names[i] = scanner.next();
}
System.out.println(Arrays.toString(names));
}
And here's a sample run:
How many names do you want to enter: 3
What is your name: one
What is your name: two
What is your name: three
[one, two, three]
CodePudding user response:
Create a String array of number length outside the loop and initialize each index with name as input.
By the way you only need to create one Scanner object for input(Not needed to create various objects for different input).
Scanner sc=new Scanner(System.in);
System.out.println("How many names do you want to enter: ");
int number = sc.nextInt();
int[] numbs = new int[number];
String []nameArray=new String[number];
for (int i = 0; i < numbs.length; i ) {
System.out.println("What is your name");
nameArray[i]=sc.nextLine();
}
CodePudding user response:
Just a small improvement over Titan's answer.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("How many names do you want to enter: ");
int numberOfTimes = sc.nextInt();
String[] names = new String[numberOfTimes];
sc.nextLine();
for (int i = 0; i < numberOfTimes; i ) {
System.out.println("What is your name");
names[i] = sc.nextLine();
}
System.out.println(Arrays.toString(names));
}