Scanner Closed - Import Via Keyboard Numbers And Calculate Mean And Standard Deviation To Results
Oct 24, 2014
The purpose of this program is to import via keyboard Numbers.txt and calculate the mean and standard deviation to Results.txt.I am able to get the mean calculate but when I open the Results.txt the standard deviation is 0. I don't know what is wrong with my code, but I believe it has something to do with the scanner being close. Here is the error message i receive after inputting Numbers.txt.
java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Unknown Source)
at java.util.Scanner.hasNext(Unknown Source)
at StatsDemo.main(StatsDemo.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
Java Code:
import java.text.DecimalFormat; //for number formatting
import java.util.Scanner; //for keyboard input
import java.io.*; //for using files
public class StatsDemo
{
public static void main(String [] args) throws IOException
Basically I'm trying to compute the standard deviation for a set of numbers and I'm using previously know data to compute this. The data I'm working with is here (note all of this is fake as its for an assignment):
Next this is the code that I know is producing the error:
/** * Pass in original data from files, and pass in computed grades as arraylist. this returns all of the standard deviations to be printed */ public static ArrayList calcStandDev(String data1[], ArrayList<Double> grades, ArrayList<Integer> averages) { double standDev = 0; int j = 0; //gets rid of first element in string array, makes processing MUCH easier String[] data = Arrays.copyOfRange(data1, 1, data1.length);
[Code] ....
Look for the line with all the ***'s, it is producing the "java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer".
The reason I dont undersand why its producing this error is because temp.get(i) should be a integer - averages.get(i) (is type double), I do believe automatic up casting is present here making them both a double then I multiply it by itself producing a double? I dont understand where the casing is occurring. And just incase you want the formula I'm using for standard deviation here it is:
for standard deviation my output is not rounded to the 10's place, how can I make it round to the 10's place. Here is my code.
import java.util.*; public class chapter7 { public static final int Max_Number_Scores = 100; public static int fillArray(double[] scores) { System.out.print("You entered "); for (int i = 0; i < scores.length; i++) { System.out.print(scores[i] + " ");
It may be (it is highly likely) possible that I have royally stuffed this up. I am attempting to write a program that asks a user for a file name, gets the number of entries and if there is more than one entry calculate the mean, variance and standard deviation. This is my code so far:
/* Calculate contains methods that determines the number of data entries in a file, and calculates the mean, variance and standard deviation of this set * ************************************************************************/ import java.util.Scanner; import java.io.*; public class Calculate { public static void main(String[] args) throws IOException { // Create a Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in);
[Code] ....
The errors I am getting are:
Calculate.java:67: error: ';' expected mean += mean/String filename; ^ Calculate.java:67: error: not a statement mean += mean/String filename;
I am trying to create a method that calculates the standard deviation of array. What I want to be able to do is something like this
package standardDevaitionAndMean; public class StandardDeviationTest { public static void main (String[] args) { int [] array = {12,12,12,12,12,12,12}; standardDev = array.StandardDevation(); System.out.print(standardDev); } }
With something like this
package standardDevaitionAndMean; public class StandardDeviation {double mean1; double standDev; public double Mean(double ... array) { for(int i=0; i<array.length;i++)
[Code]...
So basically I want to be able to make an array in a class and be able to calculate its standard deviation with my method in my other class. I know the code I wrote is terrible but I just wanted to show what I am trying to do. I am kind of shaky on how arrays operate
I was wondering how Java knows where to Import classes from when we don't specify the whole directory path.
For example: import java.util.* Does It automatically search In both your current directory and the directory created during Installation that contains the standard library (wherever It may have been Installed) ?
I'm trying to print out the results of a program that calculates the number of seats the parties will get in an election.I have to print the partial results and the national results.
I can print te number of seats per party in each constituency, but how can i sum all seats per party in each constituency and print the national results?I'm working with vectors, which I know it might not be the best option, but everything is working, except the fact that I can't loop throuhg the vector and retrieve the total sum per party.Like, adding the seats for labour party in Constituency A and B and C, etc, and print the sum. And do the same for conservative party.Is it possible, or doIhave to create a method in Parties class to solve the problem?
This is what I have now.
for (Parties p : h.geral) { show += String.format("Constituency - %5s - %5s - %d%n", p.getConstituency(), p.getParty(), p.getNum_seats()); }
I am trying to write a loop that calculates the distance traveled (distance = speed * time). It should use the loop to calc how far the vehicle traveled for each hour of time. My program asks for hours and then mph but its not calculating time * speed. Here is my code.
public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter Hours Traveled "); int hoursTraveled = input.nextInt(); System.out.println("Enter MPH "); int mph = input.nextInt();
import java.util.Scanner; public class main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int number; System.out.println("Input a number");
[code]....
What i want to do is that if the number is greater than 50 to count until 100. else if the number is less than fifty to count for example from 36 to zero.
i am trying to turn an int number etc. 6 into 6 symbols the result would be "------". how can i transform int=6 into "------"? what function do i have to use to do this count?
public class Diagram { public static void skrivDiagram() { Random rand = new Random(); int[] frequency = new int[13]; {
for (int roll = 1; roll <= 100; roll++)
[code]....
Here i want to transform the frequency[face] into "-".
I've been working on a program where you can interact with the computer and play a guessing game of numbers between 1 and 100. It's mainly finished, I just need to add in my comments and stuff. My one problem about it that I cannot figure out is trying to get the results to give the correct int. At the end of the game, it's supposed to return the user information about how well they did like this:
Overall results: total games = int total guesses = int guesses/game = double best game = int
best game should give you the least amount of guesses you had during a particular round of the game. Here is what I have so far:
import java.util.*; public class Guess { public static final int MAX = 100; public static void main(String[] args) { intro(MAX); Scanner console = new Scanner(System.in); int tempBestGame = 0;
I'm doing a program where a user enters in exam results for students. Once he's entered the code, the code should split the results in stars in 4 categories.
I am working with a while loop that prints out infinite results. What I want to do is, if the user enters 2 digits, say "12", it should output 1 "*".
If the user enters "21" and "22", it should output "**".
There are 4 categories of marks which are marked with ints range1, range2, range3 and range4, which are user enter digits. Part 1 of the code is to split the user entered digits into its correct ranges. the ranges are 0-29, 30-39, 40-69 and 70-100. I've done the part where when the user enters a digit, it goes into the correct variable. After that when the user enters a digit over 100, the enter results should show up as stars. For example
Enter number =
21 22 44 66 44 23 11 111 (the 111 is where the program breaks and moves on)
So it should then display
0-29 ****
30-39
40-69 ***
70-100
Whatever I'm trying, it displays an infinite amount of stars. If I try to use break, It only outputs a single star, no matter how many digits I enter
Here is the code
int mark = 0; int range1 = 0, range2 = 0, range3 = 0, range4 = 0; int count1 = 0, count2 = 0, count3 = 0, count4 = 0; while (mark >=0 && mark <=100) {
[Code] .....
"The problem is with the while loop, the one above is something I was experimenting with "
I wanted to know if I was off to the right start. I am trying to write a program using the for loop the calculate the product of the consecutive numbers 4 through 8 but so for I am getting 3 values output and I only want 1 value at the print out.
The code I am using outputs the numbers too large. I am trying to see where I went wrong.
for ( int i = 4 ; i <= 8; i++) { int j = i++; int k = j++; int l = k++; int m = l++; System.out.println( + (i*j*k*l*m) ); }
I'm trying to write a program that calculates the factorials of the numbers 1 through 10, based on user input... My problem is that I don't know how to address the possibility of the user entering something other than a number. When I test the following code by entering a letter, I get an Input Mismatch exception. I'd like to be able to inform the user that the entry is invalid, and ask for another response. Here is my program thus far:
Java Code:
import java.util.Scanner; public class Factorial { public static String entryString; public static char entryChar; public static Scanner input = new Scanner(System.in);
So basically, I've been trying to create a for loop that asks the user for a number between 1 and 30, then calculates the total of the series of numbers. For example, if 15 is entered, the calculation should be 1/15+2/14+3/13+...15/1 which would equal approximately 38.1. The problem I'm having is that the code doesn't loop whenever I type a number for some reason, which results in a very incorrect calculation. Here is my code so far:
import java.util.Scanner; public class HmwLoop { public static void main(String[] args) { double sum = 0; for (double num1 = 1, num2 = 30; num1 <= 30 && num2 >= 1; num1++, num2--)
Write method distance to calculate the distance between two points (x1, y1) and (x2, y2). All numbers and return values should be of type double. Incorporate this method into an application that enables the user to enter the coordinates of the points.
Hints:
- The distance between two points can be calculated by taking the square root of
( x2 - x1 )2 + ( y2 - y1 )2
- Use Math class methods to compute the distance.
- Your output should appear as follows:
Type the end-of-file indicator to terminate
On UNIX/Linux/Mac OS X type <ctrl> d then press Enter
On Windows type <ctrl> z then press Enter
Or Enter X1: 1
Enter Y1: 1
Enter X2: 4
Enter Y2: 5
Distance is 5.000000
Type the end-of-file indicator to terminate
On UNIX/Linux/Mac OS X type <ctrl> d then press Enter
I have been working with socket programming. I was trying to develop a two way communication from server to client. The data is being received when sent from server to client but there is a problem when I am sending from client to server.
I am getting an error as "Socket is closed" pointing to the line where I have created a BufferedReader. I have simplified some part of my code to be very specific.
Code attached.
import java.io.*; import java.net.*; import java.util.*; public class Server { public static void main(String args[]) { String id="policeone"; int hashcodeid=0;
[Code] ....
Error::
Receiving string from client: 'java.net.SocketException: Socket is closed at java.net.Socket.getInputStream(Socket.java:872) at Server.main(Server.java:51)
I'm want to call another statement within the same resultSet. But it gives an error that the result set is closed.
public static void updateAlarmStatus(int status) { ResultSet rs = null; database.restartDatabase(); try { rs = database .executeQuery("Select alarmId from alarms where alarmDate = '"
[Code] ....
I googled it and came to know that resultSet is automatically closed if we call another statement within in. I've also tried creating the connection again before the second statement but of no use.
We have a servlet application running under jboss 7.1.1/java 1.7 that sends http requests to another server. Everything works fine for most of the time, but occasionally (from one to a couple of times a day) we get a “Socket closed” exception. I’ve been trying to find out what might be causing this but so far I’ve been unsuccessful. By the way, this has been happening while the application was running under older versions of Jboss/Java so the version might not be that relevant.
Here’s an excerpt from the method where this happens: . . . . . try { HttpURLConnection conn = (HttpURLConnection) urlEndpoint.openConnection(); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true);
[Code] ....
And here’s what the exception looks like: . . . . . . java.net.SocketException: socket closed at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.read(SocketInputStream.java:150) at java.net.SocketInputStream.read(SocketInputStream.java:121) at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
[Code] ....
In my investigation I came across some posts that were mentioning the sockets pool the http connection is keeping, but I’m not sure whether and how this solve the problem I have.
I am working with java project which is kind of charting room..but the problem is when am writing the query for listing the message in the conversation the error prevail in my eclipse...string literal is not properly closed by double quote...this is my java file
I have a Linux Server Debian 7 x86_64 Minimal With java version "1.7.0_65" installed..Its an online Game Server wich Players can join just like other Services.Every 10 - 15 minutes all the players get kicked from the server and get this messege: "internal exception: java.io.ioexception: an existing connection was forcibly closed by remote host"
The players can't join the server for 10 seconds and the server console doesn't show anything for 10 seconds.it doesnt show that the players even left! and when the players try to join they get htis messege: "same nick is already playing"
After 10 seconds everything will work and players can join again and server console say that everyone left and joined..But that happens again after 10 minutes and im losing lots of players because of this error.
I have to write sorting algorithm for numbers given on standard input(console) with following requirements:
The application must remember the distinction between double and integer numbers. This must be evident in the sorted output. Double type numbers are written using a period (.) to separate the fraction from the whole part.
The problem is I don't know how to efficiently distinct whether something is an int or double on standard input, to save them into 2 different arrays.
Only idea I've come to so far is to read each number as String and check if it has '.' character, to figure out if it's double and than convert it into right type and add it to correct array.
Also thought that maybe this would be achieveable using Pattern but how to do that?