无法从加载的文本文件中输出数据

时间:2013-05-15 17:04:16

标签: java file-io

我有一个程序应该加载文本文件并显示/排序数据,但数据根本没有显示。关于我做错了什么的任何想法?我必须坚持使用1.4.2 Java。

以下是代码:

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

class NewClass {
    private static int quantity;
    private static String[] name;

    public static void main(String args[]) throws Exception {
        InputStreamReader kb = new InputStreamReader(System.in);
        BufferedReader in;   
        in = new BufferedReader(kb);
        String buffer;
        char choice;
        boolean fileread=false;

        int[]number=new int[quantity];
        String[]name=new String[quantity];

        String sorttype="";
        do
        {    //Setup Menu
            choice=menu(in);
            if(choice=='E')
            {
                if(fileread)
                    System.out.println("Data already has been entered");
                 else
                {
                    fileread=true;
                    getdata(number,name);
                 }
            }
            else if(choice=='D')
            {
                if(fileread)
                    display(number,name,in);
                else
                    System.out.println("Must enter data before it is displayed");
            }
            else if(choice=='S')
            {
                if(fileread)
                    sorttype=sort(number,name,in);
                 else
                    System.out.println("Must enter data before it is sorted");
            }
        } while(choice!='X');
    }

    //Sort Data
    public static void sortint(int[] number, String[] name)
    {
        int i,j;
        for(i=0;i<quantity-1;i++)
            for(j=i+1;j<quantity;j++)
                if(number[i]>number[j])
                {
                    swap(number,i,j);
                    swap(name,i,j);
                }
    }

    public static void sortstring(String[] name, int[] number)
    {
        int i,j;
        for(i=0;i<quantity-1;i++)
            for(j=i+1;j<quantity;j++)
                if(name[i].compareToIgnoreCase(name[j])>0)
                {
                    swap(number,i,j);
                    swap(name,i,j);
                }
    }

    public static void swap(int[] a,int i,int j)
    {
        int t;
        t=a[i];
        a[i]=a[j];
        a[j]=t;
    }

    public static void swap(String[] a,int i,int j)
    {
        String t;
        t=a[i];
        a[i]=a[j];
        a[j]=t;
    }

    public static String sort(int[] number, String[] name, BufferedReader kb)throws Exception
    {
        String buffer;
        do
        {
            //Allow user to sort the phone book
            System.out.println("What do you want to sort by?");
            System.out.println("Number");
            System.out.println("Name");
            System.out.print("Enter>>");
            buffer=kb.readLine();

            if(buffer.equalsIgnoreCase("number"))
            {
                sortint(number,name);
                print(name, number,kb);
                return buffer;
            }
            else if(buffer.equalsIgnoreCase("name"))
            {
                sortstring(name,number);
                print(name,number,kb);
                return buffer;
            }
            System.out.println("Invalid entry");
        } while(true);
    }

    public static void print(String[] name, int[] number, BufferedReader kb)throws Exception
    {
        System.out.println("Sorted data");
        System.out.println("Number\tName");
        for(int i=0;i<quantity;i++)
            System.out.println(number[i]+"\t"+name[i]);
    }

    public static void display(int[] number, String[] name, BufferedReader kb)throws Exception
    {

        System.out.println("Number    Name");
        for(int i=0;i<quantity;i++)
            System.out.println(number[i]+"    "+name[i]);
    }

    public static void getdata(int number[],String name[])throws Exception
    {
        FileReader file = new FileReader("phoneData.txt");
        try (BufferedReader input = new BufferedReader(file)) {
            int i;
            String buffer;
            for( i=0;i<quantity;i++)
            {
                buffer=input.readLine();
                StringTokenizer st = new StringTokenizer(buffer, ",");
                name[i]=st.nextToken();
                number[i]=Integer.parseInt((st.nextToken()).trim());
            }
        }
    }

    public static char menu(BufferedReader kb)throws Exception
    {
        String buffer;
        char input;
        do
        {
            System.out.println("\nWhat would you like to do?");
            System.out.println("E-Enter phone book data");
            System.out.println("D-Display phone book data");
            System.out.println("X-Exit program");
            System.out.println("S-Sort list");
            System.out.print("Enter E, D, X, S>>");
            buffer=kb.readLine();
            input=(buffer.toUpperCase()).charAt(0);
            if(input=='E'||input=='D'||input=='X'||input=='S')
                 return input;
            System.out.println("Invalid entry");
        } while(true);
    }
}

以下是它的回归:

What would you like to do?
E-Enter phone book data
D-Display phone book data
X-Exit program
S-Sort list
Enter E, D, X, S>>D
Number    Name

What would you like to do?
E-Enter phone book data
D-Display phone book data
X-Exit program
S-Sort list
Enter E, D, X, S>>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您可能想要初始化quantity

private static int quantity = 1;

而不仅仅是

private static int quantity;

使循环内的代码

for( i=0;i<quantity;i++)

可以获得机会......

正如我的第一条评论中所述,您应该为代码添加一些异常处理和返回值检查。

您也可以删除此行

private static String[] name;

因为name已在main本地声明了public static void getdata(int number[],String name[])throws Exception { BufferedReader input = null; try { input = new BufferedReader(new FileReader("phoneData.txt")); int i; String buffer; for( i=0;i<quantity;i++) { // readLinde returns null when EOF is reached buffer=input.readLine(); if(buffer != null) { StringTokenizer st = new StringTokenizer(buffer, ","); name[i]=st.nextToken(); number[i]=Integer.parseInt((st.nextToken()).trim()); } else { break; // since nothing left to read // remaining buckets in the arrays are left empty } } } catch (Exception e) { // catch exceptions to where know your program fails System.out.println(e.toString()); } finally { if(input != null) { // close the input stream when you are done input.close(); } } }

修改

arrays

另外,您应该考虑使用List代替public static void getdata(List number,List name) { BufferedReader input = null; try { input = new BufferedReader(new FileReader("phoneData.txt")); String buffer; while(null != (buffer = input.readLine())) { StringTokenizer st = new StringTokenizer(buffer, ","); name.add(st.nextToken()); number.add(Integer.valueOf(Integer.parseInt((st.nextToken()).trim()))); } } catch (Exception e) { System.out.println(e.toString()); } finally { if(input != null) { try { input.close(); } catch (IOException e) { // ignore } } } }

{{1}}