Yet another fraction problem with File I/O

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1137 Views - Last Post: 17 March 2010 - 06:31 PM Rate Topic: -----

#1 spc21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 04-February 09

Yet another fraction problem with File I/O

Posted 06 March 2010 - 04:59 PM

Alright so this time, I have to write a program using file I/O to input into a driver program in order to perform operations on the fractions. They will be in the form of (1,2)(3,4) in the input file. I have been struggling with this for a little while now and I can't seem to figure it out. I've decided to try and use substring to fill an array of ints and do the operations that way. However, it isn't working correctly. Here is my code, can anyone tell me what I'm doing wrong?

here is the driver:
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.String.*;

public class testRational extends Rational{


	public static void main(String[] args){
		String input2 = "", input = "", temp = "";
		int num1 = 0, den1 = 0, den2 = 0, num2 = 0, counter = 0, input3 = 0;
		int[] array = new int[4];
    	String[] inArr = null;		
		while(input.compareTo("ZZ") != 0){
			input = JOptionPane.showInputDialog("What would you like to do?:\n\n\nA. Input from file\nB. Or input from Keyboard \n\n\n(ZZ. to exit)");
			
			input = input.toUpperCase();
			
			if(input.compareTo("A") == 0){
				input2 = JOptionPane.showInputDialog("Please enter the file you would like to use for input,\n including the directory and the .txt file extension:\n");    	
				try{
					BufferedReader in = new BufferedReader(new FileReader(input)); 
    				String inputLine = null; 

	    			while ((inputLine = in.readLine())!=null)
	    			{
	    				
	    				inArr = inputLine.split(",");
	    				for(int i = 0; i <inArr.length; i++){
	    					System.out.println(inArr[i]);
	    				}
//	    				StringTokenizer tok = new StringTokenizer(inputLine);
//	    				tok.nextToken();
//	    				temp = tok.nextToken();
//	    				num1 = Integer.parseInt(temp.trim());
//	    				tok.nextToken();
//	    				temp = tok.nextToken();
//	    				den1 = Integer.parseInt(temp.trim());
//	    				tok.nextToken();
//	    				tok.nextToken();
//	    				temp = tok.nextToken();
//	    				num2 = Integer.parseInt(temp.trim());
//	    				tok.nextToken();
//	    				temp = tok.nextToken();
//	   		 			den2 = Integer.parseInt(temp.trim());
//	   		 			tok.nextToken();
//	   		 			
//	   		 			array[0] = num1;
//	   		 			array[1] = den1;
//	   		 			array[2] = num2;
//	   		 			array[3] = den2;
	
					}
				   }
			   catch(IOException e){
			   }
			   
				input = JOptionPane.showInputDialog("What would you like to do?:\n\n\nA. Input from file\nB. Or input from Keyboard \nC. Add 2 Rationals (From Keyboard input)\nD. Add 2 Rationals at a time from the array(From File input)\nE. Sub 2 Rationals (From Keyboard input) \nF. Sub 2 Rationals at a time from the array(From File input) \nG. Mul 2 Rationals (From Keyboard input) \nH. Mul 2 Rationals at a time from the array(From File input)\nI. Div 2 Rationals (From Keyboard input)\nJ. Div 2 Rationals at a time from the array(From File input)\n\n(ZZ. to exit)");
			
				input = input.toUpperCase();		   
				
				if((input.compareTo("D"))==0){
					
				}
				if((input.compareTo("F"))==0){
					
				}
				if((input.compareTo("H"))==0){
					
				}
			}
			 if(input.compareTo("B") == 0){
				for(int i = 0; i < array.length; i++){	
					input3 = Integer.parseInt(JOptionPane.showInputDialog("Please enter a(n) fraction on integer at a time" ));
					array[i] = input3;					
				}
					input = JOptionPane.showInputDialog("What would you like to do?:\n\n\n C. Add 2 Rationals (From Keyboard input)\nE. Sub 2 Rationals (From Keyboard input) \nG. Mul 2 Rationals (From Keyboard input) \nI. Div 2 Rationals (From Keyboard input)\n\n(ZZ. to exit)");
			
					input = input.toUpperCase();
					
					if((input.compareTo("C"))==0){
						add(array[0], array[1], array[2], array[3]);
					}
					if((input.compareTo("E"))==0){
						subtract(array[0], array[1], array[2], array[3]);
					}
					if((input.compareTo("G"))==0){
						multiply(array[0], array[1], array[2], array[3]);
					}
					if((input.compareTo("I"))==0){
						divide(array[0], array[1], array[2], array[3]);
					}
			}		
		}							 
	}
}



