3 Replies - 513 Views - Last Post: 17 January 2019 - 07:20 PM Rate Topic: -----

#1 scionics   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-January 19

File Input

Posted 17 January 2019 - 05:32 PM

I'm currently doing a mid-term project in my APCS class and my teacher is asking me to create a program that reads test scores from a file that prints statistics about the data set in a nicely formatted manner. The input file will be a text document containing integer scores in random order, with each score on its own line. The file will have no more than 50 data points.
Directory in which the file is located, Filename , Number of data points in the file: N , Mean: x ,Standard Deviation: s ,Minimum value, Maximum value.

I'm stuck on the Standard Deviation. Can someone help me out & tell me what the first step I should take from the code I have as of now? And I apologize in advance if I don't use this thing correctly.

import java.util.ArrayList;
import java.nio.file.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;


public class Statistics 
{

	public static ArrayList readFile(String filename) throws IOException 
	{
	ArrayList<Integer> values = new ArrayList<Integer>();
	Path path = FileSystems.getDefault().getPath(filename);
	int numline = 0;
	int total = 0;
	BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
	String strLine = null; 
	while ((strLine = reader.readLine()) != null) 
	{
	numline = numline + 1;
	int nextNum = Integer.parseInt(strLine);
	values.add(nextNum);
	    total =  total + nextNum;
	}
	System.out.println(total);
	System.out.println(numline);
	float mean = total / numline;
	System.out.println(mean);
	return values;
	}

   public static void getStdDev(ArrayList<Integer> values) 
   {
	   
   return;
}

   public static void main(String args[]) 
{
	Scanner kb = new Scanner(System.in);
	
	System.out.println("File: ");
	String filename = kb.next();
	
	try 
	{
      ArrayList<Integer> values = readFile(filename);
      getStdDev(values);
      System.out.println(filename);
  	  System.out.println(Paths.get(filename).getParent().getFileName());  
    } 
	catch (IOException e) 
	{
    System.out.println("Failed");
}
}
}


I need to display & calculate the directory in which the file is located, Filename , Number of data points in the file: N , Mean: x ,Standard Deviation: s ,Minimum value, Maximum value.

This post has been edited by NormR: 17 January 2019 - 06:17 PM
Reason for edit:: Removed extra code tag


Is This A Good Question/Topic? 0
  • +

Replies To: File Input

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 762
  • View blog
  • Posts: 5,723
  • Joined: 25-December 13

Re: File Input

Posted 17 January 2019 - 06:16 PM

Quote

stuck on the Standard Deviation.

It's been a while since I studied statistics. Can you google the formula for computing that?
Post the formula and your code for it if you have problems with it.
Was This Post Helpful? 0
  • +
  • -

#3 scionics   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-January 19

Re: File Input

Posted 17 January 2019 - 06:41 PM

View PostNormR, on 17 January 2019 - 06:16 PM, said:

Quote

stuck on the Standard Deviation.

It's been a while since I studied statistics. Can you google the formula for computing that?
Post the formula and your code for it if you have problems with it.
Work out the Mean (the simple average of the numbers)
Then for each number: subtract the Mean and square the result.
Then work out the mean of those squared differences.
Take the square root of that and that is the Standard Deviation.


Computing:
UsableStats: The Standard Deviation with Excel and The Online Calculator. Use the Excel Formula =STDEV( ) and select the range of values which contain the data. This calculates the sample standard deviation (n-1). Use the web Standard Deviation calculator and paste your data, one per line.

I do not know what the next step I have to take, as I figured out the mean. I am getting this Exception after I input a file directory.


Exception in thread "main" java.lang.NumberFormatException: For input string: "##"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:638)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at Statistics.readFile(Statistics.java:22)
at Statistics.main(Statistics.java:49)
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 762
  • View blog
  • Posts: 5,723
  • Joined: 25-December 13

Re: File Input

Posted 17 January 2019 - 07:20 PM

Quote

Exception in thread "main" java.lang.NumberFormatException: For input string: "##"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:638)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at Statistics.readFile(Statistics.java:22)
at Statistics.main(Statistics.java:49)

The code at line 22 called the parseInt method with data "##" which is obviously not an integer.

Where is the ## value coming from? Why is it being passed to parseInt?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1