Question

In: Computer Science

can you fix this problem plz: I want to return -1 if the element is not...

can you fix this problem plz:

I want to return -1 if the element is not found. fix for me just that.

thank you.

public class Search{
  
  
  
   public static void main(String[] args) {
      
      
      
      
      
       Character [] anArray = {'a','b','c','d'};
      
       Character st = 'a';
      
      
       iterativeSearch(anArray, st);
      
   //System.out.println(iterativeSearch(anArray, st));
   }

   public static <T> boolean iterativeSearch(T[] anArray, T number) {
      
      
       boolean found = false;
      
       int index = 0;
      
      
       while (!found && (index < anArray.length)) {
          
           if(number.equals(anArray[index])) {
              
               found = true;
              
               System.out.println("Element found at index: " + index);
           }
          
          
          
           index++;
          
          
          
       }
      
   return found;
      
      
      
   }

}

Solutions

Expert Solution

HIGHLIGHTED lines are modified

public class Search{
public static void main(String[] args) {
Character [] anArray = {'a','b','c','d'};
System.out.println(iterativeSearch(anArray, 'd'));
System.out.println(iterativeSearch(anArray, 'e'));
}

public static <T> int iterativeSearch(T[] anArray, T number) {
boolean found = false;
int index = 0;
  
while (!found && (index < anArray.length)) {
if(number.equals(anArray[index])) {
found = true;
System.out.println("Element found at index: " + index);
break;
}
index++;
}

if(found == false)
return -1;
return index;
}

}

// Hit the thumbs up if you are fine with the answer. Happy Learning!


Related Solutions

Can you fix to me this code plz I just want to print this method as...
Can you fix to me this code plz I just want to print this method as the reverse. the problem is not printing reverse. public void printBackward() {               Node curr = head;        Node prev = null;        Node next = null;               System.out.print("\nthe backward of the linkedlist is: ");               while(curr != null) {            next = curr.next;            curr.next = prev;   ...
I want to know how can I fix this program so it can work properly....can someone...
I want to know how can I fix this program so it can work properly....can someone run it and give me the fixed version but do not the entire program bear with this one please and thanks in advance!!!!! import java.util.Scanner; public class ifPractice { public static void main(String[] args) { Scanner KB = new Scanner(System.in); { double option; double option02; double stu_gradeTest01; double stu_gradeTest02; double stu_gradeTest03; double final_grade; { System.out.println("Enter #1 to see what test to grade first, #2...
I need to make 2 possible suggestions on how Comcast can fix the problem with their...
I need to make 2 possible suggestions on how Comcast can fix the problem with their customer service, and how Comcast can possibly improve the whole business overall while briefly discussing the impacts of these suggestions. Please put in a paragraph with the heading above, I Must come up with 350 words in total.
I already have the code of this program, I just want to know how to fix...
I already have the code of this program, I just want to know how to fix the code to Implement the 5th function (System.nanotime() and System.currentTimeMillis() methods) What Code does: Introduction Searching is a fundamental operation of computer applications and can be performed using either the inefficient linear search algorithm with a time complexity of O (n) or by using the more efficient binary search algorithm with a time complexity of O (log n). Task Requirements In this lab, you...
For this first Pause-Problem, I want you to design three (brief!) studies (they can all be...
For this first Pause-Problem, I want you to design three (brief!) studies (they can all be variations on the same idea). Make sure to note the independent and dependent variables for all 1). One should use an independent two group design 2). One should use a matched OR natural pair design 3). One should use a repeated measures design
For this Pause-Problem, I want you to design three (brief!) studies (they can all be variations...
For this Pause-Problem, I want you to design three (brief!) studies (they can all be variations on the same idea). ***Make sure to note the independent and dependent variables for all 1). One should use an independent three group design – 2). One should use a matched OR natural set design – 3). One should use a repeated measures design
I need to fix this code, and could you please tell me what was the problem...
I need to fix this code, and could you please tell me what was the problem options 1 and 9 don't work #include <stdio.h> #include <time.h> #include <stdlib.h> // generate a random integer between lower and upper values int GenerateRandomInt(int lower, int upper){     int num =(rand()% (upper - lower+1))+lower;     return num; } // use random numbers to set the values of the matrix void InitializeMatrix(int row, int column, int dimension, int mat[][dimension]){     for(int i =0; i<row; i++){...
Can you solve and send me the original code which I can copy and paste plz....
Can you solve and send me the original code which I can copy and paste plz. Thank you. Kahn's Algorithm Implement Kahn's Algorithm for giving a topological ordering of a graph as discussed in class. The input graph will be in adjacency list format. 1. Count the in-degree (number of edges ending at) each vertex. 2. Create a queue of nodes with in-degree 0. 3. While the queue is not empty: a. Add the first element in the queue to...
In python I have my code written and I want just 4 functions to be fix...
In python I have my code written and I want just 4 functions to be fix in my code according to rule. My code is running but it has some problem regarding to rules. (I have did all the other things so you do not have to worry about other functions) 1) all the players has to play until he/she reaches to at least 500 points in first round. When user reach 500 points for the first time, user may...
i want good answer with example plz i have a project useing java language but theacher...
i want good answer with example plz i have a project useing java language but theacher tell us u must use (java.util.Date) only to get the current year and month how can i do that plz jst using that class no celender or somthing else i need current year and current month to calculate the gurantee year and the guranttee month thax☺️
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT