• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble with for loop  RSS feed

 
Diego Rodriguez
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone I am new to Java and I would just like to know if I can make this program into a loop instead of writing the whole program without a loop.

Thanks here is my program -

 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi best,welcome to coderanch where exactly do you think you may use loop.
and please use codetags next time.

kind regards,
Praveen.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I can see a little problem which you might not have encountered. You are calling nextDouble on a Scanner object, followed by nextLine. Please tell us what you have been told nextLine does; if you read it in a book, please tell us the book's name.
There is a serious risk that nextLine will return the empty String and you will get all sorts of problems, particularly if you go back to nextDouble and you get an Exception. You will find an explanation and a possible solution here.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Also you are using a lot variable names which don't tell anything. Examples are: r1, yesOrNot (why not boolean instead of string?), TWF, TWFR, sqFTPerBox, SQFTPerBox (<- what they are, what they represent? when you explain in english what they represent - name them exactly as you explained). For example, lets assume TFW represents 'total wasted floor', so name it then as 'totalWasterFloor'.

2. Consider using equalsIgnoreCase() method, so you'd shorten amount of conditions by 50%. Then consider using one of logical operators, so you could get one compound condition. Think which logical operator would fit here. Or, and it is probably way to go - create a method which checks a condition and gives you an answer, so you could perform one or another action based on the answer you get.

3. Don't be worried to create a lot of methods, so you could simplify your whole program. Methods for each routine.
 
Diego Rodriguez
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

I can see a little problem which you might not have encountered. You are calling nextDouble on a Scanner object, followed by nextLine. Please tell us what you have been told nextLine does; if you read it in a book, please tell us the book's name.
There is a serious risk that nextLine will return the empty String and you will get all sorts of problems, particularly if you go back to nextDouble and you get an Exception. You will find an explanation and a possible solution here.



I learned most of my Java knowledge from watching tutrorials on the internet. The person who made the videos always used nextLine and only said it is for getting input from the keyboard for strings.

Thanks for responding back!
 
Diego Rodriguez
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:1. Also you are using a lot variable names which don't tell anything. Examples are: r1, yesOrNot (why not boolean instead of string?), TWF, TWFR, sqFTPerBox, SQFTPerBox (<- what they are, what they represent? when you explain in english what they represent - name them exactly as you explained). For example, lets assume TFW represents 'total wasted floor', so name it then as 'totalWasterFloor'.

2. Consider using equalsIgnoreCase() method, so you'd shorten amount of conditions by 50%. Then consider using one of logical operators, so you could get one compound condition. Think which logical operator would fit here. Or, and it is probably way to go - create a method which checks a condition and gives you an answer, so you could perform one or another action based on the answer you get.

3. Don't be worried to create a lot of methods, so you could simplify your whole program. Methods for each routine.


Ok I re-wrote the names


 
Diego Rodriguez
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:Hi best,welcome to coderanch where exactly do you think you may use loop.
and please use codetags next time.

kind regards,
Praveen.


I want to use a loop instead of writing each room one by one
 
Knute Snortum
Sheriff
Posts: 4284
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a bug quirk with using nextDouble() and then nextLine().  Basically, nextDouble() (and most of the nextXxx() methods) leaves the newline character(s) in the buffer and nextLine() will interpret this as you hitting the <enter> key.  The solution is this:
 
Knute Snortum
Sheriff
Posts: 4284
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know how to write and use methods?  Right now you have a lot of code in main().  This reason I point this out is a) it's good practice to get code out of main(), and b) it will make looping a lot easier.  Let me show you in pseudocode:
 
Knute Snortum
Sheriff
Posts: 4284
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There are several problems with this code.  First, you changed the name of the variable you get the keyboard input from, but not the variables you test in the if statements.

Next, when you have several conditions that all need to do the same thing, use the conditional or operator (||), like this:

if (condition1 || condition2 || condition3)

Next, there's another kind of equals() method called equalsIngnoreCase().  So putting those together, you have:
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Diego Rodriguez wrote:. . .  tutrorials on the internet. The person who made the videos always used nextLine and only said it is for getting input from the keyboard for strings.
I don't think using nextLine exclusively is the right way to use a Scanner object. Please tell us which tutorial it was.
Thanks for responding back!
Our pleasure
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:. . . Next, there's another kind of equals() method called equalsIngnoreCase().  So putting those together, you have:
An alternative way to do that is to use methods of the String object to see whether the first letter is y.
 
Diego Rodriguez
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Diego Rodriguez wrote:. . .  tutrorials on the internet. The person who made the videos always used nextLine and only said it is for getting input from the keyboard for strings.
I don't think using nextLine exclusively is the right way to use a Scanner object. Please tell us which tutorial it was.
Thanks for responding back!
Our pleasure


Go to codelearner.com
 
Diego Rodriguez
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Do you know how to write and use methods?  Right now you have a lot of code in main().  This reason I point this out is a) it's good practice to get code out of main(), and b) it will make looping a lot easier.  Let me show you in pseudocode:


Yes I have some knowledge with methods, but not that much. Thanks for the example!
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Diego Rodriguez wrote:. . . Go to codelearner.com
Thank you Since you have to pay for those tutorials, I couldn't assess them.
 
Diego Rodriguez
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Diego Rodriguez wrote:. . . Go to codelearner.com
Thank you Since you have to pay for those tutorials, I couldn't assess them.


No problem!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!