How can I change string or int into a Rational?

76 views Asked by At

Here is the code,

         System.out.print("Enter r1: ");
         r1 =  in.next();
         System.out.print("Enter r2");
         r2 = in.next();
         System.out.println(myR.addR(r1,r2));

r1 and r2 is initialize as Rational. Here is my question, How can I change r1=in.next (which is in string form)into a rational form? Can I write r1 = in.Rational ....any tips plzzz

1

There are 1 answers

1
Alex K On

This is very easy to solve.

Instead of r1 = in.next();, use r1 = in.nextInt() or r1 = in.nextDouble(), depending on which kind of number you want.