5 Replies - 3585 Views - Last Post: 06 December 2008 - 11:02 AM Rate Topic: -----

#1 camckee316  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 277
  • Joined: 29-August 08

Text to array

Posted 01 December 2008 - 12:15 PM

I searched the forms for a way to read a file into an array. And not really comprehending. I need to sort the numbers in a file, display only the lowest and the highest. along with all the other code that I have which seems to be working.
import java.io.*;
import java.util.*;

public class NumberAnalysis
{
	public static void main(String[] args)throws Exception
	{
		Scanner s = new Scanner(new File("Numbers.txt"));
		System.out.println("Here are the numbers that are in the file:");
		while (s.hasNext())
			System.out.println(s.next());
		
		
		NumberTotal nt = new NumberTotal("Numbers.txt");
		System.out.println("The total of the numbers in the array are: " + nt.getTotal());
		
		Average ave = new Average("Numbers.txt");
		System.out.println("The average of the numbers in the array are: " + ave.getAverage());
		
		
		System.out.println("The lowest number is: ");
		System.out.println("The highest number is: ");
		
	}
}


gets the average

import java.util.Scanner;
import java.io.*;

public class Average
{
	private float average;
	
	public Average(String filename) throws IOException
	{
		String str;
		
		File file = new File(filename);
		Scanner inputFile = new Scanner(file);
		
	
		float total = 0;		
		while (inputFile.hasNext())
		{
			float number = inputFile.nextFloat();
			
			total = total + number;
			average = total /19;
		}
		
		inputFile.close();
	}
	
	public float getAverage()
	{
		return average;
	}
}
	


gets the total

import java.util.Scanner;
import java.io.*;

public class NumberTotal
{
	private double sum;
	
	public NumberTotal(String filename) throws IOException
	{
		String str;
		
		File file = new File(filename);
		Scanner inputFile = new Scanner(file);
		
		sum = 0.0;
		
		
		while (inputFile.hasNext())
		{	
			double number = inputFile.nextDouble();
			
			sum = sum + number;
			
		}
		
		inputFile.close();
	}
	
	public double getTotal()
	{
		return sum;
	}

}


any help knowledge would be helpful. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Text to array

#2 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Text to array

Posted 01 December 2008 - 12:32 PM

How is the numbers seperated in the file, space, tab, line, comma, etc?
Are the numbers only integers, doubles, etc?

You print out the numbers to the console, instead just store them into the list, when this is done sort the array and print out the first and last element.

Also in each class the values are read into from a file, you could do this in the main class and pass the array into the other classes as argument.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4189
  • View blog
  • Posts: 11,863
  • Joined: 18-April 07

Re: Text to array

Posted 01 December 2008 - 12:33 PM

Well ideally you want to read the file only once, store the values in the array and then pass the array to your average and NumberTotal functions... instead of opening file there too. This would increase performance and also reduce the amount of file handling code you have.

You can start by declaring an actual array for your NumberAnalysis class. So right above your main function you can put a line like private float[] numbers = new float[10];. After this line is added the class now knows about an array of float numbers. So as you read the file, you can place each value into a position in the array with numbers[0], numbers[1], numbers[2] etc. Notice we start at zero and can go up to numbers[9] which is our tenth element.

Once we fill this array up, we can simply pass it to our average and NumberTotal functions. So make sure each of those classes accept an array as a parameter. You can then get rid of any file reading and use the array itself to find the highest, lowest and average. If you need help with that part you can refer to my blog entry on finding the highest, lowest and average of an array which covers Java as well...

Martyr2's Programming Underground - Find High / Low Values in an Array (Definitive Series)

You will keep reading the file and loading into the array until you hit the end of the file and filled up the array. So make sure your array is the proper size to hold all your numbers.

That should give you a great start and put you on your way to finding the values. Let us know if you have any other problems.

Enjoy!

"At DIC we be array loading from file code ninjas... we also load trucks with jolt cola... we love the stuff." :snap:
Was This Post Helpful? 0
  • +
  • -

#4 camckee316  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 277
  • Joined: 29-August 08

Re: Text to array

Posted 01 December 2008 - 01:25 PM

the numbers in the txt file are separated by line.
Was This Post Helpful? 0
  • +
  • -

#5 camckee316  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 277
  • Joined: 29-August 08

Re: Text to array

Posted 06 December 2008 - 10:30 AM

Still using the get total and get average code from the initial start of the thread have changed the code the main code to this:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.io.*;
import java.util.*;

public class NumberAnalysis
{
	public static void main(String[] args)
	{	
		//store numbers from the file
		List<String> numberList = new ArrayList<String>();
		
		BufferedReader br = null;
		
		try
		{
			//open file
			br = new BufferedReader(new FileReader("Numbers.txt"));
			
			String number;
			
			//loop and read line from file as long as we dont get null
			while((number = br.readLine()) != null)
			// add number to numberlist
			numberList.add(number);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{	
				//close file
				br.close();
			}
			catch (IOException ex)
			{
				ex.printStackTrace();
			}
		}
		
		//initialize new string array = to size of numberlist
		String[] number = new String[(numberList.size())];
		
		//call the numberlist toarray method to transfer items from number list to string array number
		numberList.toArray(number);
		
		//loop and display each number from the array
		System.out.println("These are the number in the file: ");
		for (int i =0; i < number.length; i++)
		System.out.println(number[i]);
		
		// finish coding total, average, lowest number, highest number,
		// get the total of the numbers in the file
		NumberTotal nt = new NumberTotal("Number.txt");
		System.out.println("The total of the numbers in the file are equal to:");
		System.out.println(nt.getTotal());
		
		// get the average of the numbers in the file
		Average ave = new Average("Number.txt");
		System.out.println("The average number in the file is:");
		System.out.println(ave.getAverage());
	}
}	


And I receive the follow errors:
----jGRASP exec: javac -g C:\Documents and Settings\Me\desktop\WITCC\3rd Quarter Classes\Java\NumberAnalysis\NumberAnalysis.java
----at: Dec 6, 2008 11:14:56 AM

----jGRASP wedge: pid for wedge is 2948.
----JGRASP wedge2: pid for wedge2 is 1736.
----JGRASP wedge2: CLASSPATH is ".;;.;C:\Program Files\QuickTime\QTSystem\QTJava.zip;C:\Program Files\jGRASP\extensions\classes".
----jGRASP wedge2: working directory is [C:\Documents and Settings\Me\desktop\WITCC\3rd Quarter Classes\Java\NumberAnalysis] platform id is 2.
----jGRASP wedge2: actual command sent ["C:\Program Files\Java\jdk1.6.0_07\bin\javac.exe" -g "C:\Documents and Settings\Me\desktop\WITCC\3rd Quarter Classes\Java\NumberAnalysis\NumberAnalysis.java"].
----JGRASP wedge2: pid for process is 3872.

C:\Documents and Settings\Me\desktop\WITCC\3rd Quarter Classes\Java\NumberAnalysis\NumberAnalysis.java:60: unreported exception java.io.IOException; must be caught or declared to be thrown
NumberTotal nt = new NumberTotal("Number.txt");
^
C:\Documents and Settings\Me\desktop\WITCC\3rd Quarter Classes\Java\NumberAnalysis\NumberAnalysis.java:65: unreported exception java.io.IOException; must be caught or declared to be thrown
Average ave = new Average("Number.txt");
^
2 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

I apologize to admin if I did not post my error listings correctly.
Would it possible for someone to explain to me what those errors mean?
Thanks.

This post has been edited by camckee316: 06 December 2008 - 10:30 AM

Was This Post Helpful? 0
  • +
  • -

#6 camckee316  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 277
  • Joined: 29-August 08

Re: Text to array

Posted 06 December 2008 - 11:02 AM

Never mind all I found my error. I needed to add "throws Exception" after public static void main(String[] args).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1