10 Replies - 425 Views - Last Post: 01 August 2013 - 09:12 AM Rate Topic: -----

#1 manuelma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-July 13

Error with inputs into an array, then printing them.

Posted 31 July 2013 - 01:19 PM

Hey y'all,

Currently I am writing a program to collect information for a real estate database. My only issue is this: On commercial properties, the user has the option to enter as many years of income as they'd like, but I only want to print the most recent 5. According to my professor, "as each new value is added, the oldest value will be discarded.

I have this:

		System.out.println("How many years do you want to enter income for?");
		incomeYears = in.nextInt();
		for (int i = 0; i < incomeYears; i +=1)
		{
			System.out.println("What was the yearly income for the year you would like to enter? Starting from most recent:");
			comProp[incomeYears] = in.nextFloat();
		}
		



But for whatever reason, as soon as the user enters a value after they've chosen how many years they're inputting for, I get an ArrayIndexOutOfBoundsException even though I set my array to the size of the years (through incomeYears).

Then with printing it, I'm not sure how to print the last 5.

I figure it would be either involve replacing the value in the array with the next with a

comProp [5] = comProp [4];
[4] = [3];

//Currently it's just:
System.out.println("The last 5 years of income: " + Arrays.toString(comProp));



etc.

but I'm not exactly sure how that'd work. That or printing the array for some value of comProp[incomeYears-5]?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Error with inputs into an array, then printing them.

#2 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 433
  • View blog
  • Posts: 2,998
  • Joined: 12-January 10

Re: Error with inputs into an array, then printing them.

Posted 31 July 2013 - 01:34 PM

i +=1


shouldnt this be incrementing the loop

i++

Was This Post Helpful? 0
  • +
  • -

#3 axel1994  Icon User is offline

  • D.I.C Head

Reputation: 83
  • View blog
  • Posts: 167
  • Joined: 27-May 13

Re: Error with inputs into an array, then printing them.

Posted 31 July 2013 - 01:40 PM

Alright.

This line:
comProp[incomeYears] = in.nextFloat();

is wrong.

You put each value at the index of incomeYears.
So every other element in your list won't be filled.

Your for loop starts at 0 and goes to incomeYears. (it loops through the positions you want to fill)

Next:
You probable initialised your comProp as following:
float[] comProp = new float[incomeYears];


Or something similar.
What this line does is create a list of elements that is exactly incomeYears long.
But a list always starts at index 0.
So a list of 5 elements will end at index 4.

That's why you get the error. (you try to place an element at the index equal to the length.

Finally:
If you only want to show a list of 5 elements.
Why not just create a list of length 5, and fill the list until it is full and do nothing with other entries (simple if statement).
Was This Post Helpful? 0
  • +
  • -

#4 ConciselyVerbose  Icon User is offline

  • D.I.C Regular

Reputation: 90
  • View blog
  • Posts: 315
  • Joined: 05-July 13

Re: Error with inputs into an array, then printing them.

Posted 31 July 2013 - 01:49 PM

For your last suggestion, he wants the most recent entries, not the first ones. I would probably use an array of the desired length, and do something more like:

int index =0;
while(!endCondition){
    comProp[index]=in.nextFloat;
    index =(index+1) % 5
}


While you could do your for loop and just ignore the first x inputs, this is more flexible and probably closer to what was intended with the assignment.

This post has been edited by ConciselyVerbose: 31 July 2013 - 01:52 PM

Was This Post Helpful? 0
  • +
  • -

#5 hax95  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 8
  • Joined: 31-July 13

Re: Error with inputs into an array, then printing them.

Posted 31 July 2013 - 02:05 PM

you can also use a for loop with an index that start at incomeYears-6, and loop up to incomeYears
//create another array
float[] lastFife = new float[5];
i=0;
for(int k=incomeYears-6;k<incomeYears;k++){
//here you add the incomeYears[k] element in the lastFive[i] array
i++
}

//finally, you print out the result
System.out.println("The last 5 years of income: " + Arrays.toString(lastFive));


Was This Post Helpful? 0
  • +
  • -

#6 manuelma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-July 13

Re: Error with inputs into an array, then printing them.

Posted 31 July 2013 - 08:51 PM

View PostConciselyVerbose, on 31 July 2013 - 01:49 PM, said:

For your last suggestion, he wants the most recent entries, not the first ones. I would probably use an array of the desired length, and do something more like:

int index =0;
while(!endCondition){
    comProp[index]=in.nextFloat;
    index =(index+1) % 5
}


While you could do your for loop and just ignore the first x inputs, this is more flexible and probably closer to what was intended with the assignment.


Hey,

