aspose file tools*
The moose likes Beginning Java and the fly likes Need help with while-loop in constructor. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with while-loop in constructor." Watch "Need help with while-loop in constructor." New topic
Author

Need help with while-loop in constructor.

raul vargas
Greenhorn

Joined: Mar 15, 2012
Posts: 14

I have my constructor made but I can't seem to figure out how to set up this while-loop

The constructor code:



Instructions on how to make the loop:

The constructor should initialize all elements of the arrays to -1.0 to indicate there is no score in that element yet.


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

Why does it have to be a while-loop?
Do you know the syntax for a while loop?
Do you know what they mean by "initialize all elements of the arrays to -1.0 "

Personally, I think a for loop would be better, but technically, you don't need any loop at all.

So...What SPECIFICALLY do you need help with?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
raul vargas
Greenhorn

Joined: Mar 15, 2012
Posts: 14

Yes, it has to be a while-loop. Those are the only loops I can work with.

I don't know how to initialize to -1.0

The example loop I have looks like this:


Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563
Does it have to be a while loop? Typically, when the programmer can identify positively how many times the loop must execute, either a for or enhanced for loop would be used. Here are the Java tutorials on looping.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

raul vargas wrote:
Yes, it has to be a while-loop. Those are the only loops I can work with.

I don't know how to initialize to -1.0

The example loop I have looks like this:



if it must, it must...
you are missing part of the while loop. Generally, they look like this:




Those braces are important. You need a closing one for each opening one, and your example is missing the closing one.

So you have the line "int i = 0;". That is just there to set up the starting conditions.

You then have "while (i < 10)". Each time through the loop, that condition is checked. So hopefully, i will be changing inside your loop, otherwise you will be stuck in there forever - what is called an "infinite loop".

So you need to put code inside the curly brackets - what is called the body of the loop. You have "i++" in the loop. Each time that line executes, i will be incremented by 1. so the first time, it will change from 0 to 1, then from 1 to 2, etc. That means that this loop will run ten times before quitting. That is convenient, since you have an array with ten elements. The common idiom is to use your variable i as an index (that's why i is often used as the variable name) of the array.

To access a specific element of the array, you say "arrayName[index]". In your example, you have one array called quizzes. you set a value the same way as any other double variable. If i had a double named "fred", i would say

fred = 1.0;

since i have an array, i can say

this.quizzes[3] = 1.0;

that would set the fourth element to 1.0 (remember, arrays are zero based indexed, so the first element is this.quizzes[0]).

So you could explicitly name all ten elements and set each to -1:

this.quizzes[0] = -1.0;
this.quizzes[1] = -1.0;
this.quizzes[2] = -1.0;
...etc

or you could use a variable...and index...that is changing. sounds like our variable 'i' from above.

Why don't you give it a try, and show us what you come back with.
raul vargas
Greenhorn

Joined: Mar 15, 2012
Posts: 14

Is this correct?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Since it doesn't assign anything to any array entries, and the point of the code was to assign -1.0 to some array entries, then... No, it isn't correct.
raul vargas
Greenhorn

Joined: Mar 15, 2012
Posts: 14

How do I do this? I've tried but I obviously don't know what I'm doing
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

What references have you read about the while-loop and what don't you understand?

You should know that this site is NotACodeMill (<--click). We will guide you around obstacles but we expect you to ShowSomeEffort (<--click-->) ItDoesntWorkIsUseless and won't cut it with folks around here.


Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with while-loop in constructor.