aspose file tools*
The moose likes Beginning Java and the fly likes Printing data in an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing data in an array" Watch "Printing data in an array" New topic
Author

Printing data in an array

J. Chris Miller
Ranch Hand

Joined: Sep 11, 2012
Posts: 30
I want to print out an object with 2 variables. The variables to be printed are a string which is a brand of tool, and a double which is the price. It's only printing out SimpleTool@7aca2076 or other random numbers after. Can you help me please? Thanks.

Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 971
    
  16

You should override the toString() method of Object class in your SimpleTool class.


Formely Pawel Pawlowicz
J. Chris Miller
Ranch Hand

Joined: Sep 11, 2012
Posts: 30
Pawel Pawlowicz wrote:You should override the toString() method of Object class in your SimpleTool class.


Can you please give me the syntax on how to do that?

Here is my attempt, which isn't working:

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7794
    
  21

J. Chris Miller wrote:Here is my attempt, which isn't working:

ItDoesntWorkIsUseless (←click). Your attempt is very close to what I'd have written (except I'd have put "@Override" on the method), so look some more, or TellTheDetails.

Hint: the problem is not with your method definition.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
J. Chris Miller
Ranch Hand

Joined: Sep 11, 2012
Posts: 30
Winston Gutkowski wrote:
J. Chris Miller wrote:Here is my attempt, which isn't working:

ItDoesntWorkIsUseless (←click). Your attempt is very close to what I'd have written (except I'd have put "@Override" on the method), so look some more, or TellTheDetails.

Hint: the problem is not with your method definition.

Winston


Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.lang.String.format(Unknown Source)
at SimpleTool.toString(SimpleTool.java:17)
at java.util.Formatter$FormatSpecifier.printString(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at SimpleTool.<init>(SimpleTool.java:12)
at SimpleToolDemo.main(SimpleToolDemo.java:28)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7794
    
  21

J. Chris Miller wrote:Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String

Right. So look carefully at that error message. What is it saying?

You've already got it right elsewhere in your program, so you really should be able to work this one out for yourself.

Winston
J. Chris Miller
Ranch Hand

Joined: Sep 11, 2012
Posts: 30
Winston Gutkowski wrote:
J. Chris Miller wrote:Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String

Right. So look carefully at that error message. What is it saying?

You've already got it right elsewhere in your program, so you really should be able to work this one out for yourself.

Winston


So, if I could figure it out myself, I wouldn't be posting on here. Man I really don't understand the point of this forum. I can watch a tutorial and get all the info I need, but whenever I come on here it's "guess" this and "hint" that. I am a beginner level programmer. If I had to guess what the error message is referring to, it's saying d does not equal the proper format for String, whatever that means. Anyway, you don't want to help. You want to sit there with your knowledge and watch me struggle. So whatever, take care.

Oh, and don't wink at me condescendingly either. Just help, or get out.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7794
    
  21

J. Chris Miller wrote:So, if I could figure it out myself, I wouldn't be posting on here. Man I really don't understand the point of this forum. I can watch a tutorial and get all the info I need, but whenever I come on here it's "guess" this and "hint" that. I am a beginner level programmer. If I had to guess what the error message is referring to, it's saying d does not equal the proper format for String, whatever that means. Anyway, you don't want to help. You want to sit there with your knowledge and watch me struggle. So whatever, take care.

Oh, and don't wink at me condescendingly either. Just help, or get out.

Chris, read the HowToAskQuestionsOnJavaRanch page. If you want a forum that simply hands you out answers then, trust me, you've come to the wrong place. And any post that does so will be deleted.

What I will tell you is that your interpretation of the error message is absolutely spot on. Now go find out why.

ShowSomeEffort and we're happy to help. Get snotty and you'll get short shrift here.

Winston
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41855
    
  63
What Winston is saying that you're using "%d" for formatting both a double and a string. What do you think "%d" means? If you're not sure, it's explained in the javadocs of the java.util.Formatter method. After reading that, the error message should become clearer.

As to the point of these forums, it is very much not to hand out straight answers, but to help people to learn. That frequently involves guiding them towards a solution, which generally makes for a better learning experience, even if it does not feel so along the way.


Ping & DNS - my free Android networking tools app
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

J. Chris Miller,

Since this topic had degenerated to the Javaranch "how to answer questions" policy, the post was moved to a new topic.

Please followup on the discussion there -- this topic will remain on the topic of the original question.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
J. Chris Miller
Ranch Hand

Joined: Sep 11, 2012
Posts: 30
Henry Wong wrote:J. Chris Miller,

Since this topic had degenerated to the Javaranch "how to answer questions" policy, the post was moved to a new topic.

Please followup on the discussion there -- this topic will remain on the topic of the original question.

Henry


Can you help me solve my problem Henry? I read through that javadoc, and it's not helping.

I need to know how to format this piece of code:



I realize that %d probably means double, or decimal, or date. It's ambiguous in the document as to which. But I can't find anywhere in the doc that says, here's how to let the first argument remain a string, while converting the second argument. Also again, I do not see the syntax for either. Can you help, please?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

J. Chris Miller wrote:
I realize that %d probably means double, or decimal, or date. It's ambiguous in the document as to which. But I can't find anywhere in the doc that says, here's how to let the first argument remain a string, while converting the second argument. Also again, I do not see the syntax for either. Can you help, please?


See JavaDoc for the formatter here ... java.util.Formatter.

Henry
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Well, obviously that particular piece of documentation isn't beginner-friendly. In fact the first time I looked at it (after 10 years of Java programming) it took me a long time to make any sense out of it. Because frankly I hardly ever use it. If I want to make some text and plug some variables into it, I have always used a MessageFormat object. And if I was writing a small piece of code like what you're writing, I wouldn't bother with String.format or System.out.printf either. Those (in my opinion) are for C programmers who are used to that sort of stuff.

However I'm not recommending that you switch to MessageFormat. Just another thing to learn from scratch. So the "conversion" you want from the Formatter class is the one which isn't obviously for something which is something other than String. That reduces your choices to the ones with Argument Category of "general". You don't want "true/false" and you don't want a hex string, so that reduces your choices to only one -- %s. For now just take it for granted that the obscure description to the right of that code includes String objects.
J. Chris Miller
Ranch Hand

Joined: Sep 11, 2012
Posts: 30
For anyone needing help after searching this topic, I found a tutorial that solved my problem in seconds: http://www.youtube.com/watch?v=49MCrx53BOI

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

Paul Clapham wrote:Well, obviously that particular piece of documentation isn't beginner-friendly. In fact the first time I looked at it (after 10 years of Java programming) it took me a long time to make any sense out of it. Because frankly I hardly ever use it. If I want to make some text and plug some variables into it, I have always used a MessageFormat object. And if I was writing a small piece of code like what you're writing, I wouldn't bother with String.format or System.out.printf either. Those (in my opinion) are for C programmers who are used to that sort of stuff.

However I'm not recommending that you switch to MessageFormat. Just another thing to learn from scratch. So the "conversion" you want from the Formatter class is the one which isn't obviously for something which is something other than String. That reduces your choices to the ones with Argument Category of "general". You don't want "true/false" and you don't want a hex string, so that reduces your choices to only one -- %s. For now just take it for granted that the obscure description to the right of that code includes String objects.


Interestingly, I never had a issue with this. I am thinking it is because I come from a C background, and the formatter is pretty close to the printf() syntax.

Henry
J. Chris Miller
Ranch Hand

Joined: Sep 11, 2012
Posts: 30
Paul Clapham wrote:Well, obviously that particular piece of documentation isn't beginner-friendly.


Thank you for recognizing that. I honestly have a very difficult time reading javadocs, much less finding a solution within them. I don't see how this forum is for beginners. I just don't see it.

As for your solution, it's another cryptic javadoc to read. I know the forum rules prohibit you from helping me in any finer detail, so I appreciate your gesture anyway. Thanks again for understanding my position as a beginner programmer :)
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
J. Chris Miller wrote:I know the forum rules prohibit you from helping me in any finer detail

But Paul has helped you in finer detail. In fact he's given you the answer.
 
 
subject: Printing data in an array