Domanda

New at coding, and I'm running some basic exercises to get used to the language. In this exercise I'm trying to generate a phone number with the following restrictions:

  1. 1st 3 digits cannot contain an 8 or 9
  2. 2nd set of 3 digits cannot be higher than 742

I've seen suggestions to add an empty string (which I have), but I don't understand why that works. For now, I'll be sticking with the following, even though I don't fully understand why it works.

num1 = rand.nextInt(7) + 1;
num2 = rand.nextInt(7) + 1;
num3 = rand.nextInt(7) + 1;
num4 = rand.nextInt(643) + 100;
num5 = rand.nextInt(1001) + 1000;

String number = "" + num1 + num2 + num3 + "-" + num4 + "-" + num5;

System.out.print("Your assigned phone number is: " + number);

EDIT: NEW CODE INCLUDES sb.append

    StringBuilder sb = new StringBuilder();

    int num1, num2, num3, num4, num5;

    num1 = rand.nextInt(7) + 1;
    num2 = rand.nextInt(7) + 1;
    num3 = rand.nextInt(7) + 1;
    num4 = rand.nextInt(643) + 100;
    num5 = rand.nextInt(1001) + 1000;

    sb.append(num1);
    sb.append(num2);
    sb.append(num3);
    sb.append("-");
    sb.append(num4);
    sb.append("-");
    sb.append(num5);

    //String number = "" + num1 + num2 + num3 + "-" + num4 + "-" + num5;

    System.out.print("Your assigned phone number is: " + sb.toString());

@Serge 's answer worked for me. Though it does seem to require more coding with all the sb.append calls I have to include. I've yet to learn about the StringBuilder class, but it definitely seems to be helpful. Thank you, everyone.

È stato utile?

Soluzione

Use a stringbuilder

StringBuilder sb = new StringBuilder();

sb.append(num1);
sb.append(num2);
sb.append(num3);
sb.append("-");
sb.append(num4);
sb.append("-");
sb.append(num5);


System.out.println("Your phone number is: " + sb.toString());

Altri suggerimenti

here's the explanation:

num1 = rand.nextInt(7) + 1; //This generates a random number between 1 and 7 (because rand.nextInt(n); returns a random number between 0 and n, then first number in a phone number can't be zero, that's why +1 is added.
num4 = rand.nextInt(643) + 100; // This one generates a random number between 1 and 643 and add 100 (because it can be 0 - 99 but that don't give us a number of 3 digits), so we add 100 and it will give us a number of 3 digits.
num5 = rand.nextInt(1001) + 1000; // returns a random number of 4 digits between 1000 and 1001, so basicaly: 1000 or 1001.

String number = "" + num1 + num2 + num3 + "-" + num4 + "-" + num5; //this will be the example:

number = "367-783-1001";
System.out.println("Your assigned phone number is: " + number);
Your assigned phone number is: 367-783-1001

It concatenates them because we're adding them into a String (text variable) instead of int (number variable)

The way they're adding the numbers is correct

Yeah if you add "" to the beggining the compiler will parse from int to String (I don't know why), but you can change it to:

String number = String.valueOf(num1) + String.valueOf(num2) + String.valueOf(num3) + "-" + String.valueOf(num4) + "-" + String.valueOf(num5);

Or:

String number = Integer.toString(num1) + Integer.toString(num2) + Integer.toString(num3) + "-" + Integer.toString(num4) + "-" + Integer.toString(num5);

The + operator behaves differently for Strings and numbers. When invoked on a String, it concatenates. When invoked on a numeric type, it adds. By starting with "", you are telling the compiler to concatenate a number to the string, which in turn converts the number to a String. This is repeated for all further concatenations.

The problem is that you are literally adding the num1, num2, and num3 etc. You need to do convert it to a string to concatenate. An easy way would be to do this.

   String no1 = Integer.toString(num1);
   String no2 = Integer.toString(num2); 

//DO THAT FOR EVERY numX and then your code will work

An alternative, is String builder

StringBuilder sb = new StringBuilder();
sb.append("");
sb.append(num1);
sb.append(num2);
sb.append(num3);
sb.append("-");
sb.append(num4);
sb.append(num5);
sb.append(num6);
sb.append(num7);
String phoneNumber = sb.toString();
System.out.println("PHONE NUMBER" + phoneNumber);
int mon = calendar.get(Calendar.MONTH);
    int year = calendar.get(Calendar.YEAR);
    int date = calendar.get(Calendar.DATE);
    int day = calendar.get(Calendar.HOUR_OF_DAY);
    int min = calendar.get(Calendar.MINUTE);
    int i = 0;


    if(mon<= 9)

         mon =""+i+mon ;

    String dates;
    if(date <= 9)
        dates = ""+'0'+date;

    int sec = calendar.get(Calendar.SECOND);
    String Time1 = date+"-"+mon+"-"+year+" "+day+":"+min+":"+sec;
    min=min+5;
    String Time2 = date+"-"+mon+"-"+year+" "+day+":"+min+":"+sec;

    Time1=Time1+"_"+Time2;

    ``System.out.println(date+"-"+mon+"-"+year+" "+day+":"+min+":"+sec);

    return Time1;

}



 mon =""+i+mon ; is not being accepted shwing error  chnage type of mon to String...Why???

In your solution int s are first changed to string representations of numbers and then they are concatenated to an empty String. This isn't a good practice because of the fact that String s are immutable.

Another way to accomplish this is by using a string builder to append the integer values as String representations into its buffer with StringBuilder.append(int) method. Then you can get the resulting string with StringBuilder.toString() .

Here you are adding blank quote in starting of String to mention that you want to perform string operation.

"+" operator works differently here.

If your string concatenation finds 2 numbers in starting of operation it will add num1 with num2.

For example,

int num1 = 2;
int num2 = 3;
String s = num1 +num2; //This will result in 5
String s2 = ""+ num1 + num2; //This will result in 23

That's why you need to put blank quote.

String is immutable so it is good choice to use StringBuilder for String Concatenation.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top