December 22, 2024, 01:01:15 PM

1,531,361 Posts in 46,734 Topics by 1,523 Members
› View the most recent posts on the forum.


Java assignment

Started by Dullahan, March 05, 2009, 01:06:38 PM

previous topic - next topic

0 Members and 1 Guest are viewing this topic.

Go Down

Dullahan

March 05, 2009, 01:06:38 PM Last Edit: March 05, 2009, 01:10:23 PM by Raekewn
Code Select
import java.io.*;
public class Friends
{
    static String search;
    static int option = 0;
    static int exit = 0;
    static int optionAgain;
    static int exitAgain = 0;
    public static void main (String [] args) throws IOException
    {
        String personName;
        String personAddress;
        String personCity;
        String personProvince;
        String personCountry;
        String personNumber;
        String numberAddress;
        String fileName = "address.dat";
        String lineData;

        System.out.println ("Please choose an option");
        System.out.println ("1 - Show list");
        System.out.println ("2 - Find a name");
        System.out.println ("3 - Add to the list");
        while (exit == 0)
        {
            option = ReadLib.readInt ();
            if (option == 1)
            {
                lineData = "";
                System.out.println ("Open the file 'address' to find the list.");
                FileReader inFile = new FileReader (fileName);
                BufferedReader inBuffer = new BufferedReader (inFile);
                while (lineData != null)
                {
                    lineData = inBuffer.readLine ();
                    System.out.println (lineData);
                }
                //if statement so that it will read each line and display on the screen
                exit++;
                inBuffer.close ();
            }
            else if (option == 2)
            {
                System.out.println ("Please enter a last name:");
                search = ReadLib.readString ();

                FileReader inFile = new FileReader (fileName);
                BufferedReader inBuffer = new BufferedReader (inFile);
                exit++;

                inBuffer.close ();
            }
            else if (option == 3)
            {
                System.out.println ("You have selected to add to the list");
                exit++;
                FileWriter outFile = new FileWriter (fileName, true);
                BufferedWriter outBuffer = new BufferedWriter (outFile);
                PrintWriter printFile = new PrintWriter (outBuffer);
                while (exitAgain <= 200)
                {
                    System.out.print ("Please enter a name: ");
                    personName = ReadLib.readString ();
                    System.out.print ("Please enter a house number: ");
                    numberAddress = ReadLib.readString ();
                    System.out.print ("Please enter a street name: ");
                    personAddress = ReadLib.readString ();
                    System.out.print ("Please enter a city: ");
                    personCity = ReadLib.readString ();
                    System.out.print ("Please enter a province or state: ");
                    personProvince = ReadLib.readString ();
                    System.out.print ("Please enter a country: ");
                    personCountry = ReadLib.readString ();
                    System.out.print ("Please enter a phone number: ");
                    personNumber = ReadLib.readString ();
                    //asks you questions and then gives you thew space needed to type in
                    printFile.println (personName);
                    printFile.println (numberAddress + " " + personAddress + ", " + personCity + ", " + personProvince + ", " + personCountry);
                    printFile.println (personNumber);
                    printFile.println (" ");
                    //makes it so that the file you open will have the things that you typed into it
                    System.out.println ("Please enter an option:");
                    System.out.println ("1 - Continue");
                    System.out.println ("2 - Finish");
                    option = ReadLib.readInt ();
                    if (optionAgain == 1)
                    {
                    }
                    else if (optionAgain == 2)
                    {
                        exit = 201;
                    }
                    else
                    {
                        System.out.println ("Incorrect number. Please choose the numbers given.");
                    }
                    //gives you the option to continue or quit
                    exitAgain++;
                }
                outBuffer.close ();
            }
            else
            {
                System.out.println ("Invalid. Please enter 1, 2, or 3 for options."); //gives error
            }
        }

    } // main method

} // Friends class
I got stuck on this part:

else if (option == 2)
            {
                System.out.println ("Please enter a last name:");
                search = ReadLib.readString ();

                FileReader inFile = new FileReader (fileName);
                BufferedReader inBuffer = new BufferedReader (inFile);
                exit++;

                inBuffer.close ();
            }

I'm trying to figure out how I can search a name, and see if it matches any of the ones displayed in the list.

Anyone know what I should do? :'(

Dullahan

Okay, here's what I did:

else if (option == 2)
{
System.out.println ("Please enter a last name:");
search = ReadLib.readString ();

FileReader inFile = new FileReader (fileName);
BufferedReader inBuffer = new BufferedReader (inFile);

if (search == lineData)
{
lineData = inBuffer.readLine ();
System.out.println ("Results:");
System.out.println ("");
System.out.println (lineData);
}
else
{
System.out.println ("Sorry, you did not mathc any results.");
}

exit++;

inBuffer.close ();
}


I don't have the program with me at the moment, but I feel that there could be an error in result. Any logic problems you can see?

Daddy

What's the error given? also, .matches() might help. It returns either true or false.

Code Select
if (fooBar.matches(search)){
//whatever you want to do if there is a match
}
else{
}


I think. I haven't done much in Java recently. I'll look through my old assignments.


lol you posted that while i was searching for the method name ;_;

Dullahan

Quote from: Raekewn on March 05, 2009, 01:24:05 PM
What's the error given? also, .matches() might help. It returns either true or false.

Code Select
if (fooBar.matches(search)){
//whatever you want to do if there is a match
}
else{
}


I think. I haven't done much in Java recently. I'll look through my old assignments.


lol you posted that while i was searching for the method name ;_;
Oh, I don't know yet. I can't download Java here because our harddrive is too small. wariodood;

But it was worth searching for the method because I don't think I'm right. Hopefully it's a logic error and it would be easy to fix because I don't see how I could get a syntax.

thezerofire

I think there is a class string.equals("*word*") that you can use, but I'm not sure anymore.

Go Up