here is the skeleton program:

//********************************************************************
//  Rational.java     
//
//  Represents one rational number with a numerator and denominator.
//********************************************************************

public class Rational
{
   private static int numerator, denominator, temp;
   
   public Rational()
   {
   		numerator = 0;
   		denominator = 0;
   }

   //-----------------------------------------------------------------
   //  Sets up the rational number by ensuring a nonzero denominator
   //  and making only the numerator signed.
   //-----------------------------------------------------------------
   public Rational(int newNum, int newDen)
   {
     numerator = newNum;
     denominator = newDen;
   }

   //-----------------------------------------------------------------
   //  Returns the numerator of this rational number.
   //-----------------------------------------------------------------
   public int getNumerator ()
   {
   		return numerator;     
   }

   //-----------------------------------------------------------------
   //  Returns the denominator of this rational number.
   //-----------------------------------------------------------------
   public int getDenominator ()
   {
   		return denominator;
   }

   //-----------------------------------------------------------------
   //  Returns the reciprocal of this rational number.
   //-----------------------------------------------------------------
   public Rational reciprocal (int n, int d)
   {
		temp = n;
		n = d;
		d = temp;
		
		return reciprocal(n, d);     
   }

   //-----------------------------------------------------------------
   //  Adds this rational number to the one passed as a parameter.
   //  A common denominator is found by multiplying the individual
   //  denominators.
   //-----------------------------------------------------------------
   public Rational add(Rational op2)
   {
     int num2;
     int den2;
     int currentNum;
     int currentDen;
     
     num2=op2.getNumerator();
     den2=op2.getDenominator();
     currentNum = getNumerator();
     currentDen = getDenominator();
     
		currentNum *= den2;
		currentDen *= den2;
		num2 *= denominator;
		
		currentNum += num2;
		
		Rational ration = new Rational(currentNum, currentDen);
		
		return ration;
     
   }

 //-----------------------------------------------------------------
   //  Adds this rational number to the integer passed as a parameter.
   //-----------------------------------------------------------------
   public  Rational       add(int a)
        {

	 int num2;
     int den2;
     int currentNum;
     int currentDen;
     
     num2=a;
     den2=1;
     currentNum = getNumerator();
     currentDen = getDenominator();
     
		currentNum *= den2;
		currentDen *= den2;
		num2 *= denominator;
		
		currentNum += num2;
		
		Rational ration = new Rational(currentNum, currentDen);
		
		return ration;

        }

   //-----------------------------------------------------------------
   //  Subtracts the rational number passed as a parameter from this
   //  rational number.
   //-----------------------------------------------------------------
   public Rational subtract (Rational op2)
   {
     int num2;
     int den2;
     int currentNum;
     int currentDen;
     
     num2=op2.getNumerator();
     den2=op2.getDenominator();
     currentNum = getNumerator();
     currentDen = getDenominator();
     
		currentNum *= den2;
		currentDen *= den2;
		num2 *= denominator;
		
		currentNum -= num2;
		
		Rational ration = new Rational(currentNum, currentDen);
		
		return ration;     
   }

   //-----------------------------------------------------------------
   //  Multiplies this rational number by the one passed as a
   //  parameter.
   //-----------------------------------------------------------------
   public Rational multiply (Rational op2)
   {
     int num2;
     int den2;
     int currentNum;
     int currentDen;
     
     num2=op2.getNumerator();
     den2=op2.getDenominator();
     currentNum = getNumerator();
     currentDen = getDenominator();
     
		currentNum *= num2;
		currentDen *= den2;
		
		Rational ration = new Rational(currentNum, currentDen);
		
		return ration;
   }

   //-----------------------------------------------------------------
   //  Divides this rational number by the one passed as a parameter
   //  by multiplying by the reciprocal of the second rational.
   //-----------------------------------------------------------------
   public Rational divide (Rational op2)
   {
     int num2;
     int den2;
     int currentNum;
     int currentDen;
     
     num2=op2.getNumerator();
     den2=op2.getDenominator();
     currentNum = getDenominator();
     currentDen = getNumerator();
     
		currentNum *= num2;
		currentDen *= den2;
		
		Rational ration = new Rational(currentNum, currentDen);
		
		return ration;   	
   }

   //-----------------------------------------------------------------
   //  Determines if this rational number is equal to the one passed
   //  as a parameter.  Assumes they are both reduced.
   //-----------------------------------------------------------------
   public boolean equals (Rational op2)
   {
   	
     int num2;
     int den2;
     int currentNum;
     int currentDen;
     
     num2=op2.getNumerator();
     den2=op2.getDenominator();
     currentNum = getDenominator();
     currentDen = getNumerator();
     
     if(currentNum == num2 && currentDen == den2){
     	return true;
     }
	 else{
	 	return false;
	 }
   }

   //-----------------------------------------------------------------
   //  Returns this rational number as a string.
   //-----------------------------------------------------------------
/*   public String toString ()
   {
      return;
   }
*/
   //-----------------------------------------------------------------
   //  Reduces this rational number by dividing both the numerator
   //  and the denominator by their greatest common divisor.
   //-----------------------------------------------------------------
   private void reduce ()
   {
		for(int count = denominator; count > 0; count--)
		{
			if (numerator % count == 0 && denominator % count == 0)
			{
				numerator /= count;
				denominator /= count;
			}
		}     
   }

   //-----------------------------------------------------------------
   //  Computes and returns the greatest common divisor of the two
   //  positive parameters. Uses Euclid's algorithm.
   //-----------------------------------------------------------------
   private int gcd (int num1, int num2)
   {
      while (num1 != num2)
         if (num1 > num2)
            num1 = num1 - num2;
         else
            num2 = num2 - num1;

      return num1;
   }


   //-----------------------------------------------------------------
   //  Displays the Rational Number
   //-----------------------------------------------------------------
   
   public  void    show()
        {
                System.out.println("numerator = " + this.numerator +
                         " denominator = " + this.denominator);
        }

    //-----------------------------------------------------------------
   //  Displays the Rational Number in real form also
   //-----------------------------------------------------------------
   
   
   public  void    display()
        {
                
         }       
   
	public static int add(int num, int den, int num2, int den2){
		System.out.println("("+num +"/" + den+")" + "+" + "("+ num2 + "/" + den2 +")");
		if(den == den2){
			num+=num2;
		}
		if(den != den2){
			num *=den2;
			den *= den2;
			num2 *= den;
		
			num += num2;
		}
		
		reduce(num, den);
		return 0;
	}
	public static int subtract(int num, int den, int num2, int den2){
		System.out.println("("+num +"/" + den+")" + "-" + "("+ num2 + "/" + den2 +")");
		if(den == den2){
			num-=num2;
		}
		if(den != den2){
			num *=den2;
			den *= den2;
			num2 *= den;
		
			num -= num2;
		}
		
		reduce(num, den);
		return 0;
	}
	public static int multiply(int num, int den, int num2, int den2){
		System.out.println("("+num +"/" + den+")" + "*" + "("+ num2 + "/" + den2 +")");
		
			num *=num2;
			den *= den2;
		
		reduce(num, den);
		return 0;
	}
	public static int divide(int num, int den, int num2, int den2){
		System.out.println("("+num +"/" + den+")" + "/" + "("+ num2 + "/" + den2 +")");
			temp = num2;
			num2 = den2;
			den2 =temp;
		
		

			num *= num2;		
			den *= den2;
		reduce(num, den);
		return 0;
	}	
	public static void reduce(int num, int den){
		for(int count = den; count > 0; count--)
		{
			if (num % count == 0 && den % count == 0)
			{
				num /= count;
				den /= count;
		
			}			
		}
		if(den == 1){
			System.out.println(num);
		}
		else if(den !=1){
			System.out.println(num +"/" + den);	
		}
	}
}   
   
   
   
   
   
   
   
   
   



Is This A Good Question/Topic? 0
  • +

Replies To: Yet another fraction problem with File I/O

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Yet another fraction problem with File I/O

Posted 06 March 2010 - 08:02 PM

What are the errors?
Was This Post Helpful? 0
  • +
  • -

#3 spc21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 04-February 09

Re: Yet another fraction problem with File I/O

Posted 07 March 2010 - 05:00 PM

Well it's not so much errors as it is I'm not sure if it's doing what it's supposed to. I want to take the numerical characters from the file (i.e (1,2)(3,4)) and parse them to ints and store them into an array only four "fractions" at a time. I imagine I'd probably have to use the .mark() (.marked()?) method so if they wanted to read more from the file that they'd be able to start from where they left off reading another four "fractions" in. Also I'm not so sure that it's using the menu options within the (file input menu) menu correctly. Due to this, it is also not displaying the information that is stored in the array (if it's actually storing it/doing what it's supposed to).
Was This Post Helpful? 0
  • +
  • -

#4 spc21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 04-February 09

Re: Yet another fraction problem with File I/O

Posted 07 March 2010 - 06:01 PM

Okay so I tweaked my code a little bit, but it's still not seeming to print or load the array. At least I don't think so. Am I doing this correctly to fill the array/are the methods for the FILE I/O working?


New Driver code:

import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.String.*;

public class testRational extends Rational{


	public static void main(String[] args){
		String input2 = "", input = "", temp = "";
		int num1 = 0, den1 = 0, den2 = 0, num2 = 0, counter = 0, input3 = 0, first=0, second = 0, last = 0;
		int[] array = new int[4];
    	int[] inArr = null;
	    Rational test1 = null;
	  	Rational test2 = null;
	    Rational result = null;	 		

		while(input.compareTo("ZZ") != 0){
			input = JOptionPane.showInputDialog("What would you like to do?:\n\n\nA. Input from file\nB. Or input from Keyboard \n\n\n(ZZ. to exit)");
			
			input = input.toUpperCase();
			
			// if A; take input from a file
			if(input.compareTo("A") == 0){
				input2 = JOptionPane.showInputDialog("Please enter the file you would like to use for input,\n including the directory and the .txt file extension:\n");    	
				try{
					BufferedReader in = new BufferedReader(new FileReader(input)); 
    				String inputLine = null; 

	    			while ((inputLine = in.readLine())!=null)
	    			{
	    				first = inputLine.indexOf("(");
	    				second = inputLine.indexOf(",");
	    				last = inputLine.indexOf(")");
	    				
	    				num1 = Integer.parseInt(inputLine.substring(first, second));
	    				den1 = Integer.parseInt(inputLine.substring(second, last));
	    				num2 = Integer.parseInt(inputLine.substring(first, second));
	    				den2 = Integer.parseInt(inputLine.substring(second, last));
						inArr[0] = num1;
						inArr[1] = den1;
						inArr[2] = num2;
						inArr[3] = den2;
	    				test1 = new Rational(inArr[0], inArr[1]);
	    				test2 = new Rational(inArr[2], inArr[3]);
	    				result = new Rational();
	    				result.reduce();	    				

					}

						input = JOptionPane.showInputDialog("What would you like to do?:\n\n\nD. Add 2 Rationals at a time from the array(From File input)\nF. Sub 2 Rationals at a time from the array(From File input)\nH. Mul 2 Rationals at a time from the array(From File input)\nJ. Div 2 Rationals at a time from the array(From File input)\n\n(ZZ. to exit)");
			
						input = input.toUpperCase();		   
					
						if((input.compareTo("D"))==0){
							result = test1.add(test2);
							result.reduce();
							result.show();
						}
						if((input.compareTo("F"))==0){
							result = test1.subtract(test2);
							result.reduce();
							result.show();
						}
						if((input.compareTo("H"))==0){
							result = test1.multiply(test2);
							result.reduce();
							result.show();
						}
						if((input.compareTo("J"))==0){
							result = test1.divide(test2);
							result.reduce();
							result.show();
						}
									

				   }
			   catch(IOException e){
			   }
			}
			
			 if(input.compareTo("B") == 0){
				for(int i = 0; i < array.length; i++){	
					input3 = Integer.parseInt(JOptionPane.showInputDialog("Please enter a(n) fraction on integer at a time" ));
					array[i] = input3;					
				}
					input = JOptionPane.showInputDialog("What would you like to do?:\n\n\n C. Add 2 Rationals (From Keyboard input)\nE. Sub 2 Rationals (From Keyboard input) \nG. Mul 2 Rationals (From Keyboard input) \nI. Div 2 Rationals (From Keyboard input)\n\n(ZZ. to exit)");
			
					input = input.toUpperCase();
					
					if((input.compareTo("C"))==0){
						add(array[0], array[1], array[2], array[3]);
					}
					if((input.compareTo("E"))==0){
						subtract(array[0], array[1], array[2], array[3]);
					}
					if((input.compareTo("G"))==0){
						multiply(array[0], array[1], array[2], array[3]);
					}
					if((input.compareTo("I"))==0){
						divide(array[0], array[1], array[2], array[3]);
					}
			}		
		}							 
	}
}



