r/javahelp • • Feb 27 '19

Homework Help me to fix the error!!!!

import java.util.ArrayList;
import java.util.Scanner;
public class Main {

String firstName;
String lastName;
String phoneNumber;
String email;
public Main() {

this.firstName = "";
this.lastName = "";
this.phoneNumber = "";
this.email = "";
}

public Main(String fName, String lName, String phone, String email) {

this.firstName = fName;
this.lastName = lName;
this.phoneNumber = phone;
this.email = email;
}

public void setFirstName(String fName) {

this.firstName = fName;
}

public void setLastName(String lName) {

this.lastName = lName;
}

public void setNumber(String phone) {

this.phoneNumber = phone;
}

public void setEmail(String email) {

this.email = email;
}

public String getFirstName() {

return this.firstName;
}

public String getLastName() {

return this.lastName;
}

public String getNumber() {

return this.phoneNumber;
}

public String getEmail() {

return this.email;
}

public static void main(String args[]) {

ArrayList<Main> Book = new ArrayList<Main>();
while (true) {

System.out.println("Contact book menu");
System.out.println("-------------------------------------------");
System.out.println("Press 1: Add a new contact.");
System.out.println("Press 2: Display all contacts.");
System.out.println("Press 3: Seach contacts.");
System.out.println("Press 4: Delete a contact.");
System.out.println("Press 5: Exit menu.");
Scanner keyboard = new Scanner(System.in);
int input = keyboard.nextInt();
switch (input) {

case 1:
Main A = new Main();
System.out.println("Enter the first name: ");
A.firstName = keyboard.next();
System.out.println("Enter the last name: ");
A.lastName = keyboard.next();
System.out.println("Enter the phone number: ");
A.phoneNumber = keyboard.next();
System.out.println("Enter the e-mail address: ");
A.email = keyboard.next();
break;
case 2:
for (Main A : Book)
Â;
{

System.out.println("Name: " + A.firstName + " " + A.lastName + "\nPhone: " + A.phoneNumber + "\nE-mail: " + A.email);
}
break;
case 3:
System.out.println("Enter the contact name you wish to search for: ");
 Â;
 Â;
 Â;
 Â;
String str = keyboard.next();
 Â;
 Â;
 Â;
 Â;
Â

Â;
 Â;
 Â;
 Â;
 Â;
 Â;
Â;
for (Main A : Book)
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
{

 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
if (A.firstName.indexOf(str) != -1) Â;
{

 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
System.out.println(A);
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
}

if (A.lastName.indexOf(str) != -1) {

 Â;
System.out.println(A);
} else if (A.email.indexOf(str) != -1) {

 Â;
System.out.println(A);
} else {

 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
System.out.println("No results.");
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
}

 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
}

break;
case 4:
System.out.println("Enter the contact name you wish to delete: ");
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
for (Main A : Book)
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
{

 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
if (A.firstName.indexOf(str) != -1) Â;
{

 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
System.out.println(A);
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
}

else if (A.lastName.indexOf(str) != -1){

 Â;
System.out.println(A);
} else if (A.email.indexOf(str) != -1) {

 Â;
System.out.println(A);
} else {

 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
System.out.println("No results.");
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
}

System.out.println("Are you sure you want to delete? (y/n)");
String deletion = keyboard.next();
if (deletion == "y") {

 Â;
 Â;
 Â;
Book.remove(A);
} else {

 Â;
System.out.println("The contact(s) was not removed.");
}

 Â;
 Â;
 Â;
 Â;
 Â;
 Â;
}

break;
 Â;
 Â;
case 5:
break;
 Â;
}

}
}
}

0 Upvotes

10 comments sorted by

View all comments

1

u/derrikcurran Feb 27 '19 edited Feb 27 '19

To fix your post:

  1. Get a clean copy of your code without the hieroglyphics, like the version you posted here: https://www.reddit.com/r/javahelp/comments/av73gs/help_me_to_fix_the_error/ehd5n6k/

  2. Run it through something like this: https://codebeautify.org/javaviewer

  3. Update your original post with the result, formatted as code.

  4. Consider including the error message so people don't have to compile the code.

To fix your code:

One of your ifs has a syntax error: if (B.firstName.indexOf(str) != -1); {.

Delete the ;. There are other errors but the error messages will be more helpful once you fix that first issue.

 Â;