0

Ok so this is my code:

import java.util.Scanner;
public class Bubble {

    public static void main(String[] args) {
        String prefix;
        int arr[];
        Scanner reader = new Scanner(System.in);
        System.out.println("What size would you like your array to be?");
        int arrSize = reader.nextInt();
        arr = new int[arrSize];
        for(int i = 1; i==arrSize; i++) {
            if((i % 10)==1 && i != 11) {
                prefix = "st";
            }else if((i % 10)==2 && i != 12) {
                prefix = "nd";
            }else if((i % 10)==3 && i != 13) {
                prefix = "rd";
            }else{
                prefix = "th";
            }

            System.out.println("What would you like the"+ i + prefix +"number in the array to be?");
            int arrNum = reader.nextInt();

        }
        System.out.println(arr);
    }

}

Now when I press run I get this:

What size would you like your array to be?

Then I put in an integer, say 3, then I get this:

[I@55f96302

It stays the same no matter what integer i use.

breh
  • 31
  • 7
  • "It stays the same no matter what integer I use." That's because you are not printing an integer. You are printing an array of integers. There's lots more issues with your code - your `for` loop uses wrong indexes, and an incorrect continuation condition. You need to fix them before debugging this problem. – Sergey Kalinichenko Nov 17 '17 at 17:19
  • https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html –  Nov 17 '17 at 17:20
  • you are printing the address in memory and not the values in your array – G2M Nov 17 '17 at 17:44
  • 1
    @G2M It is not an 'address in memory', it is the default `toString()` of an object, which contains the identity hashcode (which in theory might be derived from a memory address, but usually isn't). – Mark Rotteveel Nov 17 '17 at 18:50
  • @MarkRotteveel duely noted :) – G2M Nov 17 '17 at 19:10

1 Answers1

2

Use Arrays.toString(). Here's a great writeup of exactly the issue you're running into. By writing System.out.println(arr); you're printing a default string representation of your array. Check this answer.

As far as I could understand, you're trying to allow a user to create an array with arrSize and after this allow him to put values inside the array. However, your code has some issues:

1 - Your condition in your for loop is never verified for arrSize != 1.

2 - To save the values inside the defined array you should assign the value read from scanner arrNum to the array position i-1 (in order to avoid an ArrayIndexOutOfBoundsException).

3 - DO NOT FORGET to close Scanner. It is very important, since it can lead to memory leaks during the execution of a program if you don't close it.

Below you can find the modifications to your code.

Full Code

import java.util.Arrays;
import java.util.Scanner;
public class Bubble {

    public static void main(String[] args) {
        String prefix;
        int arr[];
        Scanner reader = new Scanner(System.in);
        System.out.println("What size would you like your array to be?");
        int arrSize = reader.nextInt();
        arr = new int[arrSize];
        for(int i = 1; i<=arrSize; i++) {

            if((i % 10)==1 && i != 11) {
                prefix = "st";
            }else if((i % 10)==2 && i != 12) {
                prefix = "nd";
            }else if((i % 10)==3 && i != 13) {
                prefix = "rd";
            }else{
                prefix = "th";
            }

            System.out.println("What would you like the "+ i + prefix +" number in the array to be?");
            int arrNum = reader.nextInt();
            arr[i-1] = arrNum;
        }

        reader.close();        
        System.out.println(Arrays.toString(arr));
    }
}
LuisFerrolho
  • 417
  • 3
  • 13
Drew Wills
  • 8,408
  • 4
  • 29
  • 40
  • I'll try it thank you so much I literally couldn't find it anywhere ive been searching for hours. – breh Nov 17 '17 at 17:25