+ 1
please someone help me 🙏 to correct this code
Java file i/o https://code.sololearn.com/clHd9ZkwH14E/?ref=app
7 Réponses
+ 1
Izzat Idham
Your code have some illegal character. It happens when you copy code from somewhere.
So its solution is remove spaces and then re-structured your code.
0
import java.io.*;
import java.util.Scanner;
public class CopyFile {
public static void main(String[] args) {
try {
File file = new File("PosIni.txt");
Scanner input = new Scanner(file);
FileWriter output = new FileWriter("output.txt");
double totalWeight = 0, totalPrice = 0, heaviest = 0;
String mostHeaviest[] = null;
while (input.hasNextLine()) {
String S = input.nextLine();
String data[] = S.split(";");
totalWeight += Double.parseDouble(data[1]);
totalPrice += Double.parseDouble(data[3]);
if (Double.parseDouble(data[1]) > heaviest) {
heaviest = Double.parseDouble(data[1]);
mostHeaviest = data;
}
}
0
output.write("Monthly Summary\n=======================\n\n");
output.write("1) The total weight of all items is " + totalWeight + "\n");
output.write("2) The total price of all items is " + totalPrice + "\n");
output.write("3) The most heaviest items details : \n\n");
output.write("\tPostcode: " + mostHeaviest[0] + "\n");
output.write("\tWeight: " + mostHeaviest[1] + "\n");
output.write("\tContent Type: " + mostHeaviest[2] + "\n");
output.write("\tPostage Fee: " + mostHeaviest[3] + "\n");
input.close();
output.close();
} catch (Exception e) {
System.out.println("Error occured");
}
}
}
0
zemiak still error boss
0
zemiak 😭😭
0
I try input
AAAAAA;1.0;2.0;2.40
BBBBBB;2.5;4.0;3.20
CCCCCC;5.25;1.0;6.55
DDDDDD;1.35;3.0;3.00
and get
Monthly Summary
=======================
1) The total weight of all items is 10.1
2) The total price of all items is 15.149999999999999
3) The most heaviest items details :
Postcode: CCCCCC
Weight: 5.25
Content Type: 1.0
Postage Fee: 6.55