Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我该如何修改它,使其只要求用户再次输入文件名,而不是将其破坏?_Java - Fatal编程技术网

Java 我该如何修改它,使其只要求用户再次输入文件名,而不是将其破坏?

Java 我该如何修改它,使其只要求用户再次输入文件名,而不是将其破坏?,java,Java,问题 当用户将文件名输入到程序中时,它将创建一个异常,说明目录中没有这样命名的文件 我想要的是,程序将重复要求用户输入文件名的消息,而不是显示异常 我的代码: import java.io.*; import java.util.*; public class reader { static int validresults = 0; static int invalidresults = 0; //used to count the number of invalid and vali

问题

当用户将文件名输入到程序中时,它将创建一个异常,说明目录中没有这样命名的文件

我想要的是,程序将重复要求用户输入文件名的消息,而不是显示异常

我的代码:

import java.io.*;
import java.util.*;


public class reader {


static int validresults = 0;
static int invalidresults = 0;
//used to count the number of invalid and valid matches

public static boolean verifyFormat(String[] words) {
    boolean valid = true;
    if (words.length != 4) { 
        valid = false;
    } else if (words[0].isEmpty() || words[0].matches("\\s+")) {
        valid = false;
    } else if ( words[1].isEmpty() || words[1].matches("\\s+")) {
        valid = false;
    }

    return valid && isInteger(words[2]) && isInteger(words[3]);}

//checks to see that the number of items in the file are equal to the four needed and the last 2 are integers
//also checks to make sure that there are no results that are just whitespace

public static boolean isInteger( String input ) {
    try {
        Integer.parseInt( input );
        return true;
    }
    catch( Exception e ) {
        return false;
    }
}
//checks to make sure that the data is an integer

public static void main(String[] args) throws FileNotFoundException {

    Scanner sc = new Scanner(System.in);
      while(true){ //Runs until it is specified to break

        System.out.println("Enter filename");
        String fileName = sc.nextLine();

        if(fileName != null && !fileName.isEmpty()){ 
          processFile(fileName);
        }else{

        }
      }
    }

private static void processFile(String fileName) throws FileNotFoundException {
    String hteam;
    String ateam;
    int hscore;
    int ascore;
    int totgoals = 0;

    Scanner s = new Scanner(new BufferedReader(
            new FileReader(fileName))).useDelimiter("\\s*:\\s*|\\s*\\n\\s*");



    while (s.hasNext()) {
        String line = s.nextLine();
        String[] words = line.split("\\s*:\\s*");
        //splits the file at colons

        if(verifyFormat(words)) {
            hteam = words[0];       // read the home team
            ateam = words[1];       // read the away team
            hscore = Integer.parseInt(words[2]);       //read the home team score
            totgoals = totgoals + hscore;
            ascore = Integer.parseInt(words[3]);       //read the away team score
            totgoals = totgoals + ascore;
            validresults = validresults + 1;

            System.out.println(hteam + " " +  "[" + hscore + "]" +  " " + "|" + " " + ateam + " " + "[" + ascore + "]");   
            //output the data from the file in the format requested

        }
        else{
            invalidresults = invalidresults + 1;
        }
    }
System.out.println("Total number of goals scored was " + totgoals);
//displays the the total number of goals
System.out.println("Valid number of games is " + validresults);
System.out.println("Invalid number of games is " + invalidresults);

    System.out.println("EOF");
}
}

您可以通过执行以下操作,首先尝试确定文件是否存在:

File file = new File(fileName);
if(file.exists()) {
    processFile(fileName)
} 
这是解决办法

public static void main(String[] args) throws FileNotFoundException {

  Scanner sc = new Scanner(System.in);
  while(true){ //Runs until it is specified to break

    System.out.println("Enter filename");
    String fileName = sc.nextLine();
    File file = new File(fileName);
    if(!file.exists()) {
      continue;
    }

    processFile(fileName);

  }
}
使用此代码:

String fileName;
File file;

for(;;) {
    /* read file name */
    System.out.print("enter file name: ");
    fileName = bufferedReader.readLine();
    file = new File(fileName);

    /* check path */
    if (!file.exists())
        System.out.println("file doesn't exist");
    else if(!file.isFile())
        System.out.println("file isn't a regular file");
    else if (...)
        ...
    else
        break;
}
其中
bufferedReader
bufferedReader bufferedReader=new bufferedReader(new InputStreamReader(System.in))如果要从键盘读取文件名

您可以检查文件是否存在
exists()
,是否是常规文件
isFile()
,是否是目录
isDirectory()
,是否可以读取
carRead()
,是否可以写入
canWrite()
,是否执行
canExecute()
,等等