• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

object & int cannot be converted to a string?  RSS feed

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting cannot convert object to string and int to string error messages that I don't understand.  This is the start of my Fraction Calculator program but as is it won't even compile yet.


Moderator edit: added code tags
 
Rancher
Posts: 3983
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When posting code can you put it in code tags (there's a button for that) so it keeps it's formatting.
Makes it easier to read.

In the meantime, what lines are giving you the error?
Can you post the compilation error here (not as an image, as text)?
 
Sheriff
Posts: 13366
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The most likely culprit(s) is(are) the calls to JOptionPane methods. Make sure your arguments match up to the correct parameters. This is the problem with long parameter lists: past the fourth or fifth argument, you often lose track of which value is going to which parameter.
 
Marshal
Posts: 63791
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I tried your code and you have six compile‑time errors. Unfortunately Java8+ has tried too hard to be helpful and the error messages can be a bit long‑winded. Let's have a look at the first error, where you are using this method. I take Junilu's point; you are liable to confusion with seven parameters, but you seem to have them in the right order. (A later error is probably caused by your getting the arguments in the wrong order.) If you look at the link, you will see that method's return type is Object. In fact, that call will return one of the four Strings "+" "-" "*" "/", but you are assigning that result to the variable sFract2, which is type String. The only way you are going to get that to work is to cast it: sChoice = (String)JOptionPane.showInputDialog(...); If you go through the link I gave you and scroll up and down, you will find several overloaded versions of showInputDialog(...), and that some of them return String and some return Object. In the cases of trying to convert an int to a String, try wrapping the entire expression in String.valueOf(...)

Unfortunately I can see lots more about your code. Why are you using option panes for keyboard input, or StringTokenizer? In fact, if you read the last link, you will see that they don't recommend you use StringTokenizer any more. Both are very old‑fashioned ways of programming, the sort of thing you would have found in a book printed in 1999. And many people would replace DecimalFormat with the String#format method or similar.
Another thing I can see about it is that the main() method is much too long. It isn't good to try to squeeze so much code into one method. You will see the ideal length of a main() method is one statement; you could probably divide that method into twenty smaller methods.
Also, you may not have been taught object orientation, but that code isn't object oriented. I would have a FractionCalculation class with the two numerators and denominators as fields, so passing 1 2 3 4 will produce an object which does arithmetic with ½ and ¾.Be descriptive with your variable names; n1 and n2 don't have obvious meanings. Don't declare multiple variables on the same line. And GarciaP1 isn't a good name for a class; GarciasFractionCalculator might be better, but most people wouldn't include their name in the class name.
You will have a much better application if you implement those things.

Search the Java® Language Specification for enums and go through that section; you will find a different way to create objects that do arithmetic there.
 
Charlie Garc
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are my errors as of right now.  I will look over my joptionpanes again. I have to learn Java this way so that I will be able to read anything written in old java...

GarciaP1.java:48: error: bad operand type String for unary operator '+++'
                               +"\n + \n - \n * \n /",
                               ^
GarciaP1.java:52: error: cannot find symbol
                       int choice = integer.ParseInt(sChoice);
                                    ^
 symbol:   variable integer
 location: class GarciaP1
GarciaP1.java:74: error: incompatible types: int cannot be converted to String
                               sResults = ((n1*d2) + (n2*d1))/(d1*d2);
                                                             ^
GarciaP1.java:79: error: incompatible types: int cannot be converted to String
                               sResults = ((n1*d2) - (n2*d1))/(d1*d2);
                                                             ^
GarciaP1.java:91: error: incompatible types: int cannot be converted to String
                               sResults = ((n1/d1) * (d2/n2));
                                                   ^
GarciaP1.java:159: error: variable sAgain is already defined in method main(String[])
                       int sAgain = JOptionPane.showInputDialog(null,
                           ^
GarciaP1.java:159: error: no suitable method found for showInputDialog(<null>,String,String,int,int)
                       int sAgain = JOptionPane.showInputDialog(null,
                                               ^
   method JOptionPane.showInputDialog(Object) is not applicable
     (actual and formal argument lists differ in length)
   method JOptionPane.showInputDialog(Object,Object) is not applicable
     (actual and formal argument lists differ in length)
   method JOptionPane.showInputDialog(Component,Object) is not applicable
     (actual and formal argument lists differ in length)
   method JOptionPane.showInputDialog(Component,Object,Object) is not applicable
     (actual and formal argument lists differ in length)
   method JOptionPane.showInputDialog(Component,Object,String,int) is not applicable
     (actual and formal argument lists differ in length)
   method JOptionPane.showInputDialog(Component,Object,String,int,Icon,Object[],Object) is not applicable
     (actual and formal argument lists differ in length)
7 errors
 
Campbell Ritchie
Marshal
Posts: 63791
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer not integer, and parseInt not ParseInt.
Don't declare the same variable twice. If you wrote int choice twice, you would have to delete int from the second occurrence.
You will have to go through the methods of option pane to check which method takes which parameters.
I don't understand the message about +++. Please correct the other errors and see what happens about that error.
 
Campbell Ritchie
Marshal
Posts: 63791
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Charlie Garc
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm making a fraction calculator program and it was running fine until I wrote the if statement.. Now I am getting incompatible types error:  int cannot be converted to string.  Here is my code and the error messages I am receiving.  I will keep looking at it.. Thanks for any help.



Garcia_P1.java:69: error: incompatible types: int cannot be converted to String
                               sResults = ((n1*d2) + (n2*d1)) / (d1*d2);
                                                              ^
Garcia_P1.java:74: error: incompatible types: int cannot be converted to String
                               sResults = ((n1*d2) - (n2*d1)) / (d1*d2);
                                                              ^
Garcia_P1.java:85: error: incompatible types: int cannot be converted to String
                               sResults = ((n1/d1) * (d2/n2));
                                                   ^
3 errors
 
Bartender
Posts: 689
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sResults is declared as a String.
((n1*d2) + (n2*d1)) / (d1*d2) resolves to an int.
You cannot assign an int to a String.

You could, however, do one of these:
or
 
Campbell Ritchie
Marshal
Posts: 63791
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have already, yesterday, suggested a solution to that particular error, wrapping the expression in String.valueOf(...), which is quite similar to Fred K's second suggestion.
I think this is a continuation of the same question, so I have merged your three questions in one threa‍d.
 
Charlie Garc
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That helped.  Thank you.
Is this how I now convert my string back to an int so that I can reduce the fraction?

 
Charlie Garc
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disregard the last question, I figured that part out. Thank you for your help!
 
Campbell Ritchie
Marshal
Posts: 63791
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Charlie Garc wrote:. . . Is this how I now convert my string back to an int so that I can reduce the fraction? . . .

That isn't the right question. I think you shouldn't be intercovnerting ints and Strings in the first place. Why go int→String→int? That is because you are not using objects. If you were using objects, you would have a Fraction class and you could create a Fraction object. If you go back to my code in my post on Friday morning, you can do this sort of thing...but the add() method returns a Fraction object.The toString() method of that Fraction object might return “1¼”.
Beware: if you look at this Unicode chart and this one, you will find that the repertoire of fraction characters is restricted and there isn't a fraction symbol ²/₇. You can get that with \u00b2;/\u20087 however.
 
Campbell Ritchie
Marshal
Posts: 63791
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shall leave you to find the spelling error in my last post; the compiler would find it easily enough. Sorry.
 
Charlie Garc
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My assignment is to contain all of my code within the main method.. My next program will be to separate it out...
 
Junilu Lacar
Sheriff
Posts: 13366
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Charlie Garc wrote:My assignment is to contain all of my code within the main method.. My next program will be to separate it out...


Really? Wow. It's a little mind-boggling that keeping everything in main() is actually part of the assignment's requirements. It might be understandable if the context for that was the instructor saying something like, "Ok, putting all your logic in main() is not really a good thing but just do that for now because our next topic will deal with methods and how to break down your program into smaller, more manageable pieces." Otherwise, just saying that you should put everything in main is misleading at best.

 
Forget Steve. Look at this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!