Need help with arrays and for loops
I am having a problem getting "The key is not found."I added notes where i think the problem is located.Thanks
Here is my code:
package lab10;
public class SortArray {
public static void main(String[] args) {
double key = 0.0;
double[] searchablearray = {
1.3, 4.6, 2.9, 5.6, 6.8, 7.2, 7.8, 9.6, 4.3, 2.2};
char[] letterarray = {
'a', 'c', 'g', 'p', 'j', 'r', 'x', 'd', 'z', 'n'};
System.out.println("The array to be searched is: ");
for (int i = 0; i < searchablearray.length; i++) {
System.out.print(searchablearray + ", ");
}
System.out.println();
linearsearch(searchablearray, key);
System.out.println();
System.out.println("The original array is: ");
for (int j = 0; j < letterarray.length; j++) {
System.out.print(letterarray[j] + ", ");
}
System.out.println();
System.out.println("The array in descending order is: ");
selectionsort(letterarray);
}
public static void linearsearch(double[] array, double key) {
for (int i = 0; i < array.length; i++) {
if (array == key) {
System.out.println("The key was found at " + i + ".");
}
/*I think this is where the code should be for the print statement.
I am trying to get it to print "key not found". If the key is not
in the array, it should print "key not found" only once. I can get it
to work, but it prints 9 times.
*/
}
}
public static void selectionsort(char[] array) {
for (int j = array.length - 1; j >= 0; j--) {
int max = (int) array[0];
int maxindex = 0;
for (int k = 1; k <= j; k++) {
if (max < array[k]) {
max = array[k];
maxindex = k;
}
}
if (maxindex != j) {
array[maxindex] = array[j];
array[j] = (char) max;
}
System.out.print(array[j] + ", ");
}
}
}
please helllpp...!!!!!!
use code tags
package lab10;
public class SortArray {
public static void main(String[] args) {
double key = 0.0;
double[] searchablearray = {
1.3, 4.6, 2.9, 5.6, 6.8, 7.2, 7.8, 9.6, 4.3, 2.2};
char[] letterarray = {
'a', 'c', 'g', 'p', 'j', 'r', 'x', 'd', 'z', 'n'};
System.out.println("The array to be searched is: ");
for (int i = 0; i < searchablearray.length; i++) {
System.out.print(searchablearray + ", ");
}
System.out.println();
linearsearch(searchablearray, key);
System.out.println();
System.out.println("The original array is: ");
for (int j = 0; j < letterarray.length; j++) {
System.out.print(letterarray[j] + ", ");
}
System.out.println();
System.out.println("The array in descending order is: ");
selectionsort(letterarray);
}
public static void linearsearch(double[] array, double key) {
for (int i = 0; i < array.length; i++) {
if (array == key) {
System.out.println("The key was found at " + i + ".");
}
/*I think this is where the code should be for the print statement.
I am trying to get it to print "key not found". If the key is not
in the array, it should print "key not found" only once. I can get it
to work, but it prints 9 times.
*/
}
}
public static void selectionsort(char[] array) {
for (int j = array.length - 1; j >= 0; j--) {
int max = (int) array[0];
int maxindex = 0;
for (int k = 1; k <= j; k++) {
if (max < array[k]) {
max = array[k];
maxindex = k;
}
}
if (maxindex != j) {
array[maxindex] = array[j];
array[j] = (char) max;
}
System.out.print(array[j] + ", ");
}
}
}
I don't believe you are testing any spots inside of the array.
So inside of the loop, you need to test each position of the array and see if it matches the "key"
for(int i = 0; i < array.length; i++)
if( array[i] == key)
System.out.println("Key was found at " + i);
When you post code, please use the [code][/code] tags, otherwise [i] turns the code italic and even more illegible.
public static void linearsearch(double[] array, double key) {
for (int i = 0; i < array.length; i++) {
if (array[i] == key) {
System.out.println("The key was found at " + i + ".");
// you've found the answer, you can now return
}
}
// once you reach the end of the loop without returning, you know the key wasn't found, so print "not found"
}
When the key is found, you exit the method, so "not found" can't be printed. If you don't find it, the loop ends, and "not found" is printed.
Message was edited by:
hunter9000
I am testing it if (array == key){do this}
Then it prints "key not found" no matter what every time. I only need it to print if the key is not found.
When you post code, please use the tags, otherwise turns the code italic and even more illegible.
public static void linearsearch(double[] array, double key) {
for (int i = 0; i < array.length; i++) {
if (array == key) {
System.out.println("The key was found at " + i + ".");
// you've found the answer, you can now return
}
}
// once you reach the end of the loop without returning, you know the key wasn't found, so print "not found"
}
When the key is found, you exit the method, so "not found" can't be printed. If you don't find it, the loop ends, and "not found" is printed.
Message was edited by:
hunter9000
^^^^^^^^
REPLY:
This is a void method. In void methods you don't return anything.
Did you change your code to what I said in a previous post? You're not even looking inside of the array. The compiler thinks you are just using some variable named "array"
You have to use array[some_number_here]
public static void linearsearch(double[] array, double key) {
boolean isFound = false;
for(int i = 0; i < array.length; i++) {
if( array[i] == key) {
System.out.println("Key was found at " + i);
isFound = true;
}
}
if(!isFound)
System.out.println("Number not found");
}
Message was edited by:
lethalwire
> This is a void method. In void methods you don't return anything.You can still force the method to return.if(something) {return;}