"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*/ -
ackage arraysfiles;
-
import UsingJava.*;
-
import java.util.*;
-
import java.io.*;
-
/**
-
*
-
* @author Jah Mason
-
*/
-
public class ArraysFiles {
-
-
public static void main(String[] args) {
-
-
int i,numb;
-
double value,maxVal,minVal,mean,sum,variance,range;
-
-
System.out.println("Please enter Integer number of values: ");
-
numb= TextIO.getlnInt();
-
while (numb<0){
-
System.out.println("Please enter positive number of values");
-
numb= TextIO.getlnInt();
-
}
-
-
-
double numbers []=new double [numb];
-
-
sum=0;mean=0;
-
for (i=0; i<numbers.length; i++){
-
-
System.out.println("Please enter Value");
-
value=TextIO.getlnDouble();
-
-
numbers[i]+= value;
-
sum+=value;
-
mean=sum/numbers.length;
-
}
-
maxVal=numbers[numbers.length-1];minVal=numbers[0];
-
for (i=0;i<numbers.length;i++){
-
if(numbers[i]>maxVal)
-
maxVal=numbers[i];
-
if(numbers[i]<minVal)
-
minVal=numbers[i];
-
}
-
//Arrays.sort(numbers); //Alternative sort to get maximum and minimum values
-
//maxVal=numbers[numbers.length-1];
-
//minVal=numbers[0];
-
-
range=maxVal-minVal;
-
variance=((sum*sum)-((sum*sum)/numbers.length))/(numbers.length-1);
-
-
System.out.println("Max is:" + maxVal);
-
System.out.println("Min is:" + minVal);
-
System.out.println("The Range of Values from Maximum to Minimum is " + range);
-
System.out.println("The Variance is " + variance);
-
System.out.println("Mean of Numbers is:" + mean);
-
System.out.println(Arrays.toString(numbers));
-
-
-
}
-
-
}
-
1 1244
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: -
import java.util.*;
-
import java.io.*;
-
/**
-
*
-
* @author Jah Mason
-
*/
-
public class ArrayFiles {
-
-
public static void main(String[] args)
-
throws IOException, FileNotFoundException
-
{
-
int i,numVal;
-
-
double maxVal,minVal,mean,sum,variance,range;
-
-
-
System.out.println("Please enter Integer number for size sample: ");
-
-
-
numVal= TextIO.getlnInt();
-
-
-
while (numVal<0){
-
System.out.println("Please enter positive number of values");
-
numVal= TextIO.getlnInt();
-
}
-
-
//Declaration of an array for numbers entered by the user
-
double numbers []=new double [numVal];
-
-
-
FileInputStream valstream= new FileInputStream("C:\\Numbers.txt");
-
-
-
DataInputStream values=new DataInputStream(valstream);
-
-
-
BufferedReader readVal = new BufferedReader(new InputStreamReader(values));
-
-
/*Loop to convert string data to double data and then assign these values into
-
the array "numbers"*/
-
for (i=0; i<numVal; i++){
-
numbers[i]=Double.parseDouble(readVal.readLine());}
-
-
System.out.println("The sample array of values is: "+Arrays.toString(numbers));
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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?
|
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...
|
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
|
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.
|
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...
| |
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
|
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)
|
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...
|
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
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
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...
| |