• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble with arrays / Validating user input  RSS feed

 
Ryan Cridelich
Greenhorn
Posts: 10
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What I'm trying to do here is create an inventory that keeps track of a collection of video games. The user would enter in the information regarding each game in stock one at a time for each iteration of the for loop. This information is supposed to be getting stored in the mygames array, everything entered in the first iteration of the for loop is supposed to be getting stored in the first spot in the array, everything entered during the second iteration gets stored in the second spot in the array and so on. At least this is what I'm trying to get it to do. Finally once the user enters in the word "stop" for the name of the game, the program should output the contents of the array one game at a time by using the inner for loop. I'm using count, to represent the current index that the loop is on.

The problem I'm having is when I go to run the program, and type in stop for the name of the game, the program just keeps on going as if stop was never entered. So I'm not sure what I'm doing wrong here, or even if the information that's supposed to be getting stored during each iteration of the loop is actually getting stored in the mygames array.

I'm just starting out learning java, so any help here would be appreciated.


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell me why you think this code will cause your app to stop looping and prompting for input?


 
Ryan Cridelich
Greenhorn
Posts: 10
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I'm trying to do there is to compare the current value of gameName to "stop", so if it is equal to stop, then end now equals true, so the program should proceed to printing out the array. Going into that code, the boolean end = false, so if stop is entered, then the boolean is changed to true, so the program should go to the last step in the program. This code should get the program to stop asking for input by verifying that stop has been entered. Which is that case, the boolean is now true.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Cridelich wrote:end now equals true, so the program should proceed to printing out the array.


Why do you think setting end to true will cause it to proceed to printing out the array?

Going into that code, the boolean end = false, so if stop is entered, then the boolean is changed to true, so the program should go to the last step in the program.


Why do you think that end being true should make it go to "the last step in the program"? And can you show me where you found Java's definition of what "the last step" is?

I'm not asking these things to be obtuse or to be a jerk. It seems like you're making certain assumptions, and I'm trying to get you to recognize and question those assumptions. It's a very important practice to get into if we're going to have any success at debugging anything.
 
Ryan Cridelich
Greenhorn
Posts: 10
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What I meant by last step in the program, is that it should proceed to the last set of statements in the program that need to be executed before program completion. You are correct about me making assumptions. I am assuming that because end was originally set to false, that as long as end equals false the program will continue looping and asking for user input, but once end equals true the program stops asking for input. I'm not sure how else to determine if the sentinel value has been entered or not.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Cridelich wrote:
What I meant by last step in the program, is that it should proceed to the last set of statements in the program that need to be executed before program completion.


Okay. But in your studies of Java so far, did you come across anything that looked like and official definition of "last set of statements etc. etc."? One of the assumptions you're making is that there is a clearly defined "last step." I'm wondering if you have something to base that on, or if you're just guessing that because you happen to look at this particular code and can easily see what you intuitively consider to be the "last step", that you are therefore assuming that the rules of the Java language have also spelled out such a "last step" at all, much less in a way that is consistent with your intuition.

That assumption is not the main source of the problem however.

I am assuming that because end was originally set to false, that as long as end equals false the program will continue looping and asking for user input, but once end equals true the program stops asking for input.


Why? For that to be the case, either the Java language must have a rule that says, "When a variable called end is set to true, break out of the loop," or else you must have written code that says, "when my end variable has been set to true, break out of the loop".

So, are you assuming that Java has such a rule? Why would you make that assumption? If it has that rule, does it also apply to variables called "complete", "finish", and "stop"?

Or can you point me to code that you wrote that does that?

I'm not sure how else to determine if the sentinel value has been entered or not.


Your code looks like it's correctly determining that the sentinel value has been entered. It's just not acting on it.
 
Ryan Cridelich
Greenhorn
Posts: 10
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I have not read anywhere about what Java considers to be the last step in any program. That was, like you said, an assumption, that I did not base off of anything, or any reading. I just assumed by looking at the program, that the last block of statements, were what I would consider to be the last step, but evidently, I was wrong, and isn't that besides the point. I am obviously missing something, or not understanding something related to that. I'm not trying to be negative, and I'm sure you can tell by my code that I'm just starting out with Java. I'm just trying to understand what you're getting at with the last step in Java. I didn't mean anything by it, by talking about what I wrongly considered to be the last step in a program. As far as the whole end equaling true or false thing, I'm not sure how that's supposed to work. I remember I looked up how to validate if the sentinel value has been entered or not, because I wasn't sure how to do it, and the example I found, had that in there, so I used it.
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan,
Welcome to the Ranch.

When I read your code I see this (written in pseudocode):



Based on the code above, how many times will the loop run? Always ten.
Unless you actually do something with "end", which is what Jeff is getting at. You need to check at some point if "end" is true, and if it is, you need to do something to stop (break) the loop.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Cridelich wrote:No, I have not read anywhere about what Java considers to be the last step in any program. That was, like you said, an assumption, that I did not base off of anything, or any reading. I just assumed by looking at the program, that the last block of statements, were what I would consider to be the last step, but evidently, I was wrong, and isn't that besides the point.


Yes, it is beside the point. As I mentioned, that wasn't the real problem. I only brought that up to drive home the ideas of precision and checking your assumptions. Sorry if it became a distraction.

