• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Coding Help

 
Colton Herren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just started doing it today and I ran into my first problem. I wrote the code but it has an error and I can't tell what it is. So if you could help me out that would be appreciated. Sorry if its super obvious.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34695
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Colton,
Welcome to CodeRanch! I've edited your post to add code tags and make it look all pretty .


This line is suspect. Since x doesn't change, it would make for an infinite loop. You don't say what "has an error" means. What should we be looking for? Did you get a compiler error? Did it not print out what you expected?
 
Colton Herren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah when I try to compile it it says cannot read 1 error.
 
Colton Herren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it by adding x = x + 1. But now when I run it it just keeps putting out big X over and over and doesn't stop.

public class Variables {
public static void main (String[] args) {
int x = 5;
while (x > 3) {
x = x + 1

if (x < 10) {
System.out.println("Small x");
} else {
System.out.println("Big X");
} // end else
} // end while loop
} // end main method
} // end class
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Colton Herren wrote:yeah when I try to compile it it says cannot read 1 error.


You need to TellTheDetails.(⇐click) Copy/paste the exact, complete error message.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So, tell me, when do you think the loop will end? (Actually, it will end eventually, but probably not when or why you expect it to.)

And in the future, please PostRealCode(⇐click) and UseCodeTags.(⇐click) You'll get better help if you show your actual code, and if you make it easy for people to read it.
 
Colton Herren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I run the program from the command prompt the words Big X continuously pop up like this
Big X
Big X
Big X
Big X
Big X
Big X
and on and on.
I need help with how to make it only pop up once.
 
Colton Herren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so it wont end because x will always be bigger than 3
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Colton Herren wrote:when I run the program from the command prompt the words Big X continuously pop up like this
Big X
Big X
Big X
Big X
Big X
Big X
and on and on.
I need help with how to make it only pop up once.


If you want it to run only once, why are you using a loop? I loop is for when you want to do something multiple times (or potentially multiple times).

So, under what conditions (in English) do you want the loop to stop or to keep going? Or do you really want it to just go exactly once no matter what?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Colton Herren wrote:so it wont end because x will always be bigger than 3

One might think that, but try this:


 
Colton Herren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I got it I didn't realize that's what I had made. Sorry
 
Colton Herren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I was looking for it to do in the beginning but for some reason I added that while and it messed me all up. Thanks for the help!

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome! Glad you got it sorted out.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad it’s sorted, but it didn’t print Big X for ever after you put the increment in the loo. It printed Big X maybe 2147483637 times. If you had counted it, you would have seen.

It kept printing so long you thought it would never stop, but it would in fact have stopped eventually. After the addition of x = x + 1;, that is. Find out about x++; as shorthand for that line.
[edit]I see Jeff has already introduced you to x++[/edit]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic