0

So I'm doing some random practice for an upcoming exam, and I don't know if it's the fact that I've been reviewing for hours and my brain isn't functioning, or something in this code is wrong.

I'm attempting to make a very simple java program that asks the user for the amount of numbers they wish to enter (totalNum), create an array that long, and then ask the user for each individual value. After it asks the user for each value in the array, it prints the array.

Here is my code:

import java.util.Scanner;
public class Practice1 {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.println("How many numbers would you like to store?");
    int totalNum = s.nextInt();

    int[] numbers= new int[totalNum];


    for (int i = 0; i>totalNum; i++) {
        System.out.println("Number" + i + " :");
        numbers[i] = s.nextInt();
        i++;
    }
    numbers.toString();
    System.out.println(numbers);


}
}

When I run it it asks the user for the numbers I want to store, then prints [I@33909752 and stops. I've done dozens of programs like this and for the life of me I can't figure out where I went wrong.

Any help would be appreciated, thanks!

Blake
  • 91
  • 6
  • 1
    This is also a duplicate of [this question](http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4), which is about the weird output more generally. The answer to that has good reading which you should probably make sure you understand (maybe after your exam, when you're less stressed :) ). But for your immediate problem of printing out the array, the question I linked to is probably more relevant. – yshavit Mar 30 '16 at 04:23
  • Also, I think you have a bug in `for (int i = 0; i>totalNum; i++)`. Think about the values of `i` and `totalNum` the first time through this loop; how many times do you expect the loop to iterate if, say, totalNum = 5? – yshavit Mar 30 '16 at 04:25

3 Answers3

2

Your loop test is backwards. This

for (int i = 0; i>totalNum; i++) {

should be

for (int i = 0; i < totalNum; i++) {

as is, the test evaluates to false and the loop isn't entered. And, don't increment i in the loop body (that's what i++ does in the for). Finally,

System.out.println(numbers);

isn't going to print the array correctly, because arrays don't override Object.toString(). You can use Arrays.toString like

System.out.println(Arrays.toString(numbers));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • right, Ive fixed that X.X. However the program still isn't running properly. Say I say i would like to store 3 numbers. it should ask me for 3 values ( [0-2]). It asks me for value 0, then value 2, then stops. Why is it jumping i two values? – Blake Mar 30 '16 at 04:26
  • You left that `i++` in the loop body. Remove that. Because you already increment `i` as part of the `for`. – Elliott Frisch Mar 30 '16 at 04:27
1

i>totalNum is the problem. The for loop will not execute even once.

The for loop has three parts:

  1. The action to perform before starting the loop
  2. The condition
  3. The action to perform after each loop

Your condition is i>totalNum, which is false for i=0 and totalNum=1. The loop won't execute even once.

The i++ is already mentioned in the loop, you do not need to include it in the loop body anymore.

The unexpected output is the caused by the default toString()-method of Array. Use Arrays.toString() for a readable output.

slartidan
  • 20,403
  • 15
  • 83
  • 131
0

Your loop condition should be

for (int i = 0; i<totalNum; i++) {

and within loop don't increment variable i

use below for your desired result.

public class Practice1 {
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.println("How many numbers would you like to store?");
    int totalNum = s.nextInt();

    int[] numbers= new int[totalNum];

    for (int i = 0; i<totalNum; i++) {
        System.out.println("Number" + i + " :");
        numbers[i] = s.nextInt();
        i++;    //remove this
    }
    numbers.toString();
    System.out.println(Arrays.toString(numbers));


}
}