Scott M Summers

Ranch Hand
+ Follow
since Sep 08, 2015
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Scott M Summers

I am having trouble with the Binary search for a string array. When my code calls binarySearch (line #43), I get an error:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
at SearchSort.binarySearch(SearchSort.java:75)
at SearchSort.main(SearchSort.java:50)
Java Result: 1

Which I believe means that I need a return statement at the end of my while loop. But in the examples in my text book, and all the examples of binary search I've found online, there is not a return statement at the end of the while loop.

My Binary Search Code:


My Entire Code:


Thanks for your time.
9 years ago
totalComparisons = (totalComparisons + comparisons + 1);

This is the answer I came up with to my problem, and it seems to work.
9 years ago
In my project I have a string array filled with names. My main method asks the user to type in a name to search for. If the name is in the array it will say, "the name is found in the array." and how many comparisons it took to find that name within the array. Then vice versa if the name is not found. I need to also print out the average number of comparisons once the program ends, this being the sum of the comparisons divided by the number of searches.
I have a variable that keeps track of the number of searches, int numberOfSearches, and I have a variable, int comparisons, that is the comparisons for one search. Is there a way to have a variable, int totalComparisons, that is the sum of my comparisons?

9 years ago

Mike. J. Thompson wrote:
Take a look at the lines in bold. If you call the linearSearch method twice it will run twice...



That took care of the double printing issue. I didn't realize that "i = SearchSort.linearSearch(0, nameToSearch);" would call the linearSearch method.
Thank you.
9 years ago
Okay, using !stopSearch, that is running my loop fine. Still getting the double print out though.

9 years ago

Paweł Baczyński wrote:Never write while(condition == true) or while (condition == false).

This is very error prone. You could easily write an assignment instead. Well... You did.

Always use while(condition) or while(!condition).



I changed my while condition to !=true and that made the loop work. But it prints line 43 or 48 twice (depending on the result) when it should only print once.

Here is my output when I search for buzz:

Enter a name to search for(type exit or quit to stop the program):
buzz
Searching for buzz...

buzz is found in the array.
buzz is found in the array.
Number of comparisons to find buzz: 61
Enter a name to search for(type exit or quit to stop the program):


It looks good except for "buzz is found in the array." printing twice. I don't see anything in my code that would make it print twice, but its there.

9 years ago
I need a loop that requests the user to enter a name to search an array. The only reason for the loop to stop is if the user enters "quit" or "exit".
Below is the code I have, I am trying a do while loop, but it always stops the search after one attempt. Additionally, it prints line 43 or 48 twice (depending on the result) when it should only print once.



Thank you for your time.
9 years ago

Carey Brown wrote:
Yes. Put it between lines 1 and 2.



Thanks for your help.
9 years ago

Carey Brown wrote:
Sorry, my suggestion only works if you declare it just inside your class declaration, and not inside a method as you have it.

You should not compare strings using ==, use equals() instead.

The == only compares the references and not the value stored at the references. Two different string containing "joker" will not have the same reference.



Thank you, that worked. When you say "my suggestion only works if you declare it just inside your class declaration" is the class declaration in between lines 1 & 2 of the code below?
9 years ago

Carey Brown wrote:You are searching 'args', not 'names'.

You should be declaring names as

This is assuming that NAMES will not be changing. This is how to declare constants, and constant names should be in all upper case.



I tried what you said, but changing my array to, private static final String[] NAMES = {..names here..} gave me an error (illegal start of expression). I also tried to put just the name of the array, names, in all caps, NAMES, but it still would not take the user input.
9 years ago
I have an array of names. I am attempting to search the array for specific names from what the user types in. My search code works when the name being search is not from user input, but once I set the parameters in my linearSearch method to take user input it will not find the String being search for.

9 years ago
got it, thanks.
9 years ago
I am attempting to write a method that returns true if A2 is greater than A1. Below is what I have, but I receive an error with my if statement.
9 years ago

Jason Bullers wrote:You can't access it because the system_task_objects is only in scope in your WorkOrders() method. As Campbell pointed out earlier, I think you intended that method to be a constructor. You should read up on how to declare those and how write a class correctly. That should help solve your current problem.

Also, as a side note: you should use camelCase for naming your variables (so systemTaskObjects and not system_task_objects).


Campbell Ritchie wrote:The type and location of that array keep changing.



Thanks for your help. I will study up on constructors.
9 years ago
I've declared my array within WorkOrders(), and I'll pass the array as arguments.





But I'm having a problem calling the method with the arguments

I've tried to call the method with (String[]system_task_objects) & (system_task_objects) neither work.
9 years ago