0

Can someone look at my code? need help. Trying to define an array without loops

/* The output is a weird number- [I@33909752 , how do i fix this and display the array the user defined by input?**/ package javaapplication111; import java.util.Scanner; public class JavaApplication111 { public static void main(String[] args) { System.out.println("Please enter how many elements are in the Array"); Scanner reply1= new Scanner(System.in); int numbers[]= new int[reply1.nextInt()]; // defining the capacity of the array System.out.println("Enter the first element"); Scanner E0= new Scanner(System.in); numbers[0]=E0.nextInt(); // defining the first element in the array System.out.println("Enter the second element"); Scanner E1= new Scanner(System.in); numbers[1]=E1.nextInt(); // defining the second element in the array System.out.println("Enter the thrid element"); Scanner E2= new Scanner(System.in); //Defining the third element in the array numbers[2]=E2.nextInt(); System.out.println("Enter the fourth element"); Scanner E3= new Scanner(System.in); //Defining the fourth element in the array numbers[3]=E3.nextInt(); System.out.println("Enter the fifth element"); Scanner E4= new Scanner(System.in); numbers[4]=E4.nextInt(); //Defining the fifth element in the array System.out.println(numbers); /* Wanting to print the array using the elements that were defined above**/ } }

19th Mar 2017, 10:31 AM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar
6 ответов
+ 2
Add import java.util.Arrays; under the import of the Scanner class and change System.out.println(numbers); to System.out.println(Arrays.toString(numbers));
19th Mar 2017, 10:33 AM
merkrafter
0
What you get is the array's hash code. You wanna import java.util.Arrays and use the Arrays.toString (numbers) method.
19th Mar 2017, 10:30 AM
merkrafter
0
How do you do that? could you give me an example please
19th Mar 2017, 10:32 AM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar
0
Thank you! i appreciate the help.
19th Mar 2017, 10:38 AM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar
0
Apart from that there are some more improvements you can do: 1. You do not need to create a new Scanner for each input. Just call the nextInt() method on the first one again. 2. After you finished using a Scanner, you should call the close() method. 3. You should use loops for filling an array. You'll get errors if the user inputs a capacity lower than 5. And you'll have many empty entries if it's higher than 5.
19th Mar 2017, 10:42 AM
merkrafter
0
i realized that before programming the actual code lol. I know it will much more efficient with a loop as well.I declared the capacity as 5 instead of asking for user input to determine the capacity. That way, the user will have to enter 5 elements. Also i made the code to run for a specific question, the question has 5 elements, so the user input will not be higher or lesser than 5 elements anyway but yeah i was just trying something new to gain a better understanding. Thanks for the advice.
19th Mar 2017, 5:25 PM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar