I have a predefined string array of size 10 that I need to create seperate metho
ID: 3537452 • Letter: I
Question
I have a predefined string array of size 10 that I need to create seperate methods for.
One of the methods should search for a person and another should be able to remove a person and another will check to see if there is an open spot to insert another string/person. if so add it, and if not notify the user.
here is what I have so far on it...Can you please give me any ideas of what I should do? I don't want to completely change my code because I feel like I am pretty close. I am pretty certain that my main program is okay just stuck on the methods.
private void searchforCandidate(String namedCandidate[]) {
Scanner input = new Scanner(System.in);
System.out.println("What would you like to search for");
String search=input.nextLine();
for (int i=0;i<namedCandidate.length;i++){
if (search==namedCandidate[i])
{System.out.println("your candidate is" +namedCandidate[]");
break;}
}}
private void deleteCandidate(String namedCandidate[ ]) {
Scanner input = new Scanner(System.in);
System.out.println("What candidate would you like to remove");
for (int i=input.nextInt();i<namedCandidate.length-1;i++)
{
namedCandidate[i]=namedCandidate[i+1];}
}
private void insertName(String namedCandidate[]) {
Scanner input = new Scanner(System.in);
for(int i=0;i<namedCandidate.length;i++)
{
System.out.println(" ");
System.out.println("What name would you like to add");
namedCandidate[i]=input.nextLine();}}
Explanation / Answer
import java.io.*;
import java.util.*;
public class Mukesh
{
static String A[]=new String[10];
private static void searchforCandidate(String namedCandidate[])
{
Scanner input = new Scanner(System.in);
System.out.println("What would you like to search for");
String search=input.nextLine();
int found=-1;
for (int i=0;i<namedCandidate.length;i++)
{
if (search.equals(namedCandidate[i]))
{
System.out.println("your candidate is " +namedCandidate[i]);
found=1;
break;
}
}
if(found==-1)
{
System.out.println(search+" doesnot exist in list.");
}
}
private static void deleteCandidate(String namedCandidate[ ])
{
Scanner input = new Scanner(System.in);
System.out.println("What candidate would you like to remove");
String search= input.next();
int i=0;
for ( i=0;i<namedCandidate.length;i++)
{
if(search.equals(namedCandidate[i]))
{
System.out.println(search+" has been successfully eliminated.");
if(i!=namedCandidate.length-1)
{
namedCandidate[i]=namedCandidate[i+1];
break;
}
else
{
namedCandidate[i]="";
}
}
}
for(int j=i+1;j<namedCandidate.length;j++)
{
namedCandidate[i]=namedCandidate[i+1];
}
}
private static void insertName(String namedCandidate[])
{
Scanner input = new Scanner(System.in);
for(int i=0;i<namedCandidate.length;i++)
{
System.out.println("What name would you like to add");
namedCandidate[i]=input.nextLine();
}
}
public static void main(String[] args)
{
insertName(A);
deleteCandidate(A);
searchforCandidate(A);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.