Sunday, 15 May 2011

java - I am getting an "error: int cannot be dereferenced" for my program designed to sequentially, iteratively, and recursively search a .txt file -



java - I am getting an "error: int cannot be dereferenced" for my program designed to sequentially, iteratively, and recursively search a .txt file -

in main method, i've tried .equals() , .compareto() , both give me same error @ lines 88, 94, , 100. when utilize ==, compiles, gives me next when runs:

type word you're searching for. or type -1 stop: curse sequentialsearch() : curse not found (comparison=13040). iterative binarysearch(): curse not found (comparison=13). recursive binarysearch(): curse not found (comparison=13). type word you're searching for. or type -1 stop:

i know word "curse" in text file i'm searching.

this code have far.

import java.io.*; import java.util.scanner; import java.util.arraylist; public class search extends object { public static final string to_stop = "-1"; public static final int not_found = -1; public static int count1; public static int count2; public static int count3; public search() { count1 = 0; count2 = 0; count3 = 0; } public static int sequentialsearch(arraylist<string> array, string value) { int low = 0; int high = array.size() - 1; (int = low; <= high; i++){ count1++; if (array.get(i) == value) homecoming i; } homecoming not_found; } public static int binarysearch(arraylist<string> array, string value) { int low = 0; int high = array.size() - 1; while (low <= high) { int mid = (low + high)/2; if (array.get(mid) != value){ count2++; high = mid - 1; } else if (array.get(mid) != value){ count2++; low = mid + 1; } else homecoming mid; } homecoming not_found; } public static int binarysearch(arraylist<string> array, int low, int high, string value) { if (low > high) homecoming not_found; int mid = (low + high)/2; if (array.get(mid) != value){ count3++; homecoming binarysearch(array, low, mid-1, value); } else if (array.get(mid) != value){ count3++; homecoming binarysearch(array, mid+1, high, value); } else homecoming mid; } public static void main (string [] args) throws ioexception { arraylist<string> array = new arraylist<string>(); file fn = new file("sortedwords.txt"); scanner sc = new scanner(fn); scanner keyboard = new scanner(system.in); boolean wantstocontinue = true; while(sc.hasnextline()){ string ln = sc.nextline(); array.add(ln); } { system.out.print("type word you're searching for. or type " + to_stop + " stop: "); string word2search = keyboard.nextline(); if(word2search.equals(to_stop)){ wantstocontinue = false; } else { int index; index = sequentialsearch(array, word2search); if (index.compareto(not_found)) system.out.println("sequentialsearch() : " + word2search + " not found (comparison=" + count1 + ")."); else system.out.println("sequentialsearch() : " + word2search + " found in [" + index + "] (comparison=" + count1 + ")."); index = binarysearch(array, word2search); if (index.compareto(not_found)) system.out.println("iterative binarysearch(): " + word2search + " not found (comparison=" + count2 + ")."); else system.out.println("iterative binarysearch(): " + word2search + " found in [" + index + "] (comparison=" + count2 + ")."); index = binarysearch(array, 0, array.size()-1, word2search); if (index.compareto(not_found)) system.out.println("recursive binarysearch(): " + word2search + " not found (comparison=" + count3 + ")."); else system.out.println("recursive binarysearch(): " + word2search + " found in [" + index + "] (comparison=" + count3 + ")."); } } while (wantstocontinue); }

}

you cant utilize compareto() or equals on int since int primitive type.you have utilize '==' compare primitives. can invoke compareto() or equals() methods on integer object.

first problem reading input file line line , putting each line entry array.then trying compare entered word against entire line in file.if trying word search, seek splitting input file contents words.

another error using == compare string.you mustn't utilize == compare strings.use equals() method instead.

java

No comments:

Post a Comment