DecimalFormat

I need to format a 6 digit number to a weird format

Page 1 of 1

2 Replies - 4809 Views - Last Post: 07 April 2007 - 01:15 PM Rate Topic: -----

#1 austinious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-September 06

DecimalFormat

Posted 07 April 2007 - 07:04 AM

		public static void main(String[] args)
{
		Scanner scan = new Scanner(System.in);
		DecimalFormat fmt = new DecimalFormat();
		fmt.toPattern("##.##.##");
		
		System.out.print("Enter a Time, no decimals");
		int time = scan.nextInt();
		
		System.out.println("Time: " + fmt.applyPattern(time));
		
 }



one of my lame efforts to get the following to work.

I'm writing a program for the Track coach to sort runners by time, and then assign them to a specific lane on the track. Easy peasy...

Problem. They need to give me up to a 6 digit number, and would prefer to get the number back in the following format "##:##.##"
I had thought of having them enter the 2 digit groups separately, but that would entail 3 sorts. I thought that them entering the time as a 6 digit number would let me do 1 sort, then I could reformat the number. I'm trying to find a method in Java, but no luck so far. As you can see, I'm spending more time on this, instead of the actual real program. Of course this is a sticking point, because if I take the easy way out, and have them enter 3 groups of 2 digits, I have to do a sort, then up to 10 subsort, and then...even more....

Hey, I could always send the number to a string and break it up and concatinate to get the right symbols....wow, that would be almost as painful.

I hope someone has a bright idea, I'm feeling rather stupid...

austinious

Is This A Good Question/Topic? 0
  • +

Replies To: DecimalFormat

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: DecimalFormat

Posted 07 April 2007 - 11:55 AM

a simple modification of: http://www.dreaminco.../snippet399.htm

DecimalFormat fmt = new DecimalFormat("00.00.00");
then to display it i believe you want the format option not applypattern:
fmt.format(time)
note that this method will likely insert leading zeros, if this is not wanted, use # instead.
Was This Post Helpful? 0
  • +
  • -

#3 austinious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-September 06

Re: DecimalFormat

Posted 07 April 2007 - 01:15 PM

I actually already tried this in ignorance and got...

Exception in thread "main" java.lang.IllegalArgumentException: Multiple decimal separators in pattern "00.00.00"
at java.text.DecimalFormat.applyPattern(DecimalFormat.java:2520)
at java.text.DecimalFormat.<init>(DecimalFormat.java:417)
at Test.main(Test.java:22)

Process completed.

Thanks anyway...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1