无法在以下程序中读取存储的名称

时间:2014-11-11 05:40:21

标签: java debugging

import java.lang.*;
import java.util.*;
import java.io.*;
public class Names
{
static Scanner in=new Scanner(System.in);

public void timeloop() //unnecessary. for typing effect only
{
    for(int i=0;i<=75000000;i++)
    {
    }
}

public void read()throws IOException //Stores in file
{
    FileWriter fout=new FileWriter("E:\\Vista Data\\Pranjal\\Desktop\\Names.txt",true);
    BufferedWriter bout=new BufferedWriter(fout);
    PrintWriter pout=new PrintWriter(bout);

    String out="\fAccessing E:\\Vista Data\\Pranjal\\Desktop\\Names.txt";

    for(int i=0;i<out.length();i++)
    {
        System.out.print(out.charAt(i));
        timeloop();
    }
    System.out.println("");
    System.out.print("Enter any key to continue... ");
    in.next();

    System.out.println("\fEnter number of students");
    int x=in.nextInt();

    System.out.print("");
    in.nextLine();

    for(int n=0;n<x;n++)
    {
        System.out.println("\nEnter a name");
        String name = in.nextLine();
        pout.println(name);
    }

    pout.flush();
    pout.close();

    main();
}

public void write()throws IOException //Reads from file
{
    FileReader fin=new FileReader("E:\\Vista Data\\Pranjal\\Desktop\\Names.txt");
    BufferedReader bin=new BufferedReader(fin);

    String out="Accessing E:\\Vista Data\\Pranjal\\Desktop\\Names.txt";

    for(int i=0;i<out.length();i++)
    {
        System.out.print(out.charAt(i));
        timeloop();
    }
    System.out.println("");
    System.out.print("Enter any key to continue... ");
    String temp=in.next();

    System.out.print("\f");

    String[] str=new String[100];
    int wordcnt=0;

    System.out.println("ERROR");
    while((str[wordcnt]=bin.readLine())!=null)
    {
        System.out.println("Name of student:"+(str[wordcnt].toUpperCase()));
        wordcnt++;
    }

    main();
}

public int cnt()throws IOException //counts number of lines in file
{
    FileReader fin=new FileReader("E:\\Vista Data\\Pranjal\\Desktop\\Names.txt");
    BufferedReader bin=new BufferedReader(fin);

    int cnt=0;
    String[] str=new String[100];
    while((str[cnt]=bin.readLine())!=null)
    {
        cnt++;
    }
    return cnt;
}

public String opt() //Accepts option from the user
{
    System.out.println("\fIf you would like to read the stored names, enter R");
    System.out.println("If you would like to append the file, enter A");
    System.out.println("If you would like to exit the program, enter E");

    String opt=(in.next());

    return opt;
}

public static void main()throws IOException
{
    Names name=new Names();

    switch(name.opt())
    {
        case "R":
        {
            System.out.println("\fNumber of students:"+(name.cnt())+"\n");
            name.write();
        }
        break;

        case "A":
        {
            name.read();
        }
        break;

        case "E":
        {
            System.exit(0);
        }
        break;

        default:
        {
            System.out.println("Wrong input... ABORTING");
            System.exit(0);
        }
    }
}
}

请注意,必须根据FileInputStream对象创建和FileOutputStream对象创建中的用户更改文件路径。

这是一项学校作业。在具有类似代码的其他程序中也存在同样的问题。

1 个答案:

答案 0 :(得分:1)

您应该使用main方法来运行程序,因此请将方法的签名更改为:

public static void main(String[] args)throws IOException

由于方法签名已更改,您必须在方法mainread()中修改此write()方法的方法调用,如下所示:

main(null)

通过执行这些更改,您的程序可以正常运行。

最后,只是一个建议,read()方法实际上正在执行写操作,而write()正在执行读操作。因此,更改方法的名称,同样更改main()方法中的代码,以便它正确调用读取和写入方法。