如何根据扫描文件中的用户输入显示消息
How to display message based on user input from scanned file
我有一个 class 可以根据用户输入的价格创建一个文本文件并放置一个时间戳,然后我可以在另一个 class.
中读取该文件
我想弄明白 如果价格与前一天的价格相差 10% 或更多,如何打印消息。 基本上 我怎样才能从文本文件中获取信息并计算价格是否在同一时间的 2 天之间变化了 10%,因此第一天中午 12 点和第二天中午 12 点。
例如,如果星期二上午 11 点的值为 50,星期三的值为 60,则应打印“11 点的价格超过 10%”
这是创建文件的代码:
class Main{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println("Price: ");
float price = scan.nextInt();
System.out.println( "Price:" + " " + price);
LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatDT = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedDT = dateTime.format(formatDT);
scan.close();
try(FileWriter fw = new FileWriter("price.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw))
{
out.println(price + " " + formattedDT);
} catch (IOException e) {
e.printStackTrace();
}
}
}
price.txt 看起来像这样:
50 29-09-2020 11:49:54
55 29-09-2020 12:54:41
60 29-09-2020 13:08:16
58 29-09-2020 14:08:21
...
60 30-09-2020 11:29:34
56 30-09-2020 12:34:21
60.3 30-09-2020 13:48:36
58.1 30-09-2020 14:18:11
这是我阅读 price.txt 文件的方式:
public class ReadFile {
public static void main(String[] args) {
try {
File readFile = new File("price.txt");
Scanner fileReader = new Scanner(readFile);
while (fileReader.hasNextLine()) {
String fileContent = fileReader.nextLine();
System.out.println(fileContent);
}
fileReader.close();
} catch (FileNotFoundException e) {
System.out.println("file was not found");
e.printStackTrace();
}
}
}
非常感谢!
使用
Files.lines(...)
逐行读取文件
Stream<String>
遍历行
String.split(...)
将每行拆分为价格和时间部分
LocalDateTime.parse(...)
将时间部分转换为 LocalDateTime
.
- 2 x 24 矩阵
Double[][]
用于缓冲两天的每小时价格
- 模运算符
%
用于在偶数天和奇数天之间切换。
查看此实现:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
public class ReadFile {
private static final DateTimeFormatter format =
DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
public static void main(String[] args) {
Double[][] prices = new Double[2][24];
AtomicInteger prevLineDayIdx = new AtomicInteger(-1);
try (Stream<String> stream = Files.lines(Paths.get("price.txt"))) {
stream.forEach(line -> {
String[] ary = line.split(" ", 2);
Double price = Double.parseDouble(ary[0]);
LocalDateTime timestamp = LocalDateTime.parse(ary[1], format);
int dayIdx = (int) timestamp.toLocalDate().toEpochDay();
int timeIdx = timestamp.getHour();
if (dayIdx != prevLineDayIdx.get()) { // Clear price buffer for
if (prevLineDayIdx.get() != -1) { // supporting line step > 1 days
for(int idx = prevLineDayIdx.get(); idx < dayIdx - 1; idx ++) {
prices[idx%2] = new Double[24];
}
}
prevLineDayIdx.set(dayIdx);
}
Double previousPrice = prices[(dayIdx - 1)%2][timeIdx];
if (previousPrice != null &&
Math.abs(previousPrice - price)/previousPrice >= 0.1d) {
System.out.println("The price " + price + " on " +
format.format(timestamp) +
" differs 10% or more from the price " +
previousPrice +
" at the same time yesterday.");
}
prices[dayIdx%2][timeIdx] = price;
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
我有一个 class 可以根据用户输入的价格创建一个文本文件并放置一个时间戳,然后我可以在另一个 class.
中读取该文件我想弄明白 如果价格与前一天的价格相差 10% 或更多,如何打印消息。 基本上 我怎样才能从文本文件中获取信息并计算价格是否在同一时间的 2 天之间变化了 10%,因此第一天中午 12 点和第二天中午 12 点。
例如,如果星期二上午 11 点的值为 50,星期三的值为 60,则应打印“11 点的价格超过 10%”
这是创建文件的代码:
class Main{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println("Price: ");
float price = scan.nextInt();
System.out.println( "Price:" + " " + price);
LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatDT = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedDT = dateTime.format(formatDT);
scan.close();
try(FileWriter fw = new FileWriter("price.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw))
{
out.println(price + " " + formattedDT);
} catch (IOException e) {
e.printStackTrace();
}
}
}
price.txt 看起来像这样:
50 29-09-2020 11:49:54
55 29-09-2020 12:54:41
60 29-09-2020 13:08:16
58 29-09-2020 14:08:21
...
60 30-09-2020 11:29:34
56 30-09-2020 12:34:21
60.3 30-09-2020 13:48:36
58.1 30-09-2020 14:18:11
这是我阅读 price.txt 文件的方式:
public class ReadFile {
public static void main(String[] args) {
try {
File readFile = new File("price.txt");
Scanner fileReader = new Scanner(readFile);
while (fileReader.hasNextLine()) {
String fileContent = fileReader.nextLine();
System.out.println(fileContent);
}
fileReader.close();
} catch (FileNotFoundException e) {
System.out.println("file was not found");
e.printStackTrace();
}
}
}
非常感谢!
使用
Files.lines(...)
逐行读取文件Stream<String>
遍历行String.split(...)
将每行拆分为价格和时间部分LocalDateTime.parse(...)
将时间部分转换为LocalDateTime
.- 2 x 24 矩阵
Double[][]
用于缓冲两天的每小时价格 - 模运算符
%
用于在偶数天和奇数天之间切换。
查看此实现:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
public class ReadFile {
private static final DateTimeFormatter format =
DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
public static void main(String[] args) {
Double[][] prices = new Double[2][24];
AtomicInteger prevLineDayIdx = new AtomicInteger(-1);
try (Stream<String> stream = Files.lines(Paths.get("price.txt"))) {
stream.forEach(line -> {
String[] ary = line.split(" ", 2);
Double price = Double.parseDouble(ary[0]);
LocalDateTime timestamp = LocalDateTime.parse(ary[1], format);
int dayIdx = (int) timestamp.toLocalDate().toEpochDay();
int timeIdx = timestamp.getHour();
if (dayIdx != prevLineDayIdx.get()) { // Clear price buffer for
if (prevLineDayIdx.get() != -1) { // supporting line step > 1 days
for(int idx = prevLineDayIdx.get(); idx < dayIdx - 1; idx ++) {
prices[idx%2] = new Double[24];
}
}
prevLineDayIdx.set(dayIdx);
}
Double previousPrice = prices[(dayIdx - 1)%2][timeIdx];
if (previousPrice != null &&
Math.abs(previousPrice - price)/previousPrice >= 0.1d) {
System.out.println("The price " + price + " on " +
format.format(timestamp) +
" differs 10% or more from the price " +
previousPrice +
" at the same time yesterday.");
}
prices[dayIdx%2][timeIdx] = price;
});
} catch (IOException e) {
e.printStackTrace();
}
}
}