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 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++){...
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☺️
this is my linkedlist and to insert a new element in it , however i want...
this is my linkedlist and to insert a new element in it , however i want it to be user entered linkedlist #include <bits/stdc++.h> using namespace std;    // A linked list Node struct Node {     int data;     struct Node* next; };    // Size of linked list int size = 0;    // function to create and return a Node Node* getNode(int data) {     // allocating space     Node* newNode = new Node();        // inserting the required data     newNode->data...
I wrote this code and it produces a typeError, so please can you fix it? import...
I wrote this code and it produces a typeError, so please can you fix it? import random def first_to_a_word(): print("###### First to a Word ######") print("Instructions:") print("You will take turns choosing letters one at a time until a word is formed.") print("After each letter is chosen you will have a chance to confirm whether or not a word has been formed.") print("When a word is formed, the player who played the last letter wins!") print("One of you has been chosen...
I want to draw a sphere and I want to rotate it, How can I do...
I want to draw a sphere and I want to rotate it, How can I do it in JOGL? ( I am working on a program to visualize the sun ) please provide a pic of the code.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT