The Artima Developer Community
Sponsored Link

Java Answers Forum
Reading from Text file

3 replies on 1 page. Most recent reply: Apr 21, 2002 11:19 AM by Jay Kandy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Reading from Text file Posted: Apr 19, 2002 9:14 PM
Reply to this message Reply
Advertisement
Trying to get greatest common divisor from numbers listed in a text file (I love Lucy in there to create exception). Driver won't pick up answer. This may be a total mess because I just started working on it. All help greatly appreciated.

import DivisorCalc;
import java.util.StringTokenizer;
import java.io.*;
public class GcdDriver {
	
 
  public static void main(String args[]) throws IOException {
		
		
		StringTokenizer tokenizer;
		String line, file = "numbers.txt";
		int num1, num2, answer;
 
		
	try{
		
		FileReader fr = new FileReader (file);
		BufferedReader inFile = new BufferedReader (fr);
		line = inFile.readLine();
		
		while (line != null){
		
		tokenizer = new StringTokenizer (line);
	
		
		try
		 {
		 	
		 num1 = Integer.parseInt (tokenizer.nextToken());
		 num2 = Integer.parseInt (tokenizer.nextToken());
		 		 
		 }
		 
		 catch (NumberFormatException exception)
		 {
		 	System.out.println ("Error in input. Line ignored:");
		    System.out.println (line);		
		 }
		
		line = inFile.readLine();
	     }
	    
	    inFile.close();		    	   
	     }
		
	catch (FileNotFoundException exception)
	
	{
		System.out.println ("The file " + file + "was not found.");
	}	
    catch (IOException exception)
    {
    	System.out.println (exception);
    }
    
        DivisorCalc.Gcd (answer);
    
        System.out.println ("Greatest common divisor:" + answer);
}
}


Here is DivisorCalc:

import java.io.*;
 
public class DivisorCalc {
	
	private static int dividend1, dividend2, num1, num2;
	private static int max, answer, result;
	
	public static int Gcd (int num1, int num2) {
		
	      if(num1 > num2) // to figure larger of the two numbers
             max = num1;
           else          
             max = num2;
           
        
      while (dividend1 != dividend2) {
        
        for ( int i = 1; i < max; i++)
        
           dividend1 = num1 - max;
           dividend2 = num2 - max;
           
           max--;
     
                 
          if (dividend1 == dividend2)  
          
          answer = dividend1;
       }             
          return answer;
                   
	   	     
} //class Gcd
          
} // class DivisorCalc	


Here is text file:
45 15
100 35
4 7
I love Lucy


Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: Reading from Text file Posted: Apr 20, 2002 10:57 AM
Reply to this message Reply
Here are a few minor things I found:
1. There was something wrong with the algorithm used for finding gcd.
2. The class DivisorCalc has all its variables declared static which by default get initialized to 0 and thats why the whilie block in Gcd() is never run.
while (dividend1 != dividend2)
{ /*dividend1, and dividend2 are always 0*/

Heres something to remember- Make a variable static when you *really* want just one one copy of the variable inspite of the number of instances of class. Same goes for a static method/block.
3. In class GcdDriver the call to gcd() should have been in the while loop.

/**
 *		GcdDriver
 */
import DivisorCalc;
import java.util.StringTokenizer;
import java.io.*;
public class GcdDriver
{
	public static void main(String args[]) throws IOException
	{
		StringTokenizer tokenizer;
		String line, file = "numbers.txt";
		int num1 = 0, num2 = 0, answer = 0;
 
		try
		{
			FileReader fr = new FileReader (file);
			BufferedReader inFile = new BufferedReader (fr);
			line = inFile.readLine();
 
			while (line != null)
			{
				tokenizer = new StringTokenizer (line);
				try
				{
					num1 = Integer.parseInt(tokenizer.nextToken());
					num2 = Integer.parseInt(tokenizer.nextToken());
 
					answer = DivisorCalc.gcd(num1, num2);
					System.out.println ("Greatest common divisor:" + answer);
				}
				catch (NumberFormatException exception)
				{
					System.out.print("Error in input. Line ignored:");
					System.out.println (line);
				}
				line = inFile.readLine();
			}
 
			inFile.close();
		}
		catch (FileNotFoundException exception)
		{
			System.out.println ("The file " + file + "was not found.");
		}
		catch (IOException exception)
		{
			System.out.println (exception);
		}
	}
}
 
/**
 *	DivisorCalc
 */
public class DivisorCalc
{
	/**
	 *		What is happening here??
	 *
	private static int dividend1, dividend2, num1, num2;
	private static int max, answer, result;

	public static int Gcd (int num1, int num2)
	{
		if(num1 > num2) // to figure larger of the two numbers
		max = num1;
		else
		max = num2;

		while (dividend1 != dividend2)
		{
		for ( int i = 1; i < max; i++)

		dividend1 = num1 - max;
		dividend2 = num2 - max;

		max--;

		if (dividend1 == dividend2)
		answer = dividend1;
		}
		return answer;
	} //class Gcd
	*/
 
	/**
	 *		Completing problem 1 presented at
	 *		<url>http://www.math.ucla.edu/~ronmiech/Pic20/Sexampdf/Sex.pdf</url>
	 *		Recursion is another way.
	 */
	public static int gcd(int a, int b)
	{
		if(a == 0 || b == 0) return 0;
 
		int A = Math.abs(a);
		int B = Math.abs(b);
 
		int c = Math.max(A,B);
		int d = Math.min(A,B);
		int r = c%d;
 
 
		while( r != 0)
		{
			c = d;
			d = r;
 
			r = c % d;
		}
		return d;
	}
} // class DivisorCalc

numbers.txt
45 15
I Love Lucy
100 35
4 7

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Thank you !!! Posted: Apr 20, 2002 12:52 PM
Reply to this message Reply
Thanks so much Jay. It's perfect.

Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: Thank you !!! Posted: Apr 21, 2002 11:19 AM
Reply to this message Reply
Thought I'd make up for the confusion with Shapes question :)

Flat View: This topic has 3 replies on 1 page
Topic: jsp and Beans Previous Topic   Next Topic Topic: swing , when mouse moves over jlabel  not firing the event

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use