I have to do this project for school that finds the Standard Deviation. The first part of it, which has to build an array from scanner with a while loop, has been killing me today.
The problem is that my teacher wants a program that will allow a user to enter a list of test scores(use 999 as a sentinel value), and have theses values stored in an integer array. I've gotten this to work with a for loop without a sentinel value, but if the instructions call for a sentinel value, then I must need it.

## All 25 Replies

ve gotten this to work with a for loop without a sentinel value

How did you stop the loop?
What do you understand a sentinel value to mean?
If you can write a loop to get input from a user, what ways do you have to have the user tell you when he is done entering input? One way is to have a special value the user will enter that tells the code in the loop that there is no more input. That value is the sentinel value.

it's a while loop that keeps going until 999 is entered. The problem is that I can't build the array from it.
This is my code:

``````import java.util.Scanner;

public class standardDeviation
{
public void arrayBuilder()
{
System.out.print("\f");
Scanner keyboard = new Scanner(System.in);

int count = 0;

{
System.out.println("Enter a number, or enter 999 to end.");
count++;
}

System.out.println("You have entered: " + (count-1) + " number(s).");

}
}``````

The sentinel works correctly. I just can't get the numbers that are put in into an array.
Help me out.

Can you code a statement that assigns a value to the first element of an array?

Then use a variable to chose which element of the array to assign a value to?

Start the variable at 0 and increment it every time something is added to the array so that it points to the next empty/unused element in the array.

I appreciate the help, but I've tried just about everything possible today. Is it possible for you to write out the code for what you are saying? I can't completely process where you are going. I had a thinking that went along with something like you said, but couldn't get it to work.

but couldn't get it to work.

Post your code and the errors you get.

You need to define an array.
You need to assign values to elements in the array using an index variable.
You need to change the index to go to the next element in the array

I didn't need to read the tutorial as it is what I am currently doing in my ap computer science class, so I know it. I was trying to do the thing where you change the index to the next element but I can't get it. I don't have the code that was attempting to do that because I've compiled so many times, that version was awhile ago. My code is still the same as the first post.

Ok, we'll do this slow and easy.
Write the code to define an array.

okay. Well, this program is taking in numbers to represent grades so I'll call the array grades.

I don't know what to put for length because the program calls for the array to be as long as how many numbers are entered.

Then make it a bigger number than you think the user will enter: 1000

Now the code to assign a value to the first element in the array.

I tried that before, but thought it would conflict when I do the calculation for standard deviation. This is because you would need the the average of the grades entered, and all the zeros after the numbers entered by the user would mess up the average.

Your code is going to need to assign values to elements in the array.

Can you write the code to assign a value to the first element in the array?

alright...

``````int [] grades = new grades[1000];

for (int i = 0; i < grades.length; i++)
{
}``````

Two things you need to consider:
1)You need to read the input into a variable so you can check for the sentinel value BEFORE you store the value into the array.
2)You need to keep track of how many numbers the user has entered into the array so you know where the end of the data in the array is. The rest will be zeros.

Both of these things were handled by the first code you posted. You could use the variable: count for the array index and it would have the number of numbers that were read into the array when the loop exited.
Remember NOT to store the sentinel into the array.

This is where I'm stuck...

Explain which part:
1)read the input into a variable
2)Test the variable to see if it has the sentinel value
3)store the variable into the array at the index
4)increment the index

You've done most of the above:
2) < YOU COULD USE AN if statement here and a break statement to exit the loop>
4) count++;

writing the loop to do all of that

Yes that all goes in the loop.

how do I go about that?
I've been thinking of something like this:

``````public class tester
{
public void arrayBuilder()
{
System.out.print("\f");
Scanner keyboard = new Scanner(System.in);
int count = 0;
int [] grades = new int[1000];

System.out.println("Enter the numbers now.");

for (int i = 0; i < grades.length; i++)
{
}

System.out.println("These are the numbers you have entered.");
for (int i = 0; i < grades.length;i++)
System.out.println("");

}
}``````

Then somewhere in the for loop, put something like if sentinel value is entered, then break out of the loop.

Something like that. Go back and read what was said earlier.

You won't want to print out the whole 1000 elements in the array.
Use the value in count to control the looping.

I'm honestly so confused right now. Can you just tell me the loop? If i can just get the loop, everything else will be simple to me. But the loop is killing me all day here.

You have a start on the loop.
Re-read what has been said about what to do in the loop. I have nothing more to add to what has already been said.

That's it for me tonight.

can you at least tell me if I should use a while or for loop? It can go either way

can you at least tell me if I should use a while or for loop? It can go either way

Either loop will be fine as long as you use them correctly

for (int i = 0; i < grades.length;i++)

I think you should use (count) as a counter to read parts of the array that has a value instead of reading the whole elements of the array
of course you need to increment the value of count in the loop that asks the user for the numbers to have a definite number of elements to read

IMHO NormR1 has said and explained everything on how you should tackle on making the loop for the string... showing the code would be spoonfeading already

I got the entire thing to work. I used a for loop to build up the array, then a break if 999 was entered. That array still contained 999 at the end, and O's if the user entered less numbers than they initially entered for the length. I then used a method to delete the 999 from the method and replace it with a 0. Then I copied the elements from this array to a new one, where it only contained the values from the beginning to the end of the numbers entered, with no leftover 0's at the end or anything. Problem solved.

There is always more than one way to solve a problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.