# How to loop this program??

• 12-30-2010, 11:27 PM
7auz
How to loop this program??
ok guys i need a little help here, i have made this program that inputs a fraction and give the output in its simplest forms, the only problem im having is that i nee to edit this coding to that if the user inserts a char, or 0 in the denominator, so the program should give an error message and return to the start until the user wants to exit the program...

here is my coding,

Code:

```public class Fraction {         private int numer = 0;         private int denom = 0;         public Fraction(int numer, int denom) {                 if (denom == 0)                 throw new NumberFormatException("denominator is zero");                 this.numer = numer;                 this.denom = denom;                 this.reduce();         }         public int getNumerator() {                 return this.numer;         }         public int getDenominator() {                 return this.denom;         }         public void reduce()         {                 int        d;                 //while (true)                 do{                         d = this.gcd(this.numer, this.denom);                         if (d == 1)                                 return;                         this.numer /= d;                         this.denom /= d;                 }while(true);         }         private int gcd(int a, int b) {                 int        t;                 while (b != 0) {                         t = a;                         a = b;                         b = t%a;                 }                 return (a);         }         public String output() {                 return ( this.numer + "/" + this.denom );         }                 public double doubleValue() {                 return (1.0*this.numer/this.denom);         }         }```
• 12-31-2010, 01:04 AM
Hibernate
Can you post the code where use the constructor?
• 12-31-2010, 07:39 PM
7auz
Code:

```                    public static void main(String[] args) {                 Scanner in=new Scanner(System.in);                                 Fraction        f1;                 int n,d;                                 System.out.print("Enter Numerator: ");                         n=in.nextInt();                 System.out.print("Enter Denominator: ");                         d=in.nextInt();                                         f1 = new Fraction(n, d);                 System.out.println("Fraction Output: " + f1.output());                         } }```
• 01-01-2011, 04:04 AM
Hibernate
Using in.nextInt(); is inadvisable, you should read the next line and parse it to an integer:
Integer.parseInt(in.nextLine());
This will throw an exception if the line in not an integer.
Catch that exception and restart that part of the program; use a for( ;; ) or while loop.

If an exception was not thrown, just check if the denominator is 0.
• 01-01-2011, 04:08 AM
JavaHater
you can use a while loop and a try catch in your main
Code:

```    public static void main(String[] args) {         Fraction    f1;         int n,d;         while(true){             try {                 Scanner in=new Scanner(System.in);                 System.out.print("Enter Numerator: ");                 n=in.nextInt();                 System.out.print("Enter Denominator: ");                 d=in.nextInt();                 break;             } catch ( InputMismatchException ex ){                 System.err.println("Enter a valid integer");             }         }         f1 = new Fraction(n, d);         System.out.println("Fraction Output: " + f1.output());     }```