Can anyone help me to fix the errors of this code. When I compile these are the
ID: 3634640 • Letter: C
Question
Can anyone help me to fix the errors of this code. When I compile these are the errors I get:TestName.java:29: cannot find symbol
symbol : variable response
location: class TestName
} while (cont(response));
^
TestName.java:51: cannot find symbol
symbol : method startswith(java.lang.String)
location: class Name
boolean b = nameArray[i].startswith(first_);
import java.util.Scanner;
public class TestName {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("How many names do you wish to enter? ");
int number = input.nextInt();
Name[] nameArray = new Name[number];
//Fill array of people's names
for (int i = 0; i < nameArray.length; i++) {
System.out.print(i + ".");
System.out.println(" Please enter full name. (first middle last) or (first last): ");
nameArray[i] = new Name(input.nextLine());
}
//Change names
do {
System.out.println("Enter a person's first name. ");
String first_ = input.nextLine();
System.out.println("Which name would you like to change?");
String whichName = input.nextLine();
System.out.println("What would you like to change the name to? ");
String newName = input.nextLine();
changeName(nameArray, first_, whichName, newName);
System.out.println("Continue? Enter 'Exit' to leave or press enter to continue. ");
String response = input.nextLine();
} while (cont(response));
for(int i=0; i<nameArray.length; i++) {
nameArray[i].displayNames();
}
}
//Method for continuation condition
public static boolean cont(String response) {
String exit = "Exit";
if (response.equalsIgnoreCase(exit)) {
return false;
}
else {
return true;
}
}
//Method for finding which name to change
public static void changeName(Name[] nameArray, String first_, String whichName, String newName) {
int person;
//Search array of objects or object with first name
for(int i=0; i><nameArray.length; i++) {
boolean b = nameArray[i].startswith(first_);
if (b) {
person = i;
break;
}
}
//Change desired name
String first = "First";
String middle = "Middle";
String last = "Last";
if (whichName.equalsIgnoreCase(first)) {
nameArray[person].changeFirstName(newName);
}
else if (whichName.equalsIgnoreCase(middle)) {
nameArray[person].changeMiddleName(newName);
}
else if (whichName.equalsIgnoreCase(last)) {
nameArray[person].changeLastName(newName);
}
}
}
Explanation / Answer
/* * Complex.java * * Created on 30 April 2006, 17:37 */ public class Complex { private double imaginary; private double real; /** @ Setter method * */ public void Set(double x, double y) { real = x; imaginary = y; } public void print() { System.out.println(real+" + "+imaginary+"i"); } }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.