0

problem with Java code.

import java.util.Random;

public class arrayTable {
public static void main (String[] args) {
    System.out.println("Index\t + Value");
    int Array[] = new int[10];

    Random Object = new Random();
    int Values;

    // Assigning random values to each element of array

    for(int i=0; i<Array.length;i++) {
        Values= (1+Object.nextInt(50));
        Array[i] = Values;
    }

    for(int j=0;j<Array.length;j++) {
        System.out.println(j + "\t" + Array[j]);
      }

    }
}

Here with this code i wrote (1+) next to the object so the index should start at 1, however when ever i run the code at always starts at index 0, and it does not matter if i type 2+ or 3+ pr whatever. Could anyone be helpful with pointing out the problem with the code.

thank you in advance.

5
  • 1
    Take care of the Java naming convention. Class names should start with upper case letter, variable names with lower Case letter Commented Aug 17, 2017 at 16:44
  • What's the problem? The values you are storing in your array are all bigger or equal to 1. Commented Aug 17, 2017 at 16:45
  • What means so the index should start at 1,? The index is i and starts as defined in the loop at 0 Commented Aug 17, 2017 at 16:45
  • Also note: i.imgur.com/U6hU1Ac.jpg Commented Aug 17, 2017 at 16:50
  • i'm very aware, that all arrays starts at 0 lol, but i want the index to count from 1 and up, and i believe i added the 1+ the wrong place. And i actully did not know classes should always start with upper case letters and vice versa for varibles? Commented Aug 17, 2017 at 17:00

1 Answer 1

3

i wrote (1+) next to the object so the index should start at 1

You wrote 1+ next to the value not the index!

So, what you were doing was:

array[0] = 50 + 1;

Instead of:

array[0 + 1] = 50;

If you wanted to start from index 1 you should write it here:

Array[i + 1] = Values;

However as you're inside a for loop, you could run into an ArrayIndexOutOfBoundsException, so, a better idea would be:

for(int i=1; i<Array.length;i++) { //Look the "i" was initialized with 1 and not with 0.

REMEMBER: ARRAYS START FROM 0 INDEX

If you want to "skip" the first element, then the above modification to for loop should work, but if you want it to run from 1 to 10 then it's a bad idea, because it should be from 0 to 9

You should also be careful to follow the Java naming conventions:

  • firstWordLowerCaseVariable
  • firstWordLowerCaseMethod()
  • FirstWordUpperCaseClass
  • ALL_WORDS_UPPER_CASE_CONSTANT

and use them consistently, this will make your code easier to read and understand for you and for us.

Also, try not to name your classes / variables as Java classes names:

Object or Array or List, etc might be wrong choices, also having object lowercase would be a bad idea as it's not descriptive either as suggested by @nicomp on the comments below


but when i type Array [i + 1] it still prints out from index 0, if for example i where to make i dice i would want it to start at index 1, is there no way to do this?

I think you didn't changed the for(int j=0;j<Array.length;j++) { loop, to start from 1

To make a dice I would:

  • Create the array with 6 slots (starting from 0)
  • Fill it (1 - 6) like below (inside a for loop):

    dice[0] = 1;
    dice[1] = 2;
    ...
    dice[5] = 6;
    
    //Example of for loop
    for (int i = 0; i < dice.length; i++) {
        dice[i] = i + 1;
    }
    
  • Get a random number (between 0 - 5) called random

  • Get the value of the array at position random

For example:

random = 3;
//dice[random] = 4;
System.out.println(dice[random]);
Sign up to request clarification or add additional context in comments.

4 Comments

If we are piling on, let's not name an object 'Object' or even 'object.'
but when i type Array [i + 1] it still prints out from index 0, if for example i where to make i dice i would want it to start at index 1, is there no way to do this?
@Frakcool I am kept awake at night by the Object class in Java. Makes me crazy.
@nicomp take care, it told me it was gonna haunt you tonight!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.