Hi guys,
After 10 days of googling and trying I finally must ask. This is not an assignment for school but its a practise one for the upcoming assignment in a few weeks.
So i have the weights of 200 oranges in a csv file. With this csv file i was supposed to create an array, serialize the object Apelsinn in to orange.ser and then deserialize it. All this is done without any trouble but next step is for the user through the interface be able to see the mean weight of the 200 oranges in the csv file.
I have already opened this csv file myself and calculated the weight of all the 200 oranges which was all together 14216,21
and i took that amount and divided it by 200 as you do for the mean weight and got 71,08155.
Anyhow, what i dont get, is how to calculate this from the csv file. I hardly believe that what they want us to do is to simply create two variables such as double mean; and double amount = 200; double weights = 14216,21 and then :
mean = weights/amount;
and then use a scanner or messagedialog with joptionpane and print it out to the consol.
I doubt it is just that easy but then again i so please guys give me a hint cus that is the next step at the end of my code. So here is my code so far.
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package apelsinenn;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author Pati
*/
public class Apelsinenn implements Serializable {
//instance variabler
String fileName;
ArrayList weight;
//Konstruktor
//När du skapar ett objekt med NEW så kallas denna konstruktor.
public Apelsinenn(){
fileName = "orange";
weight = new ArrayList();
}
//Main metoden som gör alla anrop
public static void main(String[] args) throws IOException {
Apelsinenn apelsin = new Apelsinenn();
apelsin.readFile(); apelsin.printObject();
apelsin.readObject();
}
public void readFile(){
Scanner input;
try {
input = new Scanner(new FileInputStream ("C:\\javamapp\\orange.csv"));
//while((line = input.readLine()) !=null) {
while(input.hasNextDouble()) {
double vikt = input.nextDouble();
weight.add(vikt);
}
}catch (IOException e) {
//System.out.println(e);
System.out.println("Någonting gick fel, det gick inte att Läsa från filen");
}
System.out.println("Filen har läst in till ArrayList weigth");
for (int i = 0; i < weight.size(); i++) {
System.out.println( weight.get(i).toString());
}
}
/*
En metod för att skapa ett Apelsin Objekt ock skriva det till fil.
*/
public void printObject(){
//Apelsinenn apelsin = new Apelsinenn();
try {
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("C:\\javamapp\\orangeWeights.ser"));
output.writeObject(this);
output.close();
} catch (IOException ex) {
System.out.println("Något gick fel med att skriva objektet till filen");
}
System.out.println("Objektet har skrivits i orangeWeights.ser");
}
/*
Metod för att Deserializable objektet och läsa det från filen som vi skapa i printOject metoden
*/
public void readObject(){
try {
ObjectInputStream input = new ObjectInputStream(new FileInputStream("C:\\javamapp\\orangeWeights.ser"));
Apelsinenn nyapelsin = (Apelsinenn)input.readObject();
nyapelsin.printValue();
//System.out.println(nyapelsin.toString()); //skriver ut objektadressen i minnet
} catch (IOException ex) {
System.out.println("Fel vid försök att läsa från filen");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Apelsinenn.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void printValue(){
for (Object weight1 : weight) {
System.out.println(weight1);
}
System.out.println(this.fileName);
}
}
any help is appriciated. thanks