Need to know why I am getting error and how to fix

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1624 Views - Last Post: 19 December 2011 - 04:02 PM

#1 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 256
  • Joined: 19-September 09

Need to know why I am getting error and how to fix

Posted 14 December 2011 - 08:57 PM

I'm very confused for I'm watching a video on youtube and this works fine

[code
public String toMilitary(){
return String.format("02d:02d:02d", hour, minute, second);
}
[/code]
I'm getting an error on the format say it can not accept this type of argument

here is the whole code for the class
public class tuna {
	private int hour;
	private int minute;
	private int second;
	
	public void setTime(int h, int m, int s){
		hour = ((h >= 0 && h < 24) ? h : 0);
		minute = ((m >= 0 && m < 60) ? m : 0);
		second = ((s >= 0 && s < 60) ? s : 0);
	}
	
	public String toMilitary(){
		return String.format("02d:02d:02d", hour, minute, second);
	}
}



Thanks your help is appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Need to know why I am getting error and how to fix

#2 pbl  Icon User is offline

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

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Need to know why I am getting error and how to fix

Posted 14 December 2011 - 09:04 PM

return String.format("%02d:%02d:%02d", hour, minute, second);

the % introduces a formatter
Was This Post Helpful? 0
  • +
  • -

#3 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 256
  • Joined: 19-September 09

Re: Need to know why I am getting error and how to fix

Posted 14 December 2011 - 10:53 PM

still not working, I've retyped it so much that I forgot that last time, here is the code


public class tuna {
	private int hour;
	private int minute;
	private int second;
	
	public void setTime(int h, int m, int s){
		hour = ((h >= 0 && h < 24) ? h : 0);
		minute = ((m >= 0 && m < 60) ? m : 0);
		second = ((s >= 0 && s < 60) ? s : 0);
	}
	
	public String toMilitary(){
		return String.format("%02d:%02d:%02d", hour, minute, second);
	}
}



The method format(String, Object[]) in the type String is not applicable for the arguments (String, int, int, int)

That is the error on format, why is it not allowing this type of argument?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Need to know why I am getting error and how to fix

Posted 14 December 2011 - 11:57 PM

I am sure this is OK
At what line is your error ?
Are you sure you show us the offending code ? That your actual code is not longer ?
Was This Post Helpful? 0
  • +
  • -

#5 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 256
  • Joined: 19-September 09

Re: Need to know why I am getting error and how to fix

Posted 15 December 2011 - 04:36 AM

yea positive, the error is on format, red line underneath it, hover over it and I get the above message. Did they change how format could be used in Java 7.0, I believe that is the version I am using with Eclipse. I've had similar problems saying quick fixes by going to Java 1.5, or when trying to develop for android like in the android manifest having to delete words like final in public final int before too. I agree what is above should work, but format seems like its not taking the right type of argument, any help getting this resolved would be appreciated, thanks.
Was This Post Helpful? 0
  • +
  • -

#6 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 256
  • Joined: 19-September 09

Re: Need to know why I am getting error and how to fix

Posted 15 December 2011 - 06:53 PM

Still needing an answer on this I can't continue on until I have it, thanks;)
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,475
  • Joined: 27-December 08

Re: Need to know why I am getting error and how to fix

Posted 15 December 2011 - 07:01 PM

Try wrapping the ints as Integers, or just passing an Object[] with the Integer values directly.
Was This Post Helpful? 1
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Need to know why I am getting error and how to fix

Posted 16 December 2011 - 08:04 PM

Are you on android ?
If it is the case seems that the method signature is: format(String, Object[])
Was This Post Helpful? 0
  • +
  • -

#9 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 256
  • Joined: 19-September 09

Re: Need to know why I am getting error and how to fix

Posted 18 December 2011 - 12:03 PM

Well this is under a java project on Eclipse, but yes it seems like the format your saying is the one that needs to be used. So how would I do that with my code ?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Need to know why I am getting error and how to fix

Posted 18 December 2011 - 03:12 PM

Assuming the method signature is
String.Format(String, Object[]);
you will have to

Object[] o = new Integer[3];
o[0] = new Integer(hour);
o[1] = new Integer(minute);
o[2] = new Integer(second);

return String.format("%02d:%02d:%02d", o);
Was This Post Helpful? 0
  • +
  • -

#11 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 256
  • Joined: 19-September 09

Re: Need to know why I am getting error and how to fix

Posted 18 December 2011 - 04:04 PM

interesting I'll try that when I get home, did the way I write not work due to the version of eclipse or java I'm running?
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

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

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Need to know why I am getting error and how to fix

Posted 18 December 2011 - 05:48 PM

The String class Format method signature is

http://docs.oracle.c...ava.lang.String, java.lang.Object...)

that was introcuced with JRE 1.5
String.format("%02d:%02d:%02d", 10, 20, 30);
works for me as (, Object...obj) and (, Object[] o) are equivalent in a signature

Eclipse is surely not the culprit
So you are using JRE 1.4 or lower, or you use Android of which I know nothing

But if you are using JRE 1.4 the whole format() method would be declared unknown not just it's signature. Are you sure your Eclipse is not configured to use Android ?
Was This Post Helpful? 0
  • +
  • -

#13 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 256
  • Joined: 19-September 09

Re: Need to know why I am getting error and how to fix

Posted 18 December 2011 - 06:10 PM

I'll have to check that to when I get home. It is configured for android, for I was learning to program for android first but then I started studying Java recently due to someone who has made me an offer to make some software for them. If my eclipse is configured for android how would I switch it for Java and back to android if need be?
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

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

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Need to know why I am getting error and how to fix

Posted 18 December 2011 - 06:16 PM

Never did it but I guess will be in
.Windows
.. Preference
... Java
.... Compiler
Was This Post Helpful? 0
  • +
  • -

#15 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 256
  • Joined: 19-September 09

Re: Need to know why I am getting error and how to fix

Posted 18 December 2011 - 08:54 PM

it says I'm using Java 1.6, should I change that?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2