Here is the class/methods which the driver extends:

//********************************************************************
//  Rational.java     
//
//  Represents one rational number with a numerator and denominator.
//********************************************************************

public class Rational
{
   private static int numerator, denominator, temp;
   
   public Rational()
   {
   		numerator = 0;
   		denominator = 0;
   }

   //-----------------------------------------------------------------
   //  Sets up the rational number by ensuring a nonzero denominator
   //  and making only the numerator signed.
   //-----------------------------------------------------------------
   public Rational(int newNum, int newDen)
   {
     numerator = newNum;
     denominator = newDen;
   }

   //-----------------------------------------------------------------
   //  Returns the numerator of this rational number.
   //-----------------------------------------------------------------
   public int getNumerator ()
   {
   		return numerator;     
   }

   //-----------------------------------------------------------------
   //  Returns the denominator of this rational number.
   //-----------------------------------------------------------------
   public int getDenominator ()
   {
   		return denominator;
   }

   //-----------------------------------------------------------------
   //  Returns the reciprocal of this rational number.
   //-----------------------------------------------------------------
   public Rational reciprocal (int n, int d)
   {
		temp = n;
		n = d;
		d = temp;
		
		return reciprocal(n, d);     
   }

   //-----------------------------------------------------------------
   //  Adds this rational number to the one passed as a parameter.
   //  A common denominator is found by multiplying the individual
   //  denominators.
   //-----------------------------------------------------------------
   public Rational add(Rational op2)
   {
     int num2;
     int den2;
     int currentNum;
     int currentDen;
     
     num2=op2.getNumerator();
     den2=op2.getDenominator();
     currentNum = getNumerator();
     currentDen = getDenominator();
     
		currentNum *= den2;
		currentDen *= den2;
		num2 *= denominator;
		
		currentNum += num2;
		
		Rational ration = new Rational(currentNum, currentDen);
		
		return ration;
     
   }

 //-----------------------------------------------------------------
   //  Adds this rational number to the integer passed as a parameter.
   //-----------------------------------------------------------------
   public  Rational       add(int a)
        {

	 int num2;
     int den2;
     int currentNum;
     int currentDen;
     
     num2=a;
     den2=1;
     currentNum = getNumerator();
     currentDen = getDenominator();
     
		currentNum *= den2;
		currentDen *= den2;
		num2 *= denominator;
		
		currentNum += num2;
		
		Rational ration = new Rational(currentNum, currentDen);
		
		return ration;

        }

   //-----------------------------------------------------------------
   //  Subtracts the rational number passed as a parameter from this
   //  rational number.
   //-----------------------------------------------------------------
   public Rational subtract (Rational op2)
   {
     int num2;
     int den2;
     int currentNum;
     int currentDen;
     
     num2=op2.getNumerator();
     den2=op2.getDenominator();
     currentNum = getNumerator();
     currentDen = getDenominator();
     
		currentNum *= den2;
		currentDen *= den2;
		num2 *= denominator;
		
		currentNum -= num2;
		
		Rational ration = new Rational(currentNum, currentDen);
		
		return ration;     
   }

   //-----------------------------------------------------------------
   //  Multiplies this rational number by the one passed as a
   //  parameter.
   //-----------------------------------------------------------------
   public Rational multiply (Rational op2)
   {
     int num2;
     int den2;
     int currentNum;
     int currentDen;
     
     num2=op2.getNumerator();
     den2=op2.getDenominator();
     currentNum = getNumerator();
     currentDen = getDenominator();
     
		currentNum *= num2;
		currentDen *= den2;
		
		Rational ration = new Rational(currentNum, currentDen);
		
		return ration;
   }

   //-----------------------------------------------------------------
   //  Divides this rational number by the one passed as a parameter
   //  by multiplying by the reciprocal of the second rational.
   //-----------------------------------------------------------------
   public Rational divide (Rational op2)
   {
     int num2;
     int den2;
     int currentNum;
     int currentDen;
     
     num2=op2.getNumerator();
     den2=op2.getDenominator();
     currentNum = getDenominator();
     currentDen = getNumerator();
     
		currentNum *= num2;
		currentDen *= den2;
		
		Rational ration = new Rational(currentNum, currentDen);
		
		return ration;   	
   }

   //-----------------------------------------------------------------
   //  Determines if this rational number is equal to the one passed
   //  as a parameter.  Assumes they are both reduced.
   //-----------------------------------------------------------------
   public boolean equals (Rational op2)
   {
   	
     int num2;
     int den2;
     int currentNum;
     int currentDen;
     
     num2=op2.getNumerator();
     den2=op2.getDenominator();
     currentNum = getDenominator();
     currentDen = getNumerator();
     
     if(currentNum == num2 && currentDen == den2){
     	return true;
     }
	 else{
	 	return false;
	 }
   }

   //-----------------------------------------------------------------
   //  Returns this rational number as a string.
   //-----------------------------------------------------------------
/*   public String toString ()
   {
      return;
   }
*/
   //-----------------------------------------------------------------
   //  Reduces this rational number by dividing both the numerator
   //  and the denominator by their greatest common divisor.
   //-----------------------------------------------------------------
   public void reduce()
   {
		for(int count = denominator; count > 0; count--)
		{
			if (numerator % count == 0 && denominator % count == 0)
			{
				numerator /= count;
				denominator /= count;
			}
		}
		
		System.out.println(numerator + "/" + denominator);     
   }

   //-----------------------------------------------------------------
   //  Computes and returns the greatest common divisor of the two
   //  positive parameters. Uses Euclid's algorithm.
   //-----------------------------------------------------------------
   private int gcd (int num1, int num2)
   {
      while (num1 != num2)
         if (num1 > num2)
            num1 = num1 - num2;
         else
            num2 = num2 - num1;

      return num1;
   }


   //-----------------------------------------------------------------
   //  Displays the Rational Number
   //-----------------------------------------------------------------
   
   public  void    show()
        {
                System.out.println("numerator = " + this.numerator +
                         " denominator = " + this.denominator);
        }

    //-----------------------------------------------------------------
   //  Displays the Rational Number in real form also
   //-----------------------------------------------------------------
   
   
   public  void    display()
        {
                
         }       
   
	public static int add(int num, int den, int num2, int den2){
		System.out.println("("+num +"/" + den+")" + "+" + "("+ num2 + "/" + den2 +")");
		if(den == den2){
			num+=num2;
		}
		if(den != den2){
			num *=den2;
			den *= den2;
			num2 *= den;
		
			num += num2;
		}
		
		reduce(num, den);
		return 0;
	}
	public static int subtract(int num, int den, int num2, int den2){
		System.out.println("("+num +"/" + den+")" + "-" + "("+ num2 + "/" + den2 +")");
		if(den == den2){
			num-=num2;
		}
		if(den != den2){
			num *=den2;
			den *= den2;
			num2 *= den;
		
			num -= num2;
		}
		
		reduce(num, den);
		return 0;
	}
	public static int multiply(int num, int den, int num2, int den2){
		System.out.println("("+num +"/" + den+")" + "*" + "("+ num2 + "/" + den2 +")");
		
			num *=num2;
			den *= den2;
		
		reduce(num, den);
		return 0;
	}
	public static int divide(int num, int den, int num2, int den2){
		System.out.println("("+num +"/" + den+")" + "/" + "("+ num2 + "/" + den2 +")");
			temp = num2;
			num2 = den2;
			den2 =temp;
		
		

			num *= num2;		
			den *= den2;
		reduce(num, den);
		return 0;
	}	
	public static void reduce(int num, int den){
		for(int count = den; count > 0; count--)
		{
			if (num % count == 0 && den % count == 0)
			{
				num /= count;
				den /= count;
		
			}			
		}
		if(den == 1){
			System.out.println(num);
		}
		else if(den !=1){
			System.out.println(num +"/" + den);	
		}
	}
}   


