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:


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.

8 Years
Discussion Span
Last Post by Ezzaral

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.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.