I decided to go with your method because it made the most sense to me, although I changed things slightly. I understand how it works, in terms of letting me input until the number remainder is 5, but what seems to happen is that whatever input I give it will override the last element entered in the array, which is the only one, as index is set to 0... if I understand that correctly. So when I print it, it only prints 5 elements, but the only element printed is the latest entry. I've been trying a lot of things like changing the number of the index, adding another if loop with the boolean (kind of like doing a bubblesort) but to no avail. Any tips/help?

Here's my code for everyone to see:

boolean endCondition = true;
		int index =0;
		while(endCondition == true)
			{
				endCondition = false;
				for (int i = 0; i < incomeYears; i +=1)
				{
				System.out.println("Enter the income:");
			    comProp[index]=in.nextFloat();
			    index =(index+1) % 5;
			    endCondition = true;
				}
			}



So, to clarify, if I enter 6 years, enter, in this order: 1, 2, 3, 4, 5, 6 for each successive year, the only element printed from the array is 6, and the others are printed as 0.
Was This Post Helpful? 0
  • +
  • -

#7 ConciselyVerbose  Icon User is offline

  • D.I.C Regular

Reputation: 90
  • View blog
  • Posts: 315
  • Joined: 05-July 13

Re: Error with inputs into an array, then printing them.

Posted 31 July 2013 - 08:57 PM

If you are doing a for loop you would not need to put it in a while loop, especially seeing as your while loop appears to never end. What code are your using to print your array, and what are you initializing your array as?
Was This Post Helpful? 0
  • +
  • -

#8 manuelma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-July 13

Re: Error with inputs into an array, then printing them.

Posted 31 July 2013 - 09:20 PM

View PostConciselyVerbose, on 31 July 2013 - 08:57 PM, said:

If you are doing a for loop you would not need to put it in a while loop, especially seeing as your while loop appears to never end. What code are your using to print your array, and what are you initializing your array as?

I',m, initializing it as:
	public float comProp[]  = new float [5];



and printing it as:

	System.out.println("The last 5 years of income: " + Arrays.toString(comProp));



which is part of a display method I made.

Why do you say I wouldn't need the for loop? Isn't that serving as the counter for how many times it'll be asking the income? Sorry, if I'm horribly off, I'm painfully new to Java.
Was This Post Helpful? 0
  • +
  • -

#9 ConciselyVerbose  Icon User is offline

  • D.I.C Regular

Reputation: 90
  • View blog
  • Posts: 315
  • Joined: 05-July 13

Re: Error with inputs into an array, then printing them.

Posted 31 July 2013 - 09:30 PM

You only need one or the other. The only reason I used while is because that way you could quickly modify it to take, say, an empty string as the signal to stop accepting input instead of just a count. If you do a for loop you do not need to put it inside of a while loop.
Was This Post Helpful? 0
  • +
  • -

#10 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 135
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Error with inputs into an array, then printing them.

Posted 01 August 2013 - 07:17 AM

What I did here was reverse the array so the last 5 entries are now the first 5 entries. If the income years is greater then 5 we copy those last 5 entries to the new array. If not we just print out the original array. Take a look at the way we reverse the array.

    private void reverseArray() {
        for(int i = 0; i < num/2; i++) {
            temp = arr[i];
            arr[i] = arr[num-1-i];
            arr[num-1-i] = temp;
        }
    }



import java.util.*;

public class Test2 {
    
    private int num;
    private int temp;
    private int[] arr;
    private int[] newArr = new int[5];
    
    Scanner input = new Scanner(System.in);
    
    Test2() {
        System.out.println("How many entries?");
        num = input.nextInt();
        arr = new int[num];
        for(int i = 0; i < num; i++) {
            System.out.println("Integer:");
            arr[i] = input.nextInt();
        }
        reverseArray();
        printArray();
    }
    
    private void reverseArray() {
        for(int i = 0; i < num/2; i++) {
            temp = arr[i];
            arr[i] = arr[num-1-i];
            arr[num-1-i] = temp;
        }
    }
    
    private void printArray() {
        if(num > 5) {
            System.arraycopy(arr, 0, newArr, 0, 5);
            System.out.println(Arrays.toString(newArr));
        }
        else
            System.out.println(Arrays.toString(arr));
    }
    
    public static void main(String[] arguments) {
        Test2 test = new Test2();
    }
    
}


This post has been edited by schutzzz: 01 August 2013 - 08:22 AM

Was This Post Helpful? 0
  • +
  • -

#11 manuelma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-July 13

Re: Error with inputs into an array, then printing them.

Posted 01 August 2013 - 09:12 AM

Thanks, I got it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1