| | |
Need Help
![]() |
•
•
Join Date: Apr 2006
Posts: 3
Reputation:
Solved Threads: 0
I am trying to get this program to return the index value of the number that is input by the user. It is currently returning whatever number the user inputs.
Java Syntax (Toggle Plain Text)
import java.lang.Math; import java.util.Scanner; class Arrays { public static void main (String [] args) { int A[]= {3, 1, 4, 5, 9, 2, 6, 8, 7, 0}; int Result; System.out.print("Please enter a number from 1 to 9"); Scanner stdin = new Scanner (System.in); int x = stdin.nextInt(); Result = x; if(Result == -1) System.out.println("\nThe number entered is not found in the array:\n"); else System.out.println("\n The index of the array element entered is: " + Result); } static int find (int A[], int N) { for(int index = 0; index < A.length; index++) if(A [index] == N ) return index; return -1; } }
Java Syntax (Toggle Plain Text)
import java.lang.Math; import java.util.Scanner; class Arrays { public static void main (String [] args) { int A[]= {3, 1, 4, 5, 9, 2, 6, 8, 7, 0}; int Result; System.out.print("Please enter a number from 1 to 9"); Scanner stdin = new Scanner (System.in); int x = stdin.nextInt(); Result = x; if(find(A,Result)!=-1) { System.out.println("Was found in this position"); System.out.print(find(A,Result)); } else System.out.println("Number not in array"); } static int find (int A[], int N) { for(int index = 0; index < A.length; index++) if(A [index] == N ) { return index; } return -1; } }
![]() |
Other Threads in the Java Forum
- Previous Thread: Pacman
- Next Thread: Client-server messenger
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class client code compile compiler component database development digit eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying pearl problem program programming project qt recursion scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver string superclass swing system text-file thread threads tree variablebinding windows xor






