r/javahelp • u/idblue420 • 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;
 Â;
}
}
}
}
6
u/AutoModerator Feb 27 '19
You seem to try to compare String values with == or !=.
This approach does not work in Java, since String is an object data type and these can only be compared using .equals(). For case insensitive comparison, use .equalsIgnoreCase().
See Help on how to compare String values in our wiki.
Your post is still visible. There is no action you need to take. Still, it would be nice courtesy, even though our rules state to not delete posts, to delete your post if my assumption was correct.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
4
1
u/derrikcurran Feb 27 '19 edited Feb 27 '19
To fix your post:
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/
Run it through something like this: https://codebeautify.org/javaviewer
Update your original post with the result, formatted as code.
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.
 Â;
3
u/e_falk Extreme Brewer Feb 27 '19
you could at least try to make your code readable