here's another one that i've been struggling with. I need to create a phone number that can not have an 8 or a 9 in the area code.
I thought it would be smart to simply generate the area code one number at a time
..............
num1 = generator.nextInt(7)+ 1;
num2 = generator.nextInt(7)+ 1;
num3 = generator.nextInt(7)+ 1;
....................
my thought was that by setting next int to 7 that it would prevent any 8's or 9's from displaying.......and by using the +1 that it would keep any zeros from displaying.

well.....apparently thats not the way to go because when i test my program i do occassionally get 8s or 9s........but whats even worse is that sometimes i can get a 1 digit or a 2 digit area code. I dont understand why I'm getting 8's and 9's when i'm specifying in my range that they should not appear........and i definately dont understand why i'm not getting a 3 digit value to appear in my ouput.

I think I might know the issue.

If you are saying in your code System.out.println(num1+num2+num3) to print the 3 numbers, your error is here. The print method is adding the integers together and then is printing them. If you add the integers to an empty string and print it as a string, or if you use 3 print statements instead of using the + operator, your problem should be solved.

Edited 6 Years Ago by kvass: n/a

I think I might know the issue.

If you are saying in your code System.out.println(num1+num2+num3) to print the 3 numbers, your error is here. The print method is adding the integers together and then is printing them. If you add the integers to an empty string and print it as a string, or if you use 3 print statements instead of using the + operator, your problem should be solved.

WOW!! that was it. I totally didnt realize that by using the '+' i was now adding instead of concatenating. UGHGH!! What a silly mistake. THANK YOU SO MUCH!

No problem xD I didn't realize either until I tried implementing your code and happened to come across the same error -- by debugging it i found that the error wasn't in the random generation, so I figured it must be in the print statement. But anytime!

This question has already been answered. Start a new discussion instead.