Array Display

Now serving with Compile Errors. Yum!

Page 1 of 1

4 Replies - 7375 Views - Last Post: 25 January 2009 - 01:15 PM Rate Topic: -----

#1 crummydo  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 06-January 09

Array Display

Post icon  Posted 24 January 2009 - 02:44 PM

Okay, so after I fixeda tiny error and did some other editing. I ran and started to see some action, but I got a dump at the end. NumberKeeper class is still the same. Just the application was changed, but I will post it just in case

What is supposed to happen is after I input 10 random number, the program is supposed to print out those 10 numbers from the array, excluding a duplicate and numbers less than the number 10. But all I get is an error after last number is entered. What is up?

NumberKeeper
public class NumberKeeper
{
	private int _numbers[];
	private int _currentPos;

	public NumberKeeper()
	{
		_currentPos = 0;
		_numbers = new int[10];
		for(int i =0; i < 10; i++)
		{
			_numbers[i] = 0;
		}
	}

	public void acceptNumber (int value)
	{
		boolean found = false;
		//determine if number is in array
		for(int i = 0; i < 10; i++)
		{
			if (_numbers[i] == value)
				found = true;
		}

		//if it is NOT then we add to array
		if (!found)
		{
			//add number to array
			_numbers[_currentPos] = value;
			_currentPos = _currentPos + 1;
		}
	}

	public void printNumbers()
	{
		for(int i = 0; i < 10; i++)
		{
			if (_numbers[i] > 0)
				System.out.println(_numbers[i]);
		}
	}

}



NumberApplication
import java.util.*;

public class NumberApplication
{

	public static void main(String[] args)
	{
		//declare variables
		NumberKeeper myNK = new NumberKeeper();  //this is the line with error
		Scanner input = new Scanner(System.in);
		int userNum;

		for(int i = 0; i < 10; i++)
		{
			// get user input
			System.out.println("Please input a number between 10 and 100:");
			userNum = input.nextInt();

			myNK.acceptNumber(userNum);
			
			//sent input to the acceptnumber method of numberkeeper
			myNK.acceptNumber(i);
		}
		//display the nubmers entered
		myNK.printNumbers();
	}
}



Error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 

		at NumberKeeper.acceptNumber(NumberKeeper.java:34)
		at NumberApplication.main(NumberApplication.java:22)
Java Result: 1
BUILD SUCCESSFUL (total time: 15 seconds)



Is This A Good Question/Topic? 0
  • +

Replies To: Array Display

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Array Display

Posted 24 January 2009 - 03:22 PM

am too lazy to trace this, but i have a tip for you, better to write a different method to check if the element already exists in the array.
public static boolean contains(int [] a, int n)


searches if the element exists in the array, and also get rid of that currentelement variable.. and when adding numbers to the array, you can do this
for(int i = 0; i < array.length; i++)
{
	 int n = sc.nextInt();
	 if(!contains(array, n))
		 a[i] = n;
}


hope this helps

This post has been edited by mostyfriedman: 24 January 2009 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

#3 crummydo  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 06-January 09

Re: Array Display

Posted 24 January 2009 - 03:40 PM

Im just doing begining Java. That seems a bit complex to me. can you put comments in it to explain what each line means?

View Postmostyfriedman, on 24 Jan, 2009 - 02:22 PM, said:

am too lazy to trace this, but i have a tip for you, better to write a different method to check if the element already exists in the array.
public static boolean contains(int [] a, int n)


searches if the element exists in the array, and also get rid of that currentelement variable.. and when adding numbers to the array, you can do this
for(int i = 0; i < array.length; i++)
{
	 int n = sc.nextInt();
	 if(!contains(array, n))
		 a[i] = n;
}


hope this helps

Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Array Display

Posted 24 January 2009 - 03:48 PM

sure
for(int i = 0; i < array.length; i++)//loop through the array to input values
{
	 int n = sc.nextInt(); //integer input from the user
	 if(!contains(array, n))//checks if the integer is not in the array
		 a[i] = n;
}


as for the contains method, it will be a static boolean methods that takes an array and an integer in the arguments..what the method will do is search for that integer in the array and if its there it will return true..i hope you understood what i mean
Was This Post Helpful? 0
  • +
  • -

#5 crummydo  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 06-January 09

Re: Array Display

Posted 25 January 2009 - 01:15 PM

The code I have already does something like that. I am mostly concerned with trying to figure out the compiling errors that I am getting. The program runs and gathers the 10 numbers into the array, but when it goes back to display, I get two erros. One for each class. Thats what I need to figure out first before I go on to solving for numbers less than 10 or greater than 100.

View Postmostyfriedman, on 24 Jan, 2009 - 02:48 PM, said:

sure
for(int i = 0; i < array.length; i++)//loop through the array to input values
{
	 int n = sc.nextInt(); //integer input from the user
	 if(!contains(array, n))//checks if the integer is not in the array
		 a[i] = n;
}


as for the contains method, it will be a static boolean methods that takes an array and an integer in the arguments..what the method will do is search for that integer in the array and if its there it will return true..i hope you understood what i mean

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1