简体   繁体   中英

Trouble outputting data from a loaded text file

I have a program that is supposed to load a text file and display/sort the data, however the data is not being displayed at all. Any ideas as to what I'm doing wrong? I have to stick with 1.4.2 Java only.

Here is the code:

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

And here is what it is returning:

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>>

Any help is much appreciated.

You might want to initialize quantity

private static int quantity = 1;

instead of just

private static int quantity;

so that the code inside the the loop

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

can get a chance....

And as stated in my first comment, you should add some Exception handling and return value checking to your code.

Also you might just delete this line

private static String[] name;

since you have name declared locally in main .

EDIT

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

Also you should consider using List instead of arrays

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
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM