2

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.

noobiecderssss
  • 71
  • 3
  • 4
  • 10
  • I'm not seeing an actual question in the body of this question? – neminem Mar 19 '14 at 20:36
  • @neminem his question is "why that works" i.e. (why adding "" at the beggining of String number, prints each value instead of adding num1+num2 and printing the sum) – Frakcool Mar 19 '14 at 20:39

7 Answers7

3

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());
Serge
  • 608
  • 4
  • 12
  • 24
  • 2
    String is immutable, so using a StringBuilder is the preferred way of doing this. – Serge Mar 19 '14 at 20:24
  • 2
    Yes you are correct. Because of the fact that String is immutable the String concatenation method is much less efficient and slower. – The amateur programmer Mar 19 '14 at 20:37
  • String concatenation via "+" uses a StringBuilder behind the scenes, so if you concatentate them all at once, there is no advantage to explicitly creating a StringBuilder/StringBuffer. http://docs.oracle.com/javase/8/docs/api/java/lang/String.html – Sam Barnum Mar 20 '14 at 14:18
3

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);
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • That doesn't work. This is the exact same problem. You are adding integers,not concatenating in the first code sample. See Sam Barnum's post – StreamingBits Mar 19 '14 at 20:27
  • No @StreamingBits, you're not adding integers, you're concatinting... he said he's new at coding, I added an explanation of how that code works, and why "" before integers will print them 1 by 1 instead of adding them – Frakcool Mar 19 '14 at 20:30
  • Interesting, didn't know you could do the "" thing. +1 – StreamingBits Mar 19 '14 at 20:36
1

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.

Sam Barnum
  • 10,559
  • 3
  • 54
  • 60
1

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);
StreamingBits
  • 137
  • 11
1
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???
  • Naman as you suggested String s2 = ""+ num1 + num2; //This will result in 23. I did it in my code above mon =""+i+mon ; is not being accepted shwing error chnage type of mon to String...Why??? I need to concate 0 with the mon value. – user5081873 Jul 05 '15 at 07:03
0

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() .

The amateur programmer
  • 1,238
  • 3
  • 18
  • 38
0

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.

Nimesh
  • 794
  • 3
  • 8
  • 18