Java - Printing a message when data exceeds limit?

断了今生、忘了曾经 提交于 2019-12-31 05:28:10

问题


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

    }
}

回答1:


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.");
}

Note that I changed your strings array to split the test string instead of br.readLine()



来源:https://stackoverflow.com/questions/34065989/java-printing-a-message-when-data-exceeds-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!