[英]How do you uninstall a version of Java (e.g., openjdk 15) from MacOS?
[英]how do you work with data uploaded from files? e.g., calculate the sum? java
我已經使用掃描儀將文本文件讀入Java程序。 我現在要計算文件中所有整數值的總和。 文件的結構很簡單。
Nottinghill_Gate,120
High_Street_Kensignton,100
Gloucester_Road,50
South_Kensignton,200
Sloane_Square,100
Victoria,300
St_James_Park,200
Westminster,100
Embankment,200
Temple,150
Blackfriars,200
Mansion_House,300
Cannon_Street,190
Monument,200
Tower_Hill,160
Aldgate,190
Liverpoool_Street,60
Moorgate,50
Barbican,120
Farrington,130
Kings_Cross_St_Pancras,150
Euston_Square,180
Great_Portland_Street,120
Baker_Street,135
Edware_Road,112
Paddington,115
Bayswater,165
我的代碼是
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class DataScanner {
public static void readFile(String fileName) {
try {
Scanner scanner =
new Scanner(new File(fileName));
scanner.useDelimiter
(System.getProperty("line.separator"));
while (scanner.hasNext()) {
parseLine(scanner.next());
//int total =+ distance;
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void parseLine(String line) {
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter("\\s*,\\s*");
String current = lineScanner.next();
int distance = lineScanner.nextInt();
System.out.println("The current station is " + current + " and the destination to the next station is " + distance + ".");
int total =+ distance;
System.out.println("The total distance is " + total);
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("usage: java TextScanner2"
+ "file location");
System.exit(0);
}
readFile(args[0]);
}}
我知道我必須改變
int distance = lineScanner.nextInt();
System.out.println("The current station is " + current + " and the destination to the next station is " + distance + ".");
int total =+ distance;
System.out.println("The total distance is " + total);
但是我不太確定該怎么做。 你能幫我嗎?
total
變量是局部變量 ,為每行初始化,因此它將始終包含每行的距離。 為了獲得總和,您需要將其存儲為類變量 。 另外,您不應該使用靜態方法。
public class DataScanner {
private int total = 0;
public int getTotal() {
return total;
}
public void readFile(String fileName) {
// ...
}
public void parseLine(String line) {
// ...
total += distance; // update class variable
}
public static void main(String[] args) {
DataScanner scanner = new DataScanner();
scanner.readFile(args[0]);
System.out.println("The total distance is " + scanner.getTotal() + ".");
}
}
或者,您可以返回parseLine
方法中每一行的距離,並將它們加總到readFile
:
public void readFile(String fileName) {
// ...
int total = 0;
while (scanner.hasNext()) {
int distance = parseLine(scanner.next());
total =+ distance;
}
System.out.println("The total distance is " + scanner.getTotal() + ".");
}
public int parseLine(String line) {
// ...
return distance;
}
您的錯誤在於parseLine方法。
int total =+ distance;
您不是在增加,而是在重置總數
移除int
限定
public static int total = 0;
因此您的代碼如下所示:
public class DataScanner {
public static int total = 0;
public static void readFile(String fileName) {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.