Hello All,

Now please do not laugh at me, I am trying my best here. As you all know, I am taking a Java programming introductory course at the local city college, and the instructor has mentioned that any program that contains code that he did not discuss in class will not be graded. So, now I have to format numbers in a way not using anything but what he has covered.

I am writing a program that converts a user input of a Long int number of seconds into minutes, hours and days and centuries and what have you.

I have the code working, but what I need is to make a method that would format the numbers in this fashion:

XXX,XXX,XXX,XXX

I need to find out how to Convert a Long Integer into a String. When I tried I got:

TM.java:36: long cannot be dereferenced
sNum = Num.toString();
^
1 error

Num is passed from the main method, to the method that would format the numbers, and declared as Long. sNum is declared as a String

Then I can have a loop to chop the Long Integer number into 3s and concatenate a "," as I go along.

I know there must be a Java function for that, but he has not shown us that function.

Thanks for any ideas and hints.

Recommended Answers

All 3 Replies

You can use String.valueOf(long) to get a long into a string.

If the instructor doesn't even allow the String API then...

public class Number_Manip{
	public static void main(String[] args){
		System.out.println(Number_Manip.numberToString(new Integer(8)));
	}

	public static String numberToString(Number num){
		return  "" + num;
	}
}

Well, re-reading the problem description

Long int number of seconds into minutes, hours and days and centuries and what have you.

has nothing to do with converting a single long into a string and inserting a comma between sets of three digits. It's a matter of applying the appropriate division operations to determine the desired unit of measure intervals and building a result string with that info. i.e.

System.out.println(centuries + ", " + days + ", " + hours + ", " + minutes + ", "+seconds);

or using the formatted printf() function to format the result string.

Not knowing what you have covered in class, it's hard to say what the instructor is expecting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.