• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Having problems putting some conditions

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still get 7 digit numbers.I think it contains the correct value.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I get It. thanx a bunch It WORKS!!!
 
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic