Does this look right? Re: Arrays

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

42 Replies - 1706 Views - Last Post: 10 November 2011 - 08:54 PM Rate Topic: -----

#1 ertopp  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 74
  • Joined: 27-February 11

Does this look right? Re: Arrays

Posted 04 November 2011 - 07:14 PM

For some reason I can't seem to wrap my head around arrays.
This is part of the assignment:
public TestData()
The default constructor that will do the following:
prompt the user for and input a test name which is assigned to the testName field using the following input dialog box:


initialize the count field to 0
instantiate the scores array setting its size to MAXSIZE
invoke the loadArray() method to load the scores into the array.

private void loadArray()
The method is called in the constructor. The method prompts the user to input a score, stores the score in the array, and increments the field count by 1 for each score entered. The method should not allow more than 25 scores to be entered. The scores should be enter using the following input dialog box:

**The part in bold,italicize is one part I am having trouble with. Other than that, does this even look right?**

 public TestData() {

	Scanner keyboard = new Scanner(System.in);

	testName = JOptionPane.showMessageDialog(null, "Input the name for the test. For example, Test1.");

	for (int count = 0; count < MAXSIZE; count++)

	{
		scores[count] = keyboard.nextInt();
	}

  }

    private void loadArray(int[] array)  {  // INPUT SCORES AND STORE SCORES IN ARRAY

      for (int count = 0; count < MAXSIZE; count++)

      input = JOptionPane.showMessageDialog(null, "Input test score and press ENTER (-1 to end");



Any help would be appreciated. Or if anyone can point me to a good website on arrays that would be helpful. I went on to the oracle site and that wasn't much help.

Is This A Good Question/Topic? 0
  • +

Replies To: Does this look right? Re: Arrays

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Does this look right? Re: Arrays

Posted 04 November 2011 - 07:31 PM

Why do you use Scanner and JOptionPane? Also, have you compiled your code? I don't see input declared anywhere, and you need to parse it to an int, and assign that int to array[i] in the loop.

As for a tutorial, you might find TriggaMike's Arrays Tutorial helpful.
Was This Post Helpful? 1
  • +
  • -

#3 ertopp  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 74
  • Joined: 27-February 11

Re: Does this look right? Re: Arrays

Posted 04 November 2011 - 07:36 PM

public class TestData {

    // class fields
    private String testName;
    private final int MAXSIZE = 25;
    private int count;
    private int[] scores = new int[MAXSIZE];


    public TestData() {

	Scanner keyboard = new Scanner(System.in);

	testName = JOptionPane.showMessageDialog(null, "Input the name for the test. For example, Test1.");

	for (int count = 0; count < MAXSIZE; count++)

	{
		scores[count] = keyboard.nextInt();
	}

  }

    private void loadArray(int[] array)  {  // INPUT SCORES AND STORE SCORES IN ARRAY

      for (int count = 0; count < MAXSIZE; count++)

      input = JOptionPane.showMessageDialog(null, "Input test score and press ENTER (-1 to end");
	  

	  while (scores != -1 && count < MAXSIZE)
	  {
	  	scores[count] = score;
	  }

    }



That is my full code so far.

where do i parse to an int? after the input line?
so i parse, then what do you mean by set that to an array?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Does this look right? Re: Arrays

Posted 04 November 2011 - 07:37 PM

Ok. And? What have you done to debug? Have you tried implementing any of my suggestions?

Edit: Just saw your addition. I'm sure you are familiar with the Integer.parseInt() method. That's what I'm referring to. Remember that array elements are accessed by an index. So if you have a loop:
for(int i = 0; i < array.length; i++){
    //array[i] is the ith element in the array
}


Was This Post Helpful? 0
  • +
  • -

#5 ertopp  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 74
  • Joined: 27-February 11

Re: Does this look right? Re: Arrays

Posted 04 November 2011 - 08:11 PM

I believe I have to use count instead of index, does that make sense?
I looked at the array help you linked but it didnt really help much. This is the most involved programming assignment I have had to do so far and I am totally lost.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Does this look right? Re: Arrays

Posted 04 November 2011 - 08:19 PM

The whole idea behind arrays is that you are dealing with a bunch of variables under one name and numbered. So rather than variables named item1, item2, item3, item4, you can declare an array to hold these items.
int[] items = new int[10];



Now, since ints are initialized to 0 by default, all the elements in items are 0.

To iterate through an array, you can use a for loop. Remember that arrays are indexed from 0 through length-1. So since the length of items is 10, the indices are 0-9. When I access items[i], I am accessing the element at the given index. So when i = 0, I will be accessing items[0], the first element in the array.
for(int i = 0; i < items.length; i++){
    items[i] = 3*i;
}


Was This Post Helpful? 0
  • +
  • -

#7 ertopp  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 74
  • Joined: 27-February 11

Re: Does this look right? Re: Arrays

Posted 04 November 2011 - 08:27 PM

Ok, so my max value is 25. and we have to use count.
So could my for loop look like this:
for (count = 0; count < MAXSIZE; count++)

also, would I put this line in the testData method AND the load array method?

And, any idea what it means to "invoke the loadArray() method to load the scores into the array."?
Was This Post Helpful? 0
  • +
  • -

#8 ertopp  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 74
  • Joined: 27-February 11

Re: Does this look right? Re: Arrays

Posted 05 November 2011 - 08:51 PM

I still cannot get my program to exit when hitting "-1"
Can someone PLEASE help me. I have searched high and low and tried so many different things but nothing seems to work.

I'm sure what I have now is not right, but once again, I have been trying many different ways to see if I could get this to work.

    private void loadArray(int[] array)  {  // INPUT SCORES AND STORE SCORES IN ARRAY

    int [] scores = new int[MAXSIZE];

    for(count = 0; count < scores.length; count++)
    {
      input = JOptionPane.showInputDialog(null, "Input test score and press ENTER (-1 to end) ");
      scores[count] = Integer.parseInt(input);
    }
    while (scores[count] != -1 && count < scores.length)
    {
    	scores = array;
      	count++;
    }


    }



Every time I change something I get the "incompatible types" error. Please help.
Was This Post Helpful? 0
  • +
  • -

#9 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Does this look right? Re: Arrays

Posted 05 November 2011 - 09:37 PM

I think the problem lies here. (I haven't read previous posts).

Your while loop is using the count variable which isn't be incremented because it's outside the for loop. It will loop indefinitely. You want to check if the user entered -1 with each for loop. To break out of the loop you can use the 'break;' statement.

    
for(count = 0; count < scores.length; count++)
    {
      input = JOptionPane.showInputDialog(null, "Input test score and press ENTER (-1 to end) ");
      scores[count] = Integer.parseInt(input);
    }


// This is not inside the for loop. Count will by default be 0, this will loop indefinitely unless the first value is -1 of course.
    while (scores[count] != -1 && count < scores.length)
    {
    	scores = array;
      	count++;
    }


This post has been edited by Mylo: 05 November 2011 - 09:43 PM

Was This Post Helpful? 0
  • +
  • -

#10 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Does this look right? Re: Arrays

Posted 05 November 2011 - 09:56 PM

Disregard most of the previous post, I really dislike the way DIC will not let you edit posts after a while. I misinterpreted your code.

Your checking if the user entered -1 AFTER asking for 25 values.

You are also asking for 25 values and then overriding those 25 values with an array you passed in anyway. Did you mean to put array = scores instead?

This post has been edited by Mylo: 05 November 2011 - 10:01 PM

Was This Post Helpful? 0
  • +
  • -

#11 ertopp  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 74
  • Joined: 27-February 11

Re: Does this look right? Re: Arrays

Posted 06 November 2011 - 06:40 AM

The user has to enter up to 25 scores, or can hit -1 to exit. So they don't have to enter a total of 25 scores, just UP TO 25 scores.

My book uses that while example a lot, but the word before the "!=" I don't think I am using the right term.
Was This Post Helpful? 0
  • +
  • -

#12 AMZDeCoder  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 102
  • Joined: 04-November 11

Re: Does this look right? Re: Arrays

Posted 06 November 2011 - 07:39 AM

Dear ertopp,

Can you please copy your entire code here, so that it will give a better idea, seems there are a lot of compilation errors have you compiled your code ?

View Postertopp, on 05 November 2011 - 08:51 PM, said:

I still cannot get my program to exit when hitting "-1"
Can someone PLEASE help me. I have searched high and low and tried so many different things but nothing seems to work.

I'm sure what I have now is not right, but once again, I have been trying many different ways to see if I could get this to work.

    private void loadArray(int[] array)  {  // INPUT SCORES AND STORE SCORES IN ARRAY

    int [] scores = new int[MAXSIZE];

    for(count = 0; count < scores.length; count++)
    {
      input = JOptionPane.showInputDialog(null, "Input test score and press ENTER (-1 to end) ");
      scores[count] = Integer.parseInt(input);
    }
    while (scores[count] != -1 && count < scores.length)
    {
    	scores = array;
      	count++;
    }


    }



Every time I change something I get the "incompatible types" error. Please help.



Your logic doesnot seem correct why do you need the while loop at all ???
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Does this look right? Re: Arrays

Posted 06 November 2011 - 09:43 AM

The whole point of passing an array to the method is so you don't have to re-declare a new array inside the method. Just populate array, and forget about scores entirely.
Was This Post Helpful? 0
  • +
  • -

#14 ertopp  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 74
  • Joined: 27-February 11

Re: Does this look right? Re: Arrays

Posted 06 November 2011 - 11:35 AM

This is the assignment: Create the class TestData that will store the maximum of 25 integer test scores in an integer array. The class should declare the following fields (instance variables):

private String testName;
String field to store the test name.
private final int MAXSIZE = 25;
Integer constant for the maximum size of the array field.
private int count;
Integer field for the number of scores entered.
private int scores[];
Integer array to store the test scores.
The class will contain the following methods:

public TestData()
The default constructor that will do the following:
prompt the user for and input a test name which is assigned to the testName field using the following input dialog box:
initialize the count field to 0
instantiate the scores array setting its size to MAXSIZE
invoke the loadArray() method to load the scores into the array.
private void loadArray()
The method is called in the constructor. The method prompts the user to input a score, stores the score in the array, and increments the field count by 1 for each score entered. The method should not allow more than 25 scores to be entered. The scores should be enter using the following input dialog box:

Then this is my code:
public class TestData {

    // class fields
    private String testName;
    private String input;
    private final int MAXSIZE = 25;
    private int count;
    private int scores[];


    public TestData() {

	testName = JOptionPane.showInputDialog(null, "Input the name for the test. For example, Test1.");

	count = 0;
	int [] scores;
	scores = new int[MAXSIZE];
		loadArray(scores);
  }

    private void loadArray(int[] array)  {  // INPUT SCORES AND STORE SCORES IN ARRAY

    int [] scores = new int[MAXSIZE];

    for(count = 0; count < scores.length; count++)
      input = JOptionPane.showInputDialog(null, "Input test score and press ENTER (-1 to end) ");
      scores[count] = Integer.parseInt(input);
    while (scores[count] != -1 && count < scores.length)
    {
    	scores = array;
      	count++;
 	if count < MAXSIZE
 		input = JOptionPane.showInputDialog(null, "Input test score and press ENTER (-1 to end) ");
		scores[count] = Integer.parseInt(input);
    }
   }





I can't remember what it's called, but I have a separate program to run the data.
Was This Post Helpful? 0
  • +
  • -

#15 ertopp  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 74
  • Joined: 27-February 11

Re: Does this look right? Re: Arrays

Posted 06 November 2011 - 08:49 PM

Do I have to use a "for" loop with an Array?

This is my code now.
public class TestData {

    // class fields
    private String testName;
    private String input;
    private final int MAXSIZE = 25;
    private int count;
    private int scores[];


    public TestData() {

	testName = JOptionPane.showInputDialog(null, "Input the name for the test. For example, Test1.");
	count = 0;
	int [] scores;
	scores = new int[MAXSIZE];

		loadArray(scores);

  }

    private void loadArray(int[] array)  {  // INPUT SCORES AND STORE SCORES IN ARRAY

 		input = JOptionPane.showInputDialog(null, "Input test score and press ENTER. (-1 to end) ");
 		array[count] = Integer.parseInt(input);

 		array = scores;
 		array[count]++;

 	while (array[count] != -1 && count < MAXSIZE)
 	{
 		loadArray(scores);
 	}



 }



I have changed it so many times, but I am going step by step in my professors instructions. His one line of instruction says to use the while. My program pops up asking for the test name, but then ends after I type in one test score. So that's why I am guessing I need the for loop? Anymore advice on this would be so appreciated. I am about to just give up an take a big fat 0.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3