File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Coding Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Coding Help" Watch "Coding Help" New topic
Author

Coding Help

Colton Herren
Greenhorn

Joined: May 26, 2012
Posts: 7
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

Joined: May 26, 2003
Posts: 31074
    
232

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?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Colton Herren
Greenhorn

Joined: May 26, 2012
Posts: 7
yeah when I try to compile it it says cannot read 1 error.
Colton Herren
Greenhorn

Joined: May 26, 2012
Posts: 7
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6



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

Joined: May 26, 2012
Posts: 7
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

Joined: May 26, 2012
Posts: 7
so it wont end because x will always be bigger than 3
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: May 26, 2012
Posts: 7
Oh I got it I didn't realize that's what I had made. Sorry
Colton Herren
Greenhorn

Joined: May 26, 2012
Posts: 7
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're welcome! Glad you got it sorted out.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Coding Help