can anyone help me out with the errors .. please

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

class Directory implements Comparable {
        int id;
        String name;
        String address;
        int phoneNo;

        public int getId() {
                return id;
        }

        public void setId(int id) {
                this.id = id;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public String getAddress() {
                return address;
        }

        public void setAddress(String address) {
                this.address = address;
        }

        public int getPhoneNo() {
                return phoneNo;
        }

        public void setPhoneNo(int phoneNo) {
                this.phoneNo = phoneNo;
        }

        public int compareTo(Object ob) throws ClassCastException {
                if (!(ob instanceof Directory))
                        throw new ClassCastException("Error");
                int ide = ((Directory) ob).getId();
                return this.id - ide;
        }
}

public class TelephoneDirectory {

        public static void main(String[] args) throws Exception {
                Scanner scan = new Scanner(System.in);
                int menu = 0;
                System.out.println("Telephone Directory Management System");
                System.out.println();
                System.out.println("1. Accept Data");
                System.out.println("2. Search");
                System.out.println("3. Sort Data");
                System.out.println("4. List of all persons");
                System.out.println("5. Exit");
                boolean quit = false;
                do {
                        System.out.print("Please enter your choice: ");
                        menu = scan.nextInt();
                        System.out.println();

                        switch (menu) {
                        case 1:
                                System.out.print("Enter student ID: ");
                                int ID = scan.nextInt();
                                System.out.print("Enter Name: ");
                                String name = scan.next();
                                System.out.print("Enter Address: ");
                                String address = scan.next();
                                System.out.println("Enter Phone No: ");
                                int no = scan.nextInt();
                                FileWriter fw = new FileWriter(new File("directory.txt"), true);
                                BufferedWriter out = new BufferedWriter(fw);
                                out.write(ID + " " + name + " " + address + " " + no);
                                out.newLine();
                                out.close();
                                break;
                        case 2:
                                System.out.print("Enter name to search information: ");
                                String n = scan.next();
                                File f = new File("directory.txt");
                                try {
                                        BufferedReader freader = new BufferedReader(new FileReader(
                                                        f));
                                        String s;
                                        while ((s = freader.readLine()) != null) {
                                                String[] st = s.split(" ");
                                                String id = st[0];
                                                String nm = st[1];
                                                String add = st[2];
                                                String phoneNo = st[3];
                                                if (n.equals(nm)) {
                                                        System.out
                                                                        .println("***********Information**************");
                                                        System.out.println("Address : " + add);
                                                        System.out.println("PhoneNo : " + phoneNo);
                                                }
                                        }
                                        freader.close();
                                } catch (Exception e) {
                                }
                                break;
                        case 3:
                                File file = new File("directory.txt");
                                FileInputStream fstream = new FileInputStream(file);
                                DataInputStream in = new DataInputStream(fstream);
                                BufferedReader br = new BufferedReader(
                                                new InputStreamReader(in));
                                String strLine;
                                ArrayList list = new ArrayList();
                                while ((strLine = br.readLine()) != null) {
                                        list.add(strLine);
                                }
                                int j = 0;
                                Directory data[] = new Directory[list.size()];
                                try {
                                        Iterator itr;
                                        for (itr = list.iterator(); itr.hasNext();) {
                                                String str = itr.next().toString();
                                                String[] splitSt = str.split(" ");
                                                String id = "", nn = "", add = "", pno = "";
                                                for (int i = 0; i < splitSt.length; i++) {
                                                        id = splitSt[0];
                                                        nn = splitSt[1];
                                                        add = splitSt[2];
                                                        pno = splitSt[3];

                                                }
                                                data[j] = new Directory();
                                                data[j].setId(Integer.parseInt(id));
                                                data[j].setName(nn);
                                                data[j].setAddress(add);
                                                data[j].setPhoneNo(Integer.parseInt(pno));

                                                j++;
                                        }

                                        BufferedWriter bw = new BufferedWriter(new FileWriter(file,
                                                        true));
                                        Arrays.sort(data);
                                        System.out.println("********Sorted by id********");
                                        String strVal = "";
                                        for (int i = 0; i < 8; i++) {
                                                Directory show = data[i];
                                                int ide = show.getId();
                                                String nnn = show.getName();
                                                String add = show.getAddress();
                                                int phone = show.getPhoneNo();
                                                System.out.println(ide + " " + nnn + " " + add + " "
                                                                + phone);
                                        }
                                } catch (Exception e) {
                                }
                                break;

                        case 4:
                                FileInputStream fis = new FileInputStream(new File(
                                                "directory.txt"));
                                DataInputStream dis = new DataInputStream(fis);
                                BufferedReader reader = new BufferedReader(
                                                new InputStreamReader(dis));
                                String st;
                                ArrayList al = new ArrayList();
                                while ((st = reader.readLine()) != null) {
                                        al.add(st);
                                }
                                Iterator itr;
                                for (itr = al.iterator(); itr.hasNext();) {
                                        String str = itr.next().toString();
                                        String[] splitSt = str.split(" ");
                                        String id = "", na = "", ada = "", ph = "";
                                        for (int i = 0; i < splitSt.length; i++) {
                                                id = splitSt[0];
                                                na = splitSt[1];
                                                ada = splitSt[2];
                                                ph = splitSt[3];
                                        }
                                        System.out.println(id + " " + na + " " + ada + " " + ph);
                                }
                                break;
                        case 5:
                                quit = true;
                                break;
                        default:
                                System.out.println("Invalid Entry!");
                        }
                } while (!quit);
        }
}

can anyone please help out . to fix my code or suggest to what can i do to fix it and let it run ..

Edited 4 Years Ago by raym.mart: n/a

First let us know what you are trying to do, next tell us the errors, or the incorrect behaviour in your code, do you seriously expect people to sift through 200 lines of code searching for errors ??

Edited 4 Years Ago by stephen84s: n/a

im trying to do a phone directory.
the errors were lines 4, 50, and 54 ..

-_-

Edited 4 Years Ago by raym.mart: n/a

Dude, the error messages .... my crystal ball isn't lighting up !!! :P

Edited 4 Years Ago by stephen84s: n/a

Just a guess for the error on Line 50, is your Java file correctly named as : TelephoneDirectory.java (even the case, don't care if you are on windows or linux), the name of the file, should match the name of the public class it contains

Edited 4 Years Ago by stephen84s: n/a

Just a guess for the error on Line 50, is your Java file correctly named as : TelephoneDirectory.java (even the case, don't care if you are on windows or linux), the name of the file, should match the name of the public class it contains

Might also fix Line 4?

Edit: Nevermind, didn't see the Directory closing bracket at first glance, or "class" for that matter

Edited 4 Years Ago by turt2live: n/a

I copied and pasted the code into Eclipse and got rid of warnings, ran it and tested no issues.

Here is the code I used.

Link

(Used pastebin to reduce spam)

I copied and pasted the code into Eclipse and got rid of warnings, ran it and tested no issues.

Here is the code I used.

Link

(Used pastebin to reduce spam)

Hmmm, giving fish instead of teaching someone to fish ....

Yes the file is correctly named as TelephoneDirectory.java
yet after fixing those .. the error goes on line 42 ..

Hmmm, giving fish instead of teaching someone to fish ....

It compiled and ran without errors beforehand anyways.

I literally just removed generic types and added a few // UNUSED lines

Considering the OP never gave stack traces, I don't know how else to help.

It compiled and ran without errors beforehand anyways.

I literally just removed generic types and added a few // UNUSED lines

Considering the OP never gave stack traces, I don't know how else to help.

How about wait for him to respond with relevant info,
Now as you see he has again skipped the error message.


@raym
Error messages please ??

this was the error message:

run:
java.lang.NoClassDefFoundError: javaapplication7/TelephoneDirectory
Caused by: java.lang.ClassNotFoundException: javaapplication7.TelephoneDirectory
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: javaapplication7.TelephoneDirectory. Program will exit.
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

The Program is looking for a class called: "javaapplication7.TelephoneDirectory"
Your class (atleast the code you pasted) is in the default package, you need to declare your classes to be in the "javaapplication7" package by adding the following statement at the top of your Java file (Even before the import statements)

package javaapplication7;

Also note your classfile will need to be in a folder called 'javaapplication7'.
If you are doing this via an IDE, then I suppose it will handle these teenie weenie details.

Edited 4 Years Ago by stephen84s: n/a

Comments
missed that one, should 've read the code more carefully :)

you probably used wrong casing (upper-lower) to name your files.
two remarks: instead of throwing a ClassCast exception in your compareTo method, just return false (that is the expected behaviour)
and, don't allow your main method to throw an Exception.
if need be, put a try-catch block inside your main method to allow you to handle the exception, but try to handle the exception as soon as possible.
throwing an exception from your main method will make it impossible to handle the exception and avoid your application to crash.

good idea on putting some try-catch block method. i'd like giving it a try .. :D

This question has already been answered. Start a new discussion instead.