Jump to content

Need help with simple program.

Go to solution Solved by jimakos234,

Damn i had to mess around with java 2-3 years...

        NumberFormat integerFormat = NumberFormat.getIntegerInstance();        NumberFormat integerFormat2 = NumberFormat.getIntegerInstance();        integerFormat.setMinimumIntegerDigits(3);        integerFormat.setGroupingUsed(false);        integerFormat2.setMinimumIntegerDigits(4);        integerFormat2.setGroupingUsed(false);        Random generator = new Random();        int num123, num456, num7890;        do{ //if it cant contain 8 or 9 u had it set at 777, which is wrong, because 689 would be a correct input from the programm            num123 = generator.nextInt(999) + 1;                    }while(String.valueOf(num123).contains("8")||String.valueOf(num123).contains("9"));        num456 = generator.nextInt(743) + 1;        num7890 = generator.nextInt(9999) + 1;        System.out.println("Your new random phone number is " + integerFormat.format(num123)                 + "-" + integerFormat.format(num456) + "-" + integerFormat2.format(num7890)); 

Ok so i need help with a simple program that i have almost gotten all of it correct. So basically i need to have a phone number generated randomly every time it is run and the first 3 digits cannot have a 8 or 9 and the second set of 3 digits cannot be higher than 743. I have gotten almost all of it except for the fact that i cannot seem to figure out why when a number is lower than 3 digits for the first 3 numbers it doesnt add a 0 in front of it. For example it will print a number like 705 for the last 4 digits when i need it to print 0705. Anyways heres my code if anybody can help me out, should be pretty easy its a high school level program.

 

import java.util.Random;
public class phoneNumber
{
  public static void main (String[] args)
  {
    Random generator = new Random();
    int num123, num456, num7890;
    num123 = generator.nextInt(777) + 1;
    if (num123 <= 99) num123 = (+ 0 + num123);
    
    num456 = generator.nextInt(743) + 1;
    if (num456 <= 99) num456 = (+ 0 + num456);
      
    num7890 = generator.nextInt(9999) + 1;
    if (num7890 <= 999) num7890 = (+ 0 + num7890);
    
    System.out.println("Your new random phone number is " + num123 + "-" + num456 + "-" + num7890);
  }
}
 
P.S. Sorry if my code is inefficient or isnt very good im pretty new to coding.
Edited by Justin Meyer

CPUi7 4790k (4.0 Ghz)| MotherboardMSI Z97 Gaming-5 ATX LGA1150| RAM: 16 Gb Corsair Vengeance Pro(DDR3-1600, Red 4x4Gb)GPUEVGA GTX 980 4GB Superclocked ACX 2.0| CaseFractal Design Define R5 (Black with Window)| StorageWestern Digital Caviar Blue 1TB and Samsung 850 EVO 250 GB SSD| PSUCorsair HX750i 80+ Platinum| Display(s) Asus VG248QE 144Hz 24.0" CoolingCorsair H100i GTX| Keyboard: Corsair K95 RGB| MouseRazer Deathadder Chroma|

Link to comment
https://linustechtips.com/topic/450137-need-help-with-simple-program/
Share on other sites

Link to post
Share on other sites

num7890 = (+ 0 + num7890);

You are doing something like 1+0 and you expect to get 01

You will get 1+0=1

So how do i fix it?

CPUi7 4790k (4.0 Ghz)| MotherboardMSI Z97 Gaming-5 ATX LGA1150| RAM: 16 Gb Corsair Vengeance Pro(DDR3-1600, Red 4x4Gb)GPUEVGA GTX 980 4GB Superclocked ACX 2.0| CaseFractal Design Define R5 (Black with Window)| StorageWestern Digital Caviar Blue 1TB and Samsung 850 EVO 250 GB SSD| PSUCorsair HX750i 80+ Platinum| Display(s) Asus VG248QE 144Hz 24.0" CoolingCorsair H100i GTX| Keyboard: Corsair K95 RGB| MouseRazer Deathadder Chroma|

Link to post
Share on other sites

So how do i fix it?

The simple and cheap way would be to use some if conditions before printing checking if the number is lower than 3 digits.

Otherwise give me a sec, i have not a java ide installed since i reinstalled windows

The site has changed....

Link to post
Share on other sites

I have gotten almost all of it except for the fact that i cannot seem to figure out why when a number is lower than 3 digits for the first 3 numbers it doesnt add a 0 in front of it. For example it will print a number like 705 for the last 4 digits when i need it to print 0705.

One general approach for this (which you should be able to reuse lots of times when outputting information) is to use a NumberFormat. The NumberFormat can be customised for your use (in this case, the minimum of 4 digits, which will prepend a '0' on numbers of less than 1000) and it doesn't alter the underlying data - only the presentation of said data. Here's an example for your case:

NumberFormat integerFormat = NumberFormat.getIntegerInstance();integerFormat.setMinimumIntegerDigits(4);integerFormat.setGroupingUsed(false);int[] numbersToPrint = {1, 10, 100, 1000};for(int numberToPrint : numbersToPrint) {	System.out.println("numberToPrint: "+integerFormat.format(numberToPrint));}
Output:

numberToPrint: 0001numberToPrint: 0010numberToPrint: 0100numberToPrint: 1000
Link to post
Share on other sites

Damn i had to mess around with java 2-3 years...

        NumberFormat integerFormat = NumberFormat.getIntegerInstance();        NumberFormat integerFormat2 = NumberFormat.getIntegerInstance();        integerFormat.setMinimumIntegerDigits(3);        integerFormat.setGroupingUsed(false);        integerFormat2.setMinimumIntegerDigits(4);        integerFormat2.setGroupingUsed(false);        Random generator = new Random();        int num123, num456, num7890;        do{ //if it cant contain 8 or 9 u had it set at 777, which is wrong, because 689 would be a correct input from the programm            num123 = generator.nextInt(999) + 1;                    }while(String.valueOf(num123).contains("8")||String.valueOf(num123).contains("9"));        num456 = generator.nextInt(743) + 1;        num7890 = generator.nextInt(9999) + 1;        System.out.println("Your new random phone number is " + integerFormat.format(num123)                 + "-" + integerFormat.format(num456) + "-" + integerFormat2.format(num7890)); 

The site has changed....

Link to post
Share on other sites

Awesome, thanks everyone i fixed it and now i just gotta turn it in. I appreciate the help...

CPUi7 4790k (4.0 Ghz)| MotherboardMSI Z97 Gaming-5 ATX LGA1150| RAM: 16 Gb Corsair Vengeance Pro(DDR3-1600, Red 4x4Gb)GPUEVGA GTX 980 4GB Superclocked ACX 2.0| CaseFractal Design Define R5 (Black with Window)| StorageWestern Digital Caviar Blue 1TB and Samsung 850 EVO 250 GB SSD| PSUCorsair HX750i 80+ Platinum| Display(s) Asus VG248QE 144Hz 24.0" CoolingCorsair H100i GTX| Keyboard: Corsair K95 RGB| MouseRazer Deathadder Chroma|

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×