wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Having problems putting some conditions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Having problems putting some conditions" Watch "Having problems putting some conditions" New topic
Author

Having problems putting some conditions

daisy polly
Greenhorn

Joined: Oct 10, 2005
Posts: 10
Hi,
I am having problem's putting a simple if conditions in my code.I have an Account class which produces numbers. I have some conditions in my code already for examples the length of the number should not be greater that 6.What I want is that for example when my program runs out of numbers I want to print and Error message and then quit.My code is:



public class AccountNumberMaker{

private int nextAvailableAccountNumber = 1000090;
String accountString = (Integer.toString(nextAvailableAccountNumber));

public int nextAccountNumber()
{
while(accountString.length() == 6)
{

nextAvailableAccountNumber++;

int i_first = nextAvailableAccountNumber /100000;
int i_last = nextAvailableAccountNumber %100000;

if( (i_first + i_last) % 10 == 0 )
{
return nextAvailableAccountNumber;
}
}

return nextAvailableAccountNumber;

}
}

Thanks a bunch
Daisy
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

How can you tell when your program runs out of numbers? Can you describe it in words? In order to tell the computer what to do, we have to be able to first describe it to ourselves.

Also, note that while you are changing the nextAvailabel AccountNumber, the accountString never changes (and, in fact, always has a length of 7), so this method will only ever return one value.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
daisy polly
Greenhorn

Joined: Oct 10, 2005
Posts: 10
It runs out of numbers as soon as the number becomes 7 ditit long.I need a condition to do that. I can't seem to get the condition to work.
daisy polly
Greenhorn

Joined: Oct 10, 2005
Posts: 10
My code prints 7 digits while I want it to say "RUN OUT OF NUMBERS" as soon as it more than 6.

The code now look like this:


Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Hello, Daisy Polly

Welcome to JavaRanch.


I think if you add a System.out.println right after your line of code

nextAvailableAccountNumber++;

you'll see what the problem is.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
daisy polly
Greenhorn

Joined: Oct 10, 2005
Posts: 10
I don't understand what is wrong.I put the system.out statement and the numbers always remain the same. I still don't get why it bring numbers greater than 6.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Hint... You keep checking "accountString" to see if the length is okay. However, do you ever change the variable?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
daisy polly
Greenhorn

Joined: Oct 10, 2005
Posts: 10
I chaged the code but the same problems occurs.The variables are diffrent but they have the same data.Thanks a bunch for helping.If you can solve this problem i will be really grateful.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

First of all, what do you mean "the same problem still occurs"? Second, I see that you now create a string... but are they in sync? Meaning that when you check it for length, does it contain the correct value?

Henry
daisy polly
Greenhorn

Joined: Oct 10, 2005
Posts: 10
I still get 7 digit numbers.I think it contains the correct value.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Take a look at this code snippet...



After you increment the "nextAvailableAccountNumber" variable, is the "stringNextAvailableAccountNumber" variable correct? Shouldn't it get changed too?

Henry
daisy polly
Greenhorn

Joined: Oct 10, 2005
Posts: 10
Oh I get It. thanx a bunch It WORKS!!!
 
jQuery in Action, 2nd edition
 
subject: Having problems putting some conditions
 
Similar Threads
Program format - where to put main()?
stack and heap variable
Making a small Bank Application
Generate 6-Digit Random Number
Saving to a txt file