1

Anybody got any idea on how I can check if an array indexes(not just one index) are empty and if is empty or zero put a value there. And also if all the indexes are all not empty print an error.

Sorry unfortunately I can't give rep.

import java.util.Scanner; 
public class Myhash {
/**
 * @param args
 */
public static void main(String[] args) {
 // TODO Auto-generated method stub
 int [] myData = new int[17]; 
 int [] newData = new int [17];
 System.out.println("Please enter 16 integer numbers");
 for(int i= 0; i<myData.length; i++){
 //System.out.print("Please enter 16 numbers");
 Scanner input = new Scanner(System.in);
 int data =input.nextInt(); 
 myData[i]=data; 
 int num3 = data % 17;
 newData[num3]=data; 
 } 
 System.out.println("These are the numbers you entered\n"); 
 System.out.printf("%s%8s \n", "Index", "Value"); 
 for(int t=0; t<myData.length; t++){
 System.out.printf("%5d%8d\n", t, myData[t]);
 }
 System.out.println("\n");
 System.out.println("The Hash Function:\n\n");
 System.out.printf("%5s%8s \n", "Index", "Value"); 
 for(int s=0; s<newData.length; s++){
 System.out.printf("%5d%8d\n", s, newData[s]);
 } 
 }
}

on here:

 for(int s=0; s<newData.length; s++){
 System.out.printf("%5d%8d\n", s, newData[s]);
 }

how do check for more than one index(if empty? If the index is empty how do check for the next index and if that one is not empty how do i check the next one, etc?

asked Mar 20, 2012 at 10:51
5
  • Also, what is the array storing? Strings, integers etc? Commented Mar 20, 2012 at 10:54
  • in java is an int. I only know how to check for one index Commented Mar 20, 2012 at 10:55
  • 1
    Uh, what does it mean for an index to be "empty"??? Commented Mar 20, 2012 at 11:28
  • 1
    arrays of int by default has all zero values. So if element is zero it has not been set or it has been set to zero :) Commented Mar 20, 2012 at 11:35
  • I can't figure this out: If the index is empty how do check for the next index and if that one is not empty how do i check the next one, etc? as ajozwik said, if is zero then is considered empty because is an int Commented Mar 20, 2012 at 11:48

2 Answers 2

11

Elements in primitive arrays can't be empty. They'll always get initialized to something

If you declare the array like so

 int [] newData = new int [17];

then all of the elements will default to zero.

For checking if an element is not entered, you can use a simple loop :

 for(int i=0;i<newData.length;i++)
 {
 if(newData[i]==0)
 System.out.println("The value at " + i + "is empty");
 }

Although , the above code will not work in your case, because the user might enter 0 as an input value and still this code will consider it to be empty.

What you can do is, initialize the array with all values as -1, and specify at the input prompt that only values>=0 can be entered . The initialization can be done like this:

int[] newData = new int[17];
for(int i=0;i<newData.length;i++)
{
 newData[i]= -1; 
}

Then you can ask the user for input and do the processing. Then you can use this:

for(int i=0;i<newData.length;i++)
 {
 if(newData[i]==-1)
 System.out.println("The value at " + i + "is empty");
 }
answered Mar 20, 2012 at 21:22
2

Here:

for(int i = 0; i < array.length; i++)
{
 if(array[i] == null)
 continue;
}
answered Mar 20, 2012 at 12:03
1
  • 3
    You have to explain more. This is basically a code-only answer. Commented Jun 12, 2013 at 4:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.