473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to read number values stored in columns in a text file into an array?

"Numbers.txt"
1
3
5
4
2

/*Currently i have functionality for a user to choose a number of values to be entered into an array, add numbers into the array "numbers" and process the mean, range, variance, maximum, minimum and display the array. Now i'm trying to modify the code to read values from a text file into the array "numbers" and apply the same functions*/

Expand|Select|Wrap|Line Numbers
  1. ackage arraysfiles;
  2. import UsingJava.*;
  3. import java.util.*;
  4. import java.io.*;
  5. /**
  6.  *
  7.  * @author Jah Mason
  8.  */
  9. public class ArraysFiles {
  10.  
  11.     public static void main(String[] args) {
  12.  
  13.         int i,numb;
  14.         double value,maxVal,minVal,mean,sum,variance,range;
  15.  
  16.         System.out.println("Please enter Integer number of values: ");
  17.         numb= TextIO.getlnInt();
  18.             while (numb<0){
  19.                System.out.println("Please enter positive number of values");
  20.                numb= TextIO.getlnInt();
  21.            }
  22.  
  23.  
  24.         double numbers []=new double [numb];
  25.  
  26.         sum=0;mean=0;
  27.        for (i=0; i<numbers.length; i++){
  28.  
  29.            System.out.println("Please enter Value");
  30.            value=TextIO.getlnDouble();
  31.  
  32.            numbers[i]+= value;
  33.            sum+=value;
  34.            mean=sum/numbers.length;
  35.         }
  36.         maxVal=numbers[numbers.length-1];minVal=numbers[0];
  37.         for (i=0;i<numbers.length;i++){
  38.             if(numbers[i]>maxVal)
  39.                 maxVal=numbers[i];
  40.             if(numbers[i]<minVal)
  41.                 minVal=numbers[i];
  42.         }
  43.         //Arrays.sort(numbers); //Alternative sort to get maximum and minimum values
  44.         //maxVal=numbers[numbers.length-1];
  45.         //minVal=numbers[0];
  46.  
  47. range=maxVal-minVal;
  48. variance=((sum*sum)-((sum*sum)/numbers.length))/(numbers.length-1);
  49.  
  50.         System.out.println("Max is:" + maxVal);
  51.         System.out.println("Min is:" + minVal);
  52.         System.out.println("The Range of Values from Maximum to Minimum is " + range);
  53.         System.out.println("The Variance is " + variance);
  54.         System.out.println("Mean of Numbers is:" + mean);
  55.         System.out.println(Arrays.toString(numbers));
  56.  
  57.  
  58.     }
  59.  
  60. }
  61.  
Nov 24 '10 #1
1 1232
Never mind....figured it out eventually, been avoiding the use of Input Streams and Buffered Readers for the sake of simplicity...but once you understand how they're used...it is simple.

But for the benefit of anyone with a similar problem, you can refer to the code below:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.*;
  3. /**
  4.  *
  5.  * @author Jah Mason
  6.  */
  7. public class ArrayFiles {
  8.  
  9.     public static void main(String[] args)
  10.             throws IOException, FileNotFoundException
  11.     {  
  12.         int i,numVal;
  13.  
  14.         double maxVal,minVal,mean,sum,variance,range;
  15.  
  16.  
  17.         System.out.println("Please enter Integer number for size sample: ");
  18.  
  19.  
  20.         numVal= TextIO.getlnInt();
  21.  
  22.  
  23.         while (numVal<0){
  24.                System.out.println("Please enter positive number of values");
  25.                numVal= TextIO.getlnInt();
  26.            }
  27.  
  28.         //Declaration of an array for numbers entered by the user
  29.         double numbers []=new double [numVal];
  30.  
  31.  
  32.         FileInputStream valstream= new FileInputStream("C:\\Numbers.txt");
  33.  
  34.  
  35.         DataInputStream values=new DataInputStream(valstream);
  36.  
  37.  
  38.         BufferedReader readVal = new BufferedReader(new InputStreamReader(values));
  39.  
  40. /*Loop to convert string data to double data and then assign these values into
  41.  the array "numbers"*/
  42.          for (i=0; i<numVal; i++){
  43.             numbers[i]=Double.parseDouble(readVal.readLine());}
  44.  
  45. System.out.println("The sample array of values is: "+Arrays.toString(numbers));
  46.  
Nov 25 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: NewToPHP | last post by:
I am COMPLETELY NEW TO PHP San someone supply me with a PHP script to read and write to a text file on the server Also any suggestion on a free PHP editor?
5
by: Paul C-T | last post by:
Hi, Am I trying to be too clever here? I am trying to write a PHP page to enable me to enter values into a form then write those values to a text file. I want to use the form & table that...
1
by: nasir872 | last post by:
how to read from a csv or text file and write in another file ? please help. i need a working code example. thanks
1
by: nife | last post by:
(perl) hi guys, ive searched many places for a solution and cant find one. problem: i need to extract information about an image that is stored in a text file.
1
by: bringabeeralong | last post by:
Slowly going out of my mind trying to find out how to do this, as you may have guessed i am quite new to java. i have a text file which contains a list of values, on each line there is a string...
6
by: kimiraikkonen | last post by:
Hi, I want to save all the item content of a listbox line by line into a simple text file then recall them when my project is opened. For example listbox1 contains: That - item1 Group ...
6
by: hermitking | last post by:
i need to read line 2 of a text file but not line 1 here is what i tryed Dim MyTime = Format(Now, "dd-MMM-yyyy") Dim phillstatus As String Dim phillcount As String Dim phillreader As String...
4
by: Keith G Hicks | last post by:
I'm trying to read a text file and alter the contents of specific lines in the file. I know how to use streamreader to read each line of a file. I'm doing that already to get the data into a...
3
JustRun
by: JustRun | last post by:
Hi all I'm trying to read some lines from a text file, and don't know how to do this. Actually I could read the whole file using object of StramReader, but to read custom line I think that I...
4
by: crochunter | last post by:
Hi, I want to read values from a text files from specified fields and use them as values to fill my methods inside the paintComponent() method. I am using for loop to do that but not able to do it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.