8 Replies - 7774 Views - Last Post: 03 November 2008 - 08:53 PM Rate Topic: -----

#1 westforduk  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 140
  • Joined: 16-August 07

Padding with 0

Posted 03 November 2008 - 02:22 PM

:D

This post has been edited by westforduk: 04 November 2008 - 10:10 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Padding with 0

#2 stauffski  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 210
  • Joined: 03-November 07

Re: Padding with 0

Posted 03 November 2008 - 02:32 PM

This is happening because of your mod line. Test your minutes to see if they are less than 10. If so, add a zero to the front. Do this by using the String insert method and creating a String variable minutes then creating the return String.

Good Luck!
Was This Post Helpful? 0
  • +
  • -

#3 westforduk  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 140
  • Joined: 16-August 07

Re: Padding with 0

Posted 03 November 2008 - 02:43 PM

View Poststauffski, on 3 Nov, 2008 - 01:32 PM, said:

This is happening because of your mod line. Test your minutes to see if they are less than 10. If so, add a zero to the front. Do this by using the String insert method and creating a String variable minutes then creating the return String.

Good Luck!



Thanks for the help.

Problem is I can't use loops its not allowed in the brief. :crazy: So comparing the numbers to ANYTHING is out of the question!I was thinking maybe a String.format or the printf functions might be of some use?
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,207
  • Joined: 07-September 06

Re: Padding with 0

Posted 03 November 2008 - 02:44 PM

You need to check the lengths of each string (minutes and hours), so here is some code that works (I believe) like you want it to:
	public static String toTime(int totalMinutes){
		int Mins;
		int Hrs;
		String minutes = new String();
		String hours = new String();
		Hrs = (int)(totalMinutes/60);
		Mins = (totalMinutes % 60);
		minutes = String.valueOf(Mins);
		if(minutes.length() < 2){
			minutes = "0"+minutes;
		}
		hours = String.valueOf(Hrs);
		if(hours.length() < 2){
			hours = "0"+hours;
		}
		return hours+":"+minutes;
	}


Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#5 westforduk  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 140
  • Joined: 16-August 07

Re: Padding with 0

Posted 03 November 2008 - 02:48 PM

Thanks BetaWar

That seems to do it BUT I am not allowed to use loops its in the brief!
There must be some way to do it :(
Was This Post Helpful? 0
  • +
  • -

#6 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Padding with 0

Posted 03 November 2008 - 02:52 PM

The code that BetaWar gave does not use loops, the code only uses if statements, which are not loops because loops are meant to execute 0 or more times whereas if statements are meant to execute once.
Was This Post Helpful? 0
  • +
  • -

#7 westforduk  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 140
  • Joined: 16-August 07

Re: Padding with 0

Posted 03 November 2008 - 02:54 PM

View PostBigAnt, on 3 Nov, 2008 - 01:52 PM, said:

The code that BetaWar gave does not use loops, the code only uses if statements, which are not loops because loops are meant to execute 0 or more times whereas if statements are meant to execute once.


Sorry, I am not being clear.

No selection is allowed at ALL!

Sorry Guys. Hope I'm not being annoying! :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#8 westforduk  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 140
  • Joined: 16-August 07

Re: Padding with 0

Posted 03 November 2008 - 03:14 PM

Done it now thanks guys for helping me!


	 Hrs = String.format("%02d",(int)(totalMinutes/60)%24);
	 Mins = String.format("%02d",(totalMinutes % 60));


Seems to have work! :P
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Padding with 0

Posted 03 November 2008 - 08:53 PM

View PostBigAnt, on 3 Nov, 2008 - 01:52 PM, said:

The code that BetaWar gave does not use loops, the code only uses if statements, which are not loops because loops are meant to execute 0 or more times whereas if statements are meant to execute once.

You can start a debate about that... if are loops now... may be in your mind

So format with the good old C format %02d
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1