As far as the whole end equaling true or false thing, I'm not sure how that's supposed to work. I remember I looked up how to validate if the sentinel value has been entered or not, because I wasn't sure how to do it, and the example I found, had that in there, so I used it.


Tina has laid it out quite nicely for you above. Is it clear now?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tina Smith wrote:...and if it is, you need to do something to stop (break) the loop.

I totally agree with your post; but from a programming point of view, I'd say that "stopping" is better than "breaking".

ie: Wherever possible, the loop itself should specify the conditions under which it continues. I'm not a big fan of the break statement.

Winston
 
Ryan Cridelich
Greenhorn
Posts: 10
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After looking over Tina's post I believe that I'm seeing what my program was actually doing instead of what I thought it was doing. Now I just need to somehow put in a test to determine whether or not end is equal to true, and if it is, put in a break to exit the loop.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't necessarily need to "break" out of the loop per se. That is, you don't have to use the break keyword. That's one valid approach, but it can be an inelegant way to end a loop. If you do use break, you wouldn't even need the end variable.

Another way--the way Winston is suggesting--is to put it right into your loop condition:


We get so used to writing for loops that way that we take that form for granted, and I think a lot of beginners thing it always has to be exactly like that, but there are several different pieces there that have a lot of flexibility.

In particular, the middle piece, the condition, can be any boolean expression. You could use that to say "keep going while count is less than 10 AND the end variable is not true".

Side note: You want < 10, not <= 10. Your Array is size 10, so its indices are 0..9. If you have <= 10, you'll get ArrayIndexOutOfBoundsError.

Also, all those println calls you've got in the if (....stop) block should be moved outside the loop, after it's done.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Borrowing Tina's pseudocode, what you have is this:


What you want is this:


or this:
 
Ryan Cridelich
Greenhorn
Posts: 10
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, everyone for their help, I was finally able to get my program to stop looping when the sentinel value is entered. The problem that I am now experiencing is that the information being entered, does not seem to be getting stored in the mygames array. When the array gets printed out, nothing is in it. Here's my newer code:



 
Ryan Cridelich
Greenhorn
Posts: 10
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I would have posted this message in a new topic, but I figured it might have been classified as a duplicate thread, so I'm posting it here instead. The error I am coming across is from my same Inventory Program from previous posts. So I added in a new class to my program called VideoGameRating, and I am now coming across about 6 errors that look like the following:

InventoryProgram03.java:38: error: variable gameName might not have been initialized
VideoGameRating myGame = new VideoGameRating(gameName, gameNumber, gamePrice, gameQuantity, valueOfInventory, gameRating);


I have all of those variable initialized at the top of that class. If I try removing the 6 arguments, I get 1 error stating that 6 arguments are required, and it found 0, actual and formal argument lists differ in length. I have been having quite a bit of trouble with this program, and sometimes wonder if it would have been easier to just create the program with the array already filled, instead of asking for user input and filling it that way. The requirements for the application never stated that the array had to be filled by user input, I created it the way I did because I wanted to create a program that seemed practical. Nevertheless, this is the way I did it so I should probably keep it this way. I have included below my 3 classes that I have for my Inventory Program.




 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Cridelich wrote:
I have all of those variable initialized at the top of that class.


No, you don't. You've declared them, but you haven't initialized them. Member variables get default values, but locals (which this are) do not. They don't have a value until you assign one to them.

So you declare the variables, they don't have any values defined, and then you go into the loop and do:

What value do you think you're passing for gameName there? How do you think it got that value?
 
Ryan Cridelich
Greenhorn
Posts: 10
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused as to why I need to list those variables there to begin with, or what their purpose is supposed to be, but when I try to compile that class and I don't have those variables listed there, I get an error stating that they need to be there. It just says that the actual and formal argument lists differ in length. What is says is required is String, and four doubles, but found nothing.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Cridelich wrote:I'm confused as to why I need to list those variables there to begin with, or what their purpose is supposed to be,


They're parameters. Do you understand how parameters work in methods and constructors?

If you wrote the VideoGameRating class, then you know what you're doing with them in its constructor and why they have to be there. When you're constructing a VGR object, you're telling it what its name, number, etc. will be.

It just says that the actual and formal argument lists differ in length. What is says is required is String, and four doubles, but found nothing.


That's quite explicit and to the point. It's telling you exactly what's wrong. When you invoke that constructor, you have to give it that information, but you're not providing it.
 
Ryan Cridelich
Greenhorn
Posts: 10
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I think I figured it out, the program finally compiled and ran, but after entering in info on the games, and entering in the sentinel value, the program should output the contents of the array. When I enter in the sentinel value to print out the mygames array, the information for some reason is not being stored in the array. The only thing printed out is the values that the variables were initialized to, which is 0.
 
Ryan Cridelich
Greenhorn
Posts: 10
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I got the program to print out the array, I was using count to print out the array, when I should have been using the variable a. Now the problem I am running into, is when the array is printed out. When the value of inventory for a particular game is printed, it's not print the correct value, so I checked my method for calculating valueOfInventory, and it looks correct. You need to calculate (itemQuantity * itemPrice) * .05, for a 5 percent restocking fee. But when that method is called, it's not printing out the correct value.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!