954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help with string concatenation operator

my assigment tell me to:

Write a program to randomly generate a 3-digit number N (it is ok if the number has
fewer than 3 digits). Create a number NN which is N placed next to it. Example, if
the number N is 263 then NN is 263263. If N is 7, NN is 007007. If N is 032, NN is
032032.
Next, divide NN by 7, and then by 11 and then 13.
Write the original number and the final result after the divisions.
Formula to randomly generate a 3-digit number is
(int)Math.floor(Math.random()*1000+1)

this is what I came up with but comes up with errors. Can I get some help

ublic class chapter3_problem_2 {

public static void main(String[] args) {
int n = (int)Math.floor(Math.random()*1000+1);
int nn = ("n" + "n");


System.out.println( +nn);

}
}

what am I doing wrong?

tariq1081
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

Uhm, you are trying to assign a string an integer. I don't really know what happens, but that migt be the problem. Try defining a String strNN, and then assigning it n + n (careful so you don't evalauate n + n). Then you could define int nn = Integer.parseInt(strNN)

Edit: Also, use tags from now on please

I hope this helps on your homework!
Emil Olofsson

emilo35
Junior Poster
103 posts since Feb 2010
Reputation Points: 14
Solved Threads: 12
 

You could use a java.text.DecimalFormat object to help format your integer into a string with 3 decimal places.

java.text.DecimalFormat threePlaces = new java.text.DecimalFormat("000");
strNN = threePlaces.Format(n) + threePlaces.Format(n);
armsracer
Newbie Poster
7 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You