473,805 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

3 New Member
"Numbers.tx t"
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 1244
Jah Mason
3 New Member
Never mind....figured it out eventually, been avoiding the use of Input Streams and Buffered Readers for the sake of simplicity...bu t 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
2395
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
3798
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 displays these fileds and values in different ways so am creating one page and parsing variables to it. The problem appears to be in trying to write the values to a text file. The page displays the field names correctly but when I submit the form...
1
1509
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
1252
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
1173
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 value and a double seperated by a comma, these relate to a raw material and its price, the file has 100+ such lines. what i need to do is to match the string value which my program spits out with a string value in the file and then use the double...
6
9311
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 -item2 Is -item3 Really -item4
6
1494
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 phillreader = My.Computer.FileSystem.ReadAllText("C:\Users\Richard\Documents\test.txt") phillcount = phillreader.ReadLine(1)
4
3420
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 database. What I need help with is on how to locate a specific line in the file, change it and then save the updated text file. Can anyone help me out or point me to a site that explains this clearly? Here's part of my code that reads the contents of...
3
1537
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 should use BeginRead() and EndRead() of FileStream class, but I dont know how to use them. I dont understand the parameters of them. If any one could explain, I'll be grateful. Thanks
4
2552
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 properly. Can somebody give me a good example code howto do it ?? Here is extract of my code below:- public class testing extends JPanel{ private ArrayList<Myclass> lines; private HashMap<String,Color> hm = new...
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10614
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.