Hey so I have create a class FunWithIntegers. Add a method to the class called GreaterThanFive. Have that method accept an array of int. The method should allocate a new array that contains only the values from the first array that are greater than 5 and return it.

public class FunWithIntegers 
{
	private static int counter;
	public FunWithIntegers()
	{
		counter = 0;
	}
	
	public static int[] GreaterThanFive(int[] values)
	{
		int[] vals = new int[values.length];
		for(counter = 0; counter < values.length; counter++)
		{
			for(int j = 0; j < counter; j++)
			{
				if(values[j] > 5)
				{
					vals[counter] = values[j];
				}
			}
		}		
		return vals;
	}
	
	public int m()
	{
		return counter;
	}
}
public class IntegerTester 
{
	public static void main(String[] args)
	{
		FunWithIntegers fun = new FunWithIntegers();
		/*int[] values = new int[6];
		int[] greater = fun.GreaterThanFive(values);

		for(int i = 0; i < values.length; i++)
			  System.out.println(values[i]);
		*/
		int[] values2 = {1, 7, 9, 10};
		int[] greater2 = fun.GreaterThanFive(values2);
		
		int[] values3 = {5, 5, 5, 150};
		int[] greater3 = fun.GreaterThanFive(values2);
		
		
		for(int i = 0; i < values2.length; i++){
			System.out.println(greater2[i]);
		}
		for(int j = 0; j < values3.length; j++){
			System.out.println(greater3[j]);
		}


		//System.out.println(fun.m());
	}
}

I keep getting this:

0
0
7
9
0
0
7
9


But I want to get this:
7
9
10
150

it doesn't matter if there are 0's instead of other numbers.
and I can't change any of the method, I need to used nested loop.

tell me what I should do

Basically debug your code, find the error(s) and correct them.
One technique for debugging code is to add println statements that print out the values of the variables as they are changed and used. The print out will show you where the code is doing differently than you thought.
Some times you have to add lots of printlns to see what is happening. Be sure to put unique id Strings on the printouts so you know where they were printed:
System.out.println("var1=" + var1);

Don't just print a number without a label like this statement does:
System.out.println(greater3[j]);

Another useful tool for debugging arrays is the Arrays.toString method:

System.out.println("G2=" + java.util.Arrays.toString(greater2));

Edited 4 Years Ago by NormR1: n/a

int[] vals = new int[values.length];

using this line, you make sure vals is as long as your values array is, but you don't know whether you'll have the same amount of elements.

first: you don't need that counter variable, you don't need your constructor and you don't need that m() method.
Also, you don't need to instantiate an Object of type FunWithIntegers: since your method is static, you can call it like this:

int[] newArray = FunWithIntegers.GreaterThanFive(oldRow);

you also don't need two nested for loops, that's a bit overkill

step 1
inititalize vals as null, since it is an array, it's an object, so this is no problem.
step 2:
in your for loop,
if element > 5
=> if vals = null
=> declare vals as a new array with one element
=> else
=> declare vals as a new array with one elment more than it has now, and store the elements it already had on the same place.
you'll need a temporary array to do this though

when done: return vals

also:

for(int i = 0; i < values2.length; i++){
			System.out.println(greater2[i]);
		}

this will get you into trouble.
since you won't copy the elements that are smaller than 5, your greater2 array will not be as big as the values2 array, so this will lead to an ArrayIndexOutOfBoundsException

I got that one.
But I have to do the same thing using Integer:

public static Integer[] greaterThanFive(Integer[] array)
	{
			ArrayList<Integer> al = new ArrayList<Integer>();
			for(Integer i = 0; i < array.length; ++i) {
				if(array[i] > 5)
					al.add(array[i]);
			}
			Integer[] result = new Integer[al.size()];
			for(Integer i = 0; i < al.size(); ++i)
				result[i] = al.get(i);
			return result;
	}
Integer[] val = new Integer[6];
		Integer[] great = fun.greaterThanFive(val);
		for(Integer i = 0; i < val.length; i++)
		{
			values[i] = r.nextInt(Integer.MAX_VALUE);
			great = fun.greaterThanFive(val);
			System.out.println(great[i]);
		}
		
		Integer[] val2 = {1, 7, 9, 10};
		Integer[] great2 = fun.greaterThanFive(val2);
		for(Integer i = 0; i < val2.length; i++){
			System.out.println(great2[i]);
		}

I am getting this error:
Exception in thread "main" java.lang.NullPointerException
at FunWithIntegers.greaterThanFive(FunWithIntegers.java:27)
at IntegerTester.main(IntegerTester.java:28)
any idea of what I am doing wrong.

And also I can't use arraylist, I tried that but forgot to delete it from the code

you can use ArrayList, or do you mean you're not allowed to?
your error message says there's a problem on the 27th line of your program, since I don't knw what line that is in your code, it's difficult to say.

but, it's clear that you are somewhere trying to use a value, maybe calling a mehtod from a non-instantiated object.

I am not allowed to.
(FunWithIntegers.java:27) = if(array > 5)
(IntegerTester.java:28) = Integer[] great = fun.greaterThanFive(val);

also I updated the first code(not the Integer one):

static int[] greater5Array(int[] array) {
	int nb = 0;
	for(int i = 0; i < array.length; ++i) {
		if(array[i] > 5)
			++nb;
	}
	int[] result = new int[nb];
	int count = 0;
	for(int i = 0; i < array.length; ++i) {
		if(array[i] > 5)
			result[count++] = array[i];
	}
	return result;
}

and as you said before "since you won't copy the elements that are smaller than 5, your greater2 array will not be as big as the values2 array, so this will lead to an ArrayIndexOutOfBoundsException" I get that error. any idea of solving it. because the one that I did printed 0 for the number less than 5 instead of printing nothing.

the one that I did before the upper one:

int counter = 0;
		int[] vals = new int[values.length];
		for(counter = 0; counter < values.length; counter++)
		{
			if(values[counter] > 5)
			{
				vals[counter] = values[counter];
			}
		}	
		return vals;

Can you post the full code now that you have changed it to use ArrayList and Integer?

Your array names are not clear in meaning and usage.
What is the difference between val and values?
Something like inputVals and selectedVals would be clearer

Edited 4 Years Ago by NormR1: n/a

here is the full code:

import java.util.ArrayList;

public class FunWithIntegers 
{
	public FunWithIntegers()
	{
	}
	
	public static int[] GreaterThanFive(int[] values)
	{
		int counter = 0;
		int[] vals = new int[values.length];
		for(counter = 0; counter < values.length; counter++)
		{
			if(values[counter] > 5)
			{
				vals[counter] = values[counter];
			}
		}	
		return vals;
	}
	
	public static Integer[] greaterThanFiveArray(Integer[] array)
	{
			Integer[] al = new Integer[array.length];
			for(Integer i = 0; i < array.length; ++i) {
				if(array[i] > 5)
					al[i] = array[i];
			}
			Integer[] result = new Integer[al.length];
			for(Integer i = 0; i < al.length; ++i)
				result[i] = al[i];
			return result;
	}
}

tester:

import java.util.*;
public class IntegerTester 
{
	public static void main(String[] args)
	{
		FunWithIntegers fun = new FunWithIntegers();
		Random r = new Random();
		int[] values = new int[6];
		int[] greater = fun.GreaterThanFive(values);
		for(int i = 0; i < values.length; i++)
		{
			values[i] = r.nextInt(Integer.MAX_VALUE);
			greater = fun.GreaterThanFive(values);
			System.out.println(greater[i]);
		}
		
		int[] values2 = {1, 7, 9, 10};
		int[] greater2 = fun.GreaterThanFive(values2);
		for(int j = 0; j < values2.length; j++)
		{
			System.out.println(greater2[j]);
		}
		
		
		
		
		Integer[] val = new Integer[6];
		Integer[] great = fun.greaterThanFiveArray(val);
		for(Integer i = 0; i < val.length; i++)
		{
			values[i] = r.nextInt(Integer.MAX_VALUE);
			great = fun.greaterThanFiveArray(val);
			System.out.println(great[i]);
		}
		
		Integer[] val2 = {1, 7, 9, 10};
		Integer[] great2 = fun.greaterThanFiveArray(val2);
		for(Integer i = 0; i < val2.length; i++){
			System.out.println(great2[i]);
		}
	}
}

I am getting the null pointer exception error for the greaterthanfivearray method

I am getting the null pointer exception error for the greaterthanfivearray method

Object arrays are different than arrays of primitives. The array of primitives like ints get a default value of 0. Arrays of objects, like Integer, get a null default value.
If you have not assigned any Integer objects to the array its contents will be null values.
Check that you have assigned an object to all the slots in the array before you try to reference its value.

What is your design/logic method you are trying to code?
What is the method supposed to return?
List the steps the code should go through to do the task it is supposed to do.

the tester for it is this:

Integer[] val = new Integer[6];
		Integer[] great = fun.greaterThanFiveArray(val);
		for(Integer i = 0; i < val.length; i++)
		{
			values[i] = r.nextInt(Integer.MAX_VALUE);
			great = fun.greaterThanFiveArray(val);
			System.out.println(great[i]);
		}
		
		Integer[] val2 = {1, 7, 9, 10};
		Integer[] great2 = fun.greaterThanFiveArray(val2);
		for(Integer i = 0; i < val2.length; i++){
			System.out.println(great2[i]);
		}

and it is supposed to return this:

a random number
a random number
a random number
a random number
a random number
a random number
7
8
10

the first six are random number so if it is less than 5 it shouldn't be printed..
the code should be similar to the greaterthanfive method.
I am supposed to add a to my class that does exactly the same thing, except accepts an array of Integer and returns an array of Integer. Copy the above tester code and modify it to be all Integer[] instead of int[].

and it is supposed to return this:

What does it return?

For testing you should get the code to work with a fixed, defined array. When that works then use random numbers.
Don't test with random numbers until AFTER it works with several different fixed arrays.

Edited 4 Years Ago by NormR1: n/a

I did like you said and tested it with the code given below and it works it prints:

Integer[] val2 = {1, 7, 9, 10};
		Integer[] great2 = fun.greaterThanFiveArray(val2);
		for(Integer i = 0; i < val2.length; i++){
			System.out.println(great2[i]);
		}

null
7
9
10

but when I try the same with random number it prints an error:

Integer[] val = new Integer[6];
		Integer[] great = fun.greaterThanFiveArray(val);
		for(Integer i = 0; i < val.length; i++)
		{
			values[i] = r.nextInt(Integer.MAX_VALUE);
			great = fun.greaterThanFiveArray(val);
			System.out.println(great[i]);
		}

error message:
Exception in thread "main" java.lang.NullPointerException
at FunWithIntegers.greaterThanFiveArray(FunWithIntegers.java:27)
at IntegerTester.main(IntegerTester.java:28)

note: for checking the line number (example: java:28) check the full code given above.

What variable is null? Add a println to show the value of all the variables used on the line where the exception occurs.

Where do you put any elements into the val array?

numbers used = 1,7,9,10
1 is printed as null

when I try printing with random numbers the error occurs it doesn't print anything other than the error. I just want to that is there something wrong with my tester class code or the other class code?

null is the default value for an empty slot in an array of objects. Integers are objects.
If the array were int, the value would be 0.

You can not use a null slot in an array for a reference. If your code must have empty slots, the test if the contents of the slot is null before trying to print it.

What is the line of code where the exception occurs?

Edited 4 Years Ago by NormR1: n/a

Integer[] great = fun.greaterThanFiveArray(val);
is the line the exception occurs

Integer[] great = fun.greaterThanFiveArray(val);

Did you print out the values of the variables?
The only variable on that line that will cause a NullPointerException is fun.
Is fun null?

Exception in thread "main" java.lang.NullPointerException
at FunWithIntegers.greaterThanFiveArray(FunWithIntegers.java:27)
at IntegerTester.main(IntegerTester.java:28)

The trace of the stack from the error says the error occurred on line 27 in the FunWithIntegers class in the greaterThanFiveArray method. What line is there?

Edited 4 Years Ago by NormR1: n/a

public static int[] GreaterThanFive(int[] values)
	{
		int counter = 0;
		int[] vals = new int[values.length];
		for(counter = 0; counter < values.length; counter++)
		{
			if(values[counter] > 5)
			{
				vals[counter] = values[counter];
			}
		}	
		return vals;
	}

Isn't correct. You need something like this :

public static int[] GreaterThanFive(int[] values)
	{
		int[] vals = new int[values.length];
                int resultIndex =0 ;
		for(int counter = 0; counter < values.length; counter++)
		{
			if(values[counter] > 5)
			{
				vals[resultIndex++] = values[counter];
			}
		}	
		return vals;
	}

But there is also one problem with the above, the problem is that vals.length will be the same as values.length but vals might not contain all values greater than 5. So you might want to trim it.

no, he doesn't need to trim anything.
I already told him in my first post a way to counter this, and how to make sure he doesn't get a
0 (for int)
or a
null (for Integers)
in his array

I'll quote it here, so he can find it without scrolling back :)

int[] vals = new int[values.length];

using this line, you make sure vals is as long as your values array is, but you don't know whether you'll have the same amount of elements.

first: you don't need that counter variable, you don't need your constructor and you don't need that m() method.
Also, you don't need to instantiate an Object of type FunWithIntegers: since your method is static, you can call it like this:

int[] newArray = FunWithIntegers.GreaterThanFive(oldRow);

you also don't need two nested for loops, that's a bit overkill

step 1
inititalize vals as null, since it is an array, it's an object, so this is no problem.
step 2:
in your for loop,
if element > 5
=> if vals = null
=> declare vals as a new array with one element
=> else
=> declare vals as a new array with one elment more than it has now, and store the elements it already had on the same place.
you'll need a temporary array to do this though

when done: return vals

also:

for(int i = 0; i < values2.length; i++){
			System.out.println(greater2[i]);
		}

this will get you into trouble.
since you won't copy the elements that are smaller than 5, your greater2 array will not be as big as the values2 array, so this will lead to an ArrayIndexOutOfBoundsException

Edited 4 Years Ago by stultuske: deleted a double quote

This article has been dead for over six months. Start a new discussion instead.