Was This Post Helpful? 0
  • +
  • -

#5 spc21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 04-February 09

Re: Yet another fraction problem with File I/O

Posted 08 March 2010 - 10:51 PM

No one?
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Yet another fraction problem with File I/O

Posted 09 March 2010 - 04:16 AM

I noticed a messup here:
if(input.compareTo("A") == 0){
    input2 = JOptionPane.showInputDialog("Please enter the file you would like to use for input,\n including the directory and the .txt file extension:\n");        
    try{
        // I changed it from input to input2
        BufferedReader in = new BufferedReader(new FileReader(input2)); 
        String inputLine = null; 



Your filename is input2, not input...
Secondly, you might be able to make the if (input.compareTo("A")) part better by using the string method, equalsIgnoreCase();

 
if (input.equalsIgnoreCase("A")) {
   ...



You could also use compareToIgnoreCase() if you really like the integers...
Was This Post Helpful? 0
  • +
  • -

#7 spc21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 04-February 09

Re: Yet another fraction problem with File I/O

Posted 11 March 2010 - 10:14 PM

Alright, so I CANNOT believe I missed that input2 thing! :nottalkingtoyou: I feel so stupid it's not even funny. Okay, so now that that's fixed here's the errors I'm getting from reading the file in.

Exception in thread "main" java.lang.NumberFormatException: For input string: "(4"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
        at java.lang.Integer.parseInt(Integer.java:447)
        at java.lang.Integer.parseInt(Integer.java:497)
        at testRational.main(testRational.java:39)



All of the other errors are the same just different line numbers. Is there a better way I could be interpreting the input from the file?
Was This Post Helpful? 0
  • +
  • -

#8 spc21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 04-February 09

Re: Yet another fraction problem with File I/O

Posted 13 March 2010 - 12:52 AM

Am I going about the interpretation correctly? Can anyone tell me at least that much?
Was This Post Helpful? 0
  • +
  • -

#9 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Yet another fraction problem with File I/O

Posted 13 March 2010 - 03:51 AM

Your trying to parse a String to an int but its given you this NumberFormatException because you have got a bracket in there..

For input string: "(4"
Was This Post Helpful? 1
  • +
  • -

#10 spc21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 04-February 09

Re: Yet another fraction problem with File I/O

Posted 13 March 2010 - 04:37 PM

Alright, but what should I do to avoid that from happening? I tried to use tokenizer and it didn't work either.
Was This Post Helpful? 0
  • +
  • -

#11 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Yet another fraction problem with File I/O

Posted 13 March 2010 - 04:39 PM

Use the substring() method to trim the String so the bracket isnt there, then try and parse it to an int.
Was This Post Helpful? 1
  • +
  • -

#12 spc21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 04-February 09

Re: Yet another fraction problem with File I/O

Posted 13 March 2010 - 05:08 PM

That's what I'm doing, I have it as
first = inputLine.indexOf("(");
second = inputLine.indexOf(",");
last = inputLine.indexOf(")");
   				
num1 = Integer.parseInt(inputLine.substring(first, second));
den1 = Integer.parseInt(inputLine.substring(second, last));


and that's the error I'm getting.
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10477
  • View blog
  • Posts: 38,835
  • Joined: 27-December 08

Re: Yet another fraction problem with File I/O

Posted 13 March 2010 - 05:44 PM

The reason you are getting an error is b/c you start your second substring at the index of the comma den1 = Integer.parseInt(inputLine.substring(second, last));. Remember that substring() returns a String from the first index inclusive to the second index exclusive. So if you have String x = "1234,567";, and you have invoke x.substring(x.indexOf(","), x.length());, the resulting String would be ",567". To completely avoid the comma in your situation, you need the starting index to be second+1.
Was This Post Helpful? 0
  • +
  • -

#14 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Yet another fraction problem with File I/O

Posted 13 March 2010 - 05:49 PM

You are grabbing the substring from the index of the parenthesis to the end...not the character after. Here is what the substring method does.
substring(start_index(inclusive), end_index(exclusive)

The inclusive means that it includes the current digit, and exclusive means it goes UP to that point...Also, the indexOf() methods have two things that you could improve with...indexOf() can optionally take two parameters...
indexOf(what_to_search_for, starting_location) And also note that it returns -1 if one is not found...

Here's how to improve:
first = inputLine.indexOf("(");
// Find the , AFTER first
second = inputLine.indexOf(",". first);

// Find the ) AFTER second
last = inputLine.indexOf(")", second);

// Check to make sure none are -1
if (first == -1 || second == -1 || last == -1)
    // Quit
    System.exit(1);

try {
    num1 = Integer.parseInt(inputLine.substring(first+1, second));
    den1 = Integer.parseInt(inputLine.substring(second+1, last));
} catch (NumberFormatException e) {
    System.out.println("Even with all our efforts, something went wrong");
}



:)
Was This Post Helpful? 0
  • +
  • -

#15 spc21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 04-February 09

Re: Yet another fraction problem with File I/O

Posted 14 March 2010 - 12:05 AM

Okay so I'm hoping that this will be my last and final problem with this. Now I'm trying to read in 4 "fractions" from the file so that means four variables num1, den1, num2, and finally den2. How do I go about parsing these values out correctly? Should I use a .mark()command and substring it from that point on? I'm kind of new to using these methods because none of my professors ever went over this stuff. Here is what my most up to date code looks like.

import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.String.*;

public class testRational extends Rational{


	public static void main(String[] args){
		String input2 = "", input = "", temp = "";
		int num1 = 0, den1 = 0, den2 = 0, num2 = 0, counter = 0, input3 = 0, first=0, second = 0, last = 0;
		int[] array = new int[4];
    	int[] inArr = new int[4];
	    Rational test1 = null;
	  	Rational test2 = null;
	    Rational result = null;	 		

		while(input.compareTo("ZZ") != 0){
			input = JOptionPane.showInputDialog("What would you like to do?:\n\n\nA. Input from file\nB. Or input from Keyboard \n\n\n(ZZ. to exit)");
			
			input = input.toUpperCase();
			
			// if A; take input from a file
			if(input.compareTo("A") == 0){
				input2 = JOptionPane.showInputDialog("Please enter the file you would like to use for input,\n including the directory and the .txt file extension:\n");    	
				try{

					BufferedReader in = new BufferedReader(new FileReader(input2)); 
    				String inputLine = null; 

	    			while ((inputLine = in.readLine())!=null)
	    			{
	    				first = inputLine.indexOf("(");
	    				second = inputLine.indexOf(",", first);
	    				last = inputLine.indexOf(")", second);
	    				
	    				if(first == -1 || second == -1 || last == -1){
	    					System.exit(1);
	    				}
						try {
							    num1 = Integer.parseInt(inputLine.substring(first+1, second));
							    den1 = Integer.parseInt(inputLine.substring(second+1, last));
			    				num2 = Integer.parseInt(inputLine.substring(first, second));
			    				den2 = Integer.parseInt(inputLine.substring(second, last));
						inArr[0] = num1;
						inArr[1] = den1;
						inArr[2] = num2;
						inArr[3] = den2;
						System.out.println(inArr[0]);
						System.out.println(inArr[1]);
						System.out.println(inArr[2]);
						System.out.println(inArr[3]);
						System.out.println(inArr[4]);
//						for(int i = 0; i <inArr.length; i++){
//						System.out.println(inArr[i]);
//						}
	    				test1 = new Rational(inArr[0], inArr[1]);
	    				test2 = new Rational(inArr[2], inArr[3]);
	    				result = new Rational();
						} catch (NumberFormatException e) {
							System.out.println("Even with all our efforts, something went wrong");
						}
					}



As you can see it's not reading the second set of numbers on the line into num2, and den2. As I said the only thing I can think of would be using marked().
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2