Hello! I am doing an assignment for one of my classes and there seems to be something wrong with my code. It's supposed to allow the user to input a string and it will perform the linear search on the first array and binary search on the second array and return their positions in the array if the string is found.

Here's the code:

import java.util.Scanner;
import java.io.*;

public class Lab9b {

public static int binarySearch(String [] strArray, String strValue){
	int i,n,mid,low=0,high=strArray.length-1;
	mid = (low+high)/2;

	while(true){
		if(strValue == strArray[mid])
			return mid;
		if(strArray[mid].compareTo(strValue) < 0){
			low=mid;
		}else
			high = mid;
			n = mid;
			mid = (low+high)/2;
		if(n==mid)
		break;
	}

	return -1;
} // end binarySearch method


public static void printStringArray(String [] strArray){

	for(int i = 0; i < strArray.length; i++){
		if (strArray[i] != null) 
		System.out.println(strArray[i]); 
	}
	
} // end printStringArray method


public static int linearSearch(String [] strArray, String strValue){

	for(int n=0;n<strArray.length;n++){
		if(strArray[n]==strValue)
		return n;
	}
	
	return -1;

} // end linearSearch method


public static void main(String[] args) throws FileNotFoundException{

	// (A) Declare our 25 string array unsorted
	String[] s = new String[25];
        Scanner file = new Scanner(new File("unsorted.txt"));
	
        int loc = 0;
	   while(file.hasNextLine()){
      	   s[loc++] = file.nextLine();
           }      
		
      System.out.println("Number of lines in s read: " + loc);
	
 	// (B) Declare our 30 string array sorted 
	String[] j = new String[30];
        Scanner file2 = new Scanner(new File("sorted.txt"));
	
        int loca = 0;
	   while(file2.hasNextLine()){
      	   j[loca++] = file2.nextLine();
        }      
		
        System.out.println("Number of lines in j read: " + loca); 	
			
System.out.println();

// Now we will prompt the user to enter strings to check if they are in the arrays

	Scanner scan = new Scanner(System.in);
	String q;
	
	for (int x = 0; x < s.length; x++){
		System.out.println("Enter a String to see if it's in the arrays.");
		q = scan.nextLine();
		if (q.equals("quit"))
			System.exit(0);
		else
			System.out.println("Using linear search: " + linearSearch(s,q));
			System.out.println("Using binary search: " + binarySearch(j,q));
			
	}//end for


} // end main

} // end class

I think around line 75 of the code is where something is terribly wrong. Is there anything I can do to fix it???

My unsorted.txt and sorted.txt is just a list of random names.

For starters, use the .equals method to compare the equality of Strings and any other objects.
See what happens and I will look closely the rest of code later.

String s1 = "a";
String s2 = "b";

// examples

boolean b = s1.equals(s2);
b = s1.equals("ccc");
b = "abc".equals(s2);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.