943,165 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 530
  • Java RSS
Mar 21st, 2010
0

help with string concatenation operator

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tariq1081 is offline Offline
1 posts
since Mar 2010
Mar 21st, 2010
0
Re: help with string concatenation operator
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
Last edited by emilo35; Mar 21st, 2010 at 12:29 pm.
Reputation Points: 11
Solved Threads: 8
Junior Poster in Training
emilo35 is offline Offline
74 posts
since Feb 2010
Mar 21st, 2010
0
Re: help with string concatenation operator
You could use a java.text.DecimalFormat object to help format your integer into a string with 3 decimal places.

JAVA Syntax (Toggle Plain Text)
  1. java.text.DecimalFormat threePlaces = new java.text.DecimalFormat("000");
  2. strNN = threePlaces.Format(n) + threePlaces.Format(n);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
armsracer is offline Offline
7 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: java problem
Next Thread in Java Forum Timeline: Alphabet Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC