Results 1 to 5 of 5
  1. #1
    7auz is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default 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,

    Java 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);
    	}
    	}

  2. #2
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    5

    Default

    Can you post the code where use the constructor?
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  3. #3
    7auz is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    Java 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());
    		
    	}
    }
    Last edited by 7auz; 12-31-2010 at 07:42 PM.

  4. #4
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    5

    Default

    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.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  5. #5
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    5

    Default

    you can use a while loop and a try catch in your main
    Java 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());
        }

Similar Threads

  1. Adding Arrays and Enhanced For Loop into program.
    By vinyacam in forum New To Java
    Replies: 1
    Last Post: 05-10-2010, 07:28 AM
  2. Replies: 15
    Last Post: 11-06-2009, 11:31 AM
  3. Loop Program Help Please?
    By rjones215 in forum New To Java
    Replies: 2
    Last Post: 10-07-2009, 11:50 PM
  4. Trouble with For loop and variables in a program
    By dablyz in forum New To Java
    Replies: 12
    Last Post: 05-06-2008, 05:25 AM
  5. Replies: 2
    Last Post: 05-05-2008, 02:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •