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

3

u/e_falk Extreme Brewer Feb 27 '19

you could at least try to make your code readable

1

u/idblue420 Feb 27 '19

Does that help!

2

u/shagieIsMe Extreme Brewer Feb 27 '19

If you take your code (with indentation), paste it into stack overflow text area, select it all, hit the {} button in the editor, and then copy and paste it back here it will be properly formatted so that it will look something more like:

for (int foo = 0; foo < bar; foo += 2) {
    System.out.println("baz");
}

Indenting and line numbers (and avoiding any other markdown bits of * getting changed into italics text) will further enhance the readability of your code.

1

u/dusty-trash Feb 27 '19

Why don't you post the error? I can't understand your thinking here.

"fix the error" doesn't tell us anything.

-1

u/idblue420 Feb 27 '19

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 B : Book)

{
if (B.firstName.indexOf(str) != -1);
{
System.out.println(A);
}
else if (B.lastName.indexOf(str) != -1){

System.out.println(A);
} else if (B.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(B);
} else {

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

}

break;
case 5:
break;
}

}
}
}

5

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.

1

u/[deleted] Feb 28 '19

Roasted

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

u/e_falk Extreme Brewer Feb 27 '19

 Â;

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.

 Â;