Java - Printing a message when data exceeds limit?(Java - 当数据超过限制时打印一条消息?)
问题描述
我的代码可以运行了,虽然不是很漂亮,但它确实是我的工作 :) 现在我想编写一段代码,如果文本文件中有 19 条或更多条数据,则停止加载数据然后显示一条消息,例如输入无效.我不确定如何执行此操作,因此将不胜感激.
I've got my code working, it's not pretty but it doe's the job :) Now I want to write a piece of code that stops the data from being loaded if there is 19 or more pieces of data in the text file and then display a message saying Invalid input for example. I'm not sure how to do this so any help would be appreciated.
package stackandqueue;
import java.util.*;
import java.util.Stack;
import java.util.Queue;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.Arrays;
public class StackAndQueue {
public static void main(String[] args) throws IOException {
// Create three empty stacks of Bays.
// Bay 1 linked list
Queue<String> bayoneStack = new LinkedList<String>();
// Bay 2 linkd list.
Queue<String> baytwoStack = new LinkedList<String>();
// Bay 3 linked list
Queue<String> baythreeStack = new LinkedList<String>();
Queue<String> bayloadStack = new LinkedList<String>();
System.out.println("***********************************************");
// Open and read text file
String inputFileName = "PodData4.txt";
FileReader fileReader = new FileReader("PodData4.txt");
// Create the FileReader object
try (BufferedReader br = new BufferedReader(fileReader);) {
// Sort the data into the relevant linked list by type F, T or P.
String[] strings = br.readLine().split(",");
for (String str : strings) {
switch (str.charAt(0)) {
case 'F':
bayoneStack.add(str);
break;
case 'T':
baytwoStack.add(str);
break;
case 'P':
baythreeStack.add(str);
break;
default:
// In-case of invalid input
}
System.out.println(str);
}
} catch (IOException ex) {
// handle exception;
} finally {
fileReader.close();
}
// Prints out the linked list stacks showing all Bays.
System.out.println("***********************************************");
System.out.println("Bay 1:Food: " + bayoneStack.toString());
System.out.println("Bay 2:Technical: " + baytwoStack.toString());
System.out.println("Bay 3:Personal: " + baythreeStack.toString());
}
}
推荐答案
只要先用条件语句检查总长度即可.类似的东西
Just check the overall length first with a conditional statement. Something like
String test = br.readLine();
if (test.length() < 19) {
String[] strings = test.split(",");
//continue operations with string input
}
else {
//change this to what you want the error to read.
System.out.println("Invalid input.");
}
请注意,我更改了您的 strings
数组以拆分 test
字符串而不是 br.readLine()
Note that I changed your strings
array to split the test
string instead of br.readLine()
这篇关于Java - 当数据超过限制时打印一条消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java - 当数据超过限制时打印一条消息?
基础教程推荐
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01