Java – 当数据超出限制时打印消息?

我有我的代码工作,它不漂亮,但它的工作:)现在我想写一段代码,如果文本文件中有19个或更多的数据,然后显示停止数据加载例如,输入无效输入的消息。 我不知道如何做到这一点,所以任何帮助将不胜感激。

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 bayoneStack = new LinkedList(); // Bay 2 linkd list. Queue baytwoStack = new LinkedList(); // Bay 3 linked list Queue baythreeStack = new LinkedList(); Queue bayloadStack = new LinkedList(); 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()); } } 

只需使用条件语句检查总长度。 就像是

 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()