在循环中调用switch语句并验证从文件

时间:2017-05-06 10:11:24

标签: java arrays file-io exception-handling switch-statement

import java.util.*;
import java.io.*;
public class Marks
{
    public static void main (String [] args)
    {
        String fileName;
        int studentCount;
        Scanner sc = new Scanner(System.in);
        System.out.println("enter file name");
        fileName = sc.nextLine();
        studentCount = numLine(fileName);
        String arrayString[] = new String[studentCount];
        double arrayReal[] = new double[studentCount];
        readFile(fileName, arrayString, arrayReal);
    }



    public static int numLine (String fileName)
    {
        FileInputStream fileStrm = null;
        InputStreamReader rdr;
        BufferedReader bufRdr;
        int lineNum=0;
        String line;
        try
        {
            fileStrm = new FileInputStream(fileName);
            rdr = new InputStreamReader(fileStrm);
            bufRdr = new BufferedReader(rdr);


            line = bufRdr.readLine();
            while (line != null)
            {
                lineNum++;
               line = bufRdr.readLine();
            }
            fileStrm.close();

        }
        catch (IOException e)
        {
            if(fileStrm != null)
            {
                try
                {
                    fileStrm.close();
                }
                catch (IOException ex2){}
            }
            System.out.println("error in file processing"+ e.getMessage());
        }
        return lineNum;
    }

    public static void readFile(String fileName, String arrayString[], double arrayReal[])
    {
        FileInputStream fileStrm = null;
        InputStreamReader rdr;
        BufferedReader bufRdr;
        int lineNum;
        String line;
        try
        {
            fileStrm = new FileInputStream(fileName);
            rdr = new InputStreamReader(fileStrm);
            bufRdr = new BufferedReader(rdr);
            for(int i=0;i<arrayString.length;i++)
            {
                line = bufRdr.readLine();
                arrayString[i] = processString(line);
                arrayReal[i] = processReal(line);
            }
            choice(arrayString, arrayReal);
        }
        catch(IOException e)
        {
            if(fileStrm != null)
            {
                try
                {
                    fileStrm.close();
                }
                catch (IOException ex2){}
            }
            System.out.println("error in file processing");
        }
    }



    public static String processString(String line)
    {
        String stringPart;
        String lineArray[] = line.split(",");
        stringPart = lineArray[0];
        return stringPart;
    }



    public static double processReal(String line)
    {
        double realPart;
        String lineArray[] = line.split(",");
        realPart = Double.parseDouble (lineArray[1]);
        if (realPart<0 || realPart>100)
        {
            System.out.println("invalid testmark");
        }
        return realPart;
    }

    public static void choice( String arrayString[], double arrayReal[])
    {
        int choice;
        Scanner sc = new Scanner(System.in);
        System.out.println("1.display mark");
        System.out.println("2.exit");
        choice = sc.nextInt();
        if (choice == 2)
        {
            System.out.println("exiting");
        }
        while (choice !=2)
        {
            switch (choice)
            {
                case 1:
                    output(arrayString, arrayReal);
                    break;
                case 2:
                    System.out.println("exiting");
                    break;
                default:
                    System.out.println("invalid choice choose between 1 and 2");
            }
            System.out.println("1.display marks");
            System.out.println("2.exit");
            choice = sc.nextInt();
        }
    }



    public static void output(String arrayString[], double arrayReal[])
    {
        String name;
        Scanner sc = new Scanner(System.in);
        for (int i=0;i<arrayString.length;i++)
        {
            System.out.println(arrayString[i]);
        }
        System.out.println("enter stident name");
        for (int j=0;j<arrayString.length;j++)
        {
            if (arrayString[j].equals(name))
            {
                System.out.println("mark of " + arrayString[j] + "is " + arrayReal[j]);  
            }
        }
    }
}

这是一个从csv文件中获取名称和标记的代码。它编译和部分工作。我想检查文件中的有效值,例如。 0到100之间的标记,如果它无效退出程序,但我的程序只显示&#34;无效标记&#34;并继续。如果文件为空,则打印出文件为空。我不知道在哪里放置代码以及如何。最后我希望它保持循环,直到输入有效名称,但是当我在第二次循环后测试它时,我在线程&#34; main&#34;中得到异常。错误。我知道我的选择方法和捕捉异常是不正确的,但我想不出任何其他方式来改变它。

1 个答案:

答案 0 :(得分:0)

可能会有所帮助的两件事:

  1. 如果您要退出,请使用System.exit(0);
  2. 不要随时随地Scanner创建新的new Scanner(System.in)
  3. 只创建一个并在整个程序中使用它,如下所示:

     public class Marks {
        // Set up global Scanner "sc" that we'll use everywhere
        static Scanner sc = new Scanner(System.in);
    
        public static void main(String args[]){
           ...
           // Don't make a new scanner, just use the global one
           // Scanner sc = new Scanner(System.in);
        }
        ....
        public static void choice(){
            // Don't create scanner again; just use the global one
            // Scanner sc = new Scanner(System.in);
            ...
            choice = sc.nextInt();
            ...
        }