Cale CWC
Cale CWC

Reputation: 13

Int array returning all zeroes except for one

I am trying use a for loop to make a new array that will return the rightmost digit of every number from a different array, but for some reason it will return the rightmost digit of the last value first, and then it will return a zero for every other value.

Here's my code so far:

import java.util.*;

public class LabU6b {

   public static void main(String[] args) 
   {
      int[] num = new int[10];
      for (int i : num)
      {
        num[i]= (int)(Math.random() * 90 + 10);            
        System.out.println(num[i] + "  ");
      }
      LabU6b temp = new LabU6b();
      temp.rightDigitDuplicator(num);
    }
      
 public static int[] rightDigitDuplicator(int[] num)
   {
    int[] rightNums = new int[10];
    for (int i = 0; i < num.length; i++)
      {
        rightNums[i] = num[i] % 10;
        System.out.println(rightNums[i]);
        
      }
     return rightNums;
   }
}

And here's my output:

14  
18  
47  
80  
41  
13  
20  
13  
81  
87

7
0
0
0
0
0
0
0
0
0

Any help would be greatly appreciated.

Upvotes: 1

Views: 104

Answers (4)

Dhruv Phansalkar
Dhruv Phansalkar

Reputation: 53

Using a for-each loop in the first part of your code which you have used to assign values to num[] is incorrect.

`

for (int i: num)
      {
        num[i]= (int)(Math.random() * 90 + 10);            
        System.out.println(num[i] + "  ");
      }

` Here the 'i' which I think you intended to use as an index is always zero, since the default value of int is zero. Hence the created array is assigned a value at the 0th index for each iteration.

By the same logic, the values you are printing are incorrect since you are not printing the array but the value of num[i] where i is always 0.

Using a simple for loop should fix the problem.

`

for (int i = 0; i < num.length; i++)
      {
        num[i]= (int)(Math.random() * 90 + 10);            
        System.out.println(num[i] + "  ");
      }

`

Upvotes: 0

Piyush Chavan
Piyush Chavan

Reputation: 11

// Using this loop value initialize to 0 index only 
for (int i : num)
      {
        num[i]= (int)(Math.random() * 90 + 10);            
        System.out.println(num[i] + "  ");
      }

// Solution : need to add
for (int i = 0; i < num.length; i++) {// here incrementing index value to initialize to next
      {
        num[i]= (int)(Math.random() * 90 + 10);            
        System.out.println(num[i] + "  ");
      }

Upvotes: 0

link-j
link-j

Reputation: 1

int[] num = new int[10]; //it means that array is [0,0,0,0,0,0,0,0,0,0]

for (int i : num)  //this is different from javascript, it means that every 'i' is 0
                   
//so you can do this
for(int i=0;i<num.length;i++){
   num[i]= (int)(Math.random() * 90 + 10);            
   System.out.println(num[i] + "  ");
}

Upvotes: 0

ernest_k
ernest_k

Reputation: 45309

This code here:

int[] num = new int[10];
for (int i : num) {
    num[i]= (int)(Math.random() * 90 + 10);            
    System.out.println(num[i] + "  ");
}

is looping through all zeros in num (remember, new int[10] creates an array of size 10 filled with zeros, which is the default value for int)

That means num[i]= (int)(Math.random() * 90 + 10); is only setting the 0th element of the array.

To fix this, use a for loop:

for (int i = 0; i < num.length; i++) {
    num[i]= (int)(Math.random() * 90 + 10);            
    System.out.println(num[i] + "  ");
}

Upvotes: 2

Related Questions