Results 1 to 20 of 21
Thread: Having a problem with to String
 06012012, 01:28 PM #1Member
 Join Date
 Feb 2011
 Posts
 33
 Rep Power
 0
Having a problem with to String
Hello I am trying to learn java and I am having a problem adding and mulitiplying fractions and displaying it on a to String. My main class is called Rational and have two private ints numerator and denominator. I have a constructor with two objects: int newNumerator and newDenominator. I have two public accessors methods and two public moderator methods. I created two public methods: one named Rational multiply (Rational r) and Rational add (Rational r) ( this is where the problem starts). I put two temp variable inside the Rational multiply method and four temp variable inside the Rational add method. I did my algorithm for both and get no errors. I have for both to return new Rational (tempvariable1, tempvariable2) for both. Now when I start the toString method, I type my phrase and include my temp variables but there are error lines at each variable. Can someone please help me, I'll really appreciate it.
here is code for Rational methods:
Java Code:public Rational multiply (Rational r) { int tempnumer; int tempdenom; tempnumer = numerator * r.numerator; tempdenom = denominator * r.denominator; return new Rational(tempnumer, tempdenom); } public Rational add (Rational r) { int tempa; int tempb; int tempc; int tempd; tempa = denominator * r.denominator; tempb = r.denominator * numerator; tempc = denominator * r.numerator; tempd = tempb + tempc; return new Rational(tempd,tempa); } public String toString() { return "2/3 * 5/7 = " + tempnumer + "/" + tempdenom + "/n" + "2/3 + 5/7 = " + tempd + "/" + tempa; }
Last edited by Norm; 06012012 at 01:58 PM. Reason: added code tags
 06012012, 01:58 PM #2
Re: Having a problem with to String
there are error lines at each variable.If you don't understand my response, don't ignore it, ask a question.
 06012012, 02:01 PM #3Member
 Join Date
 Feb 2011
 Posts
 33
 Rep Power
 0
Re: Having a problem with to String
The error message is: cannot find symbol
symbol: variable tempnumer
location: class Rational
each temp variable has this error message
 06012012, 02:14 PM #4
Re: Having a problem with to String
The compiler can not find a definition for the variable shown in the message: tempnumber that is in scope (within the same pair of {}s) where the variable is being used.
Where are those variables defined? Make sure they are in scope where you are trying to use them.If you don't understand my response, don't ignore it, ask a question.
 06012012, 02:17 PM #5Member
 Join Date
 Feb 2011
 Posts
 33
 Rep Power
 0
Re: Having a problem with to String
The variable are only defined in those methods and nowhere else
 06012012, 02:29 PM #6
Re: Having a problem with to String
The compiler can not find the definitions for those variables that are in scope where you are trying to use them.
Do you know what "in scope" means?
Where is tempnumber defined?
Where is the error message?If you don't understand my response, don't ignore it, ask a question.
 06012012, 06:44 PM #7Member
 Join Date
 Feb 2011
 Posts
 33
 Rep Power
 0
 06012012, 06:52 PM #8
Re: Having a problem with to String
tempnumer(numerator) is defined inside the Rational multiply (Rational r) method and nowhere else.
The posted (post#3) error message does not show the line and line number where the compiler found the problem.If you don't understand my response, don't ignore it, ask a question.
 06012012, 07:04 PM #9Member
 Join Date
 Feb 2011
 Posts
 33
 Rep Power
 0
 06012012, 07:15 PM #10
Re: Having a problem with to String
Did you read and understand what I said in post #8? Especially this part:
You will not be able to access that variable from any other method.If you don't understand my response, don't ignore it, ask a question.
 06022012, 01:45 AM #11Member
 Join Date
 Feb 2011
 Posts
 33
 Rep Power
 0
 06022012, 02:50 AM #12
Re: Having a problem with to String
Was that the wrong answer?
You'll have to post the code so we can see what the problem is.If you don't understand my response, don't ignore it, ask a question.
 06022012, 03:07 AM #13Member
 Join Date
 Feb 2011
 Posts
 33
 Rep Power
 0
Re: Having a problem with to String
Main Class
Java Code:public class Rational { private int denominator; private int numerator; public Rational(int newDenominator, int newNumerator) //Set up the constructor { setDenominator(newDenominator); setNumerator(newNumerator); } public int getDenominator() // Set up the accessor { return denominator; } public int getNumerator() //Set up the accessor { return numerator; } public void setDenominator(int newDenominator) // Set up the moderator { if (newDenominator >=1) denominator = newDenominator; else if (newDenominator == 0 ) denominator = 1; } public void setNumerator(int newNumerator) //Set up the moderator { numerator = newNumerator; } public Rational multiply (Rational r) { int tempnumer; int tempdenom; tempnumer = numerator * r.numerator; tempdenom = denominator * r.denominator; return new Rational(tempnumer, tempdenom); } public Rational add (Rational r) { int tempa; int tempb; int tempc; int tempd; tempa = denominator * r.denominator; tempb = r.denominator * numerator; tempc = denominator * r.numerator; tempd = tempb + tempc; return new Rational(tempd,tempa); } public String toString() { return "2/3 * 5/7 = " + tempnumer + "/" + tempdenom + "/n" + "2/3 + 5/7 = " + tempd + "/" + tempa; } Client public class RationalClient { public static void main(String [] args) { // Setting up an object to pull from Rational and display the integers on the print line Rational integer = new Rational(3, 2); System.out.println("The numerator of rational # 1 is " + integer.getNumerator()); System.out.println("The denominator of rational # 1 is " + integer.getDenominator()); Rational fraction = new Rational(7, 5); System.out.println("Rational # 2 is " + fraction.getNumerator() + "/" + fraction.getDenominator()); // Suppose to display the addition and multiplication of the fractions System.out.println(integer.toString()); //????? end of code???
Last edited by Norm; 06022012 at 03:14 AM. Reason: added code tags
 06022012, 03:13 AM #14
Re: Having a problem with to String
when I ran the program, it equaled 0/0.
Does the code you just posted compile? It looks like it has the same problem as your first post. Where is the code where you have tried to put the variables in the main class?Last edited by Norm; 06022012 at 03:18 AM.
If you don't understand my response, don't ignore it, ask a question.
 06022012, 04:38 AM #15Member
 Join Date
 Feb 2011
 Posts
 33
 Rep Power
 0
Re: Having a problem with to String
This is how I had it when I put it into the main class :
public class Rational
{
private int denominator;
private int numerator;
int tempnumer;
int tempdenom;
int tempa;
int tempb;
int tempc;
int tempd;
public Rational(int newDenominator, int newNumerator) //Set up the constructor
{
setDenominator(newDenominator);
setNumerator(newNumerator);
}
public int getDenominator() // Set up the accessor
{
return denominator;
}
public int getNumerator() //Set up the accessor
{
return numerator;
}
public void setDenominator(int newDenominator) // Set up the moderator
{ if (newDenominator >=1)
denominator = newDenominator;
else if (newDenominator == 0 )
denominator = 1;
}
public void setNumerator(int newNumerator) //Set up the moderator
{
numerator = newNumerator;
}
public Rational multiply (Rational r)
{
tempnumer = numerator * r.numerator;
tempdenom = denominator * r.denominator;
return new Rational(tempnumer, tempdenom);
}
public Rational add (Rational r)
{
tempa = denominator * r.denominator;
tempb = r.denominator * numerator;
tempc = denominator * r.numerator;
tempd = tempb + tempc;
return new Rational(tempd,tempa);
}
public String toString()
{
return "2/3 * 5/7 = " + tempnumer + "/" + tempdenom + "/n" +
"2/3 + 5/7 = " + tempd + "/" + tempa;
}
When I compile this code it give me the 0/0
 06022012, 04:45 AM #16
Re: Having a problem with to String
Its better if the code compiles. If it does not compile, then it will never work.
Try debugging the code by adding println statements to show the values of variables as they are changed and used. The print out can show you where the problem is. Add lots of printlns to show the values of all the variables.If you don't understand my response, don't ignore it, ask a question.
 06022012, 05:28 AM #17Member
 Join Date
 Feb 2011
 Posts
 33
 Rep Power
 0
 06022012, 06:48 AM #18
Re: Having a problem with to String
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
 06022012, 07:46 AM #19Member
 Join Date
 Feb 2011
 Posts
 33
 Rep Power
 0
 06022012, 07:58 AM #20
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,315
 Blog Entries
 7
 Rep Power
 25
Re: Having a problem with to String
Yes you did ;) may I suggest that you keep your Rationals immutable (i.e. you shouldn't be able to change the numerator and/or denominator); also simplify the numerator and denominator in the constructor of that Class; you have to do that only once. The operators (add, mul etc.) return a new Rational object. The only clever stuff should happen in your toString() method, i.e. 3/1 should be printed as 3 and 0/3 should be printed as 0.
kind regards,
JosLast edited by JosAH; 06022012 at 08:59 AM. Reason: silly typo ...
The only person who got everything done by Friday was Robinson Crusoe.
Similar Threads

Basic string problem, just need to extract two numbers out of string
By gonzoateafly in forum New To JavaReplies: 6Last Post: 12062010, 10:26 AM 
String Display problem
By 123456 in forum New To JavaReplies: 4Last Post: 06152010, 01:15 PM 
Problem Showing A Big String?
By AJArmstron@aol.com in forum New To JavaReplies: 1Last Post: 05052010, 02:56 AM 
string to negative int problem
By pendulum101 in forum New To JavaReplies: 5Last Post: 11012009, 06:47 PM 
String Problem
By gnomewise in forum New To JavaReplies: 1Last Post: 10192008, 12:37 PM
Bookmarks