Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Having problems putting some conditions

 
daisy polly
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
daisy polly
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9068
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
daisy polly
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint... You keep checking "accountString" to see if the length is okay. However, do you ever change the variable?

Henry
 
daisy polly
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still get 7 digit numbers.I think it contains the correct value.
 
Henry Wong
author
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I get It. thanx a bunch It WORKS!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic