Need Help

Reply

Join Date: Apr 2006
Posts: 3
Reputation: LEGACYONE_2006 is an unknown quantity at this point 
Solved Threads: 0
LEGACYONE_2006 LEGACYONE_2006 is offline Offline
Newbie Poster

Need Help

 
0
  #1
Apr 29th, 2006
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.

  1.  
  2. import java.lang.Math;
  3. import java.util.Scanner;
  4.  
  5. class Arrays {
  6. public static void main (String [] args) {
  7. int A[]= {3, 1, 4, 5, 9, 2, 6, 8, 7, 0};
  8. int Result;
  9.  
  10. System.out.print("Please enter a number from 1 to 9");
  11.  
  12.  
  13. Scanner stdin = new Scanner (System.in);
  14. int x = stdin.nextInt();
  15.  
  16. Result = x;
  17.  
  18. if(Result == -1)
  19. System.out.println("\nThe number entered is not found in the array:\n");
  20.  
  21. else
  22. System.out.println("\n The index of the array element entered is: "
  23. + Result);
  24. }
  25.  
  26. static int find (int A[], int N) {
  27. for(int index = 0; index < A.length; index++)
  28. if(A [index] == N )
  29. return index;
  30.  
  31. return -1;
  32. }
  33. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Need Help

 
0
  #2
Apr 29th, 2006
  1. import java.lang.Math;
  2. import java.util.Scanner;
  3.  
  4.  
  5. class Arrays {
  6.  
  7. public static void main (String [] args) {
  8. int A[]= {3, 1, 4, 5, 9, 2, 6, 8, 7, 0};
  9. int Result;
  10.  
  11. System.out.print("Please enter a number from 1 to 9");
  12.  
  13.  
  14. Scanner stdin = new Scanner (System.in);
  15. int x = stdin.nextInt();
  16.  
  17. Result = x;
  18.  
  19. if(find(A,Result)!=-1)
  20. {
  21. System.out.println("Was found in this position");
  22. System.out.print(find(A,Result));
  23.  
  24. }
  25. else
  26. System.out.println("Number not in array");
  27.  
  28. }
  29.  
  30. static int find (int A[], int N) {
  31. for(int index = 0; index < A.length; index++)
  32. if(A [index] == N )
  33. {
  34. return index;
  35. }
  36.  
  37. return -1;
  38. }
  39. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 3
Reputation: LEGACYONE_2006 is an unknown quantity at this point 
Solved Threads: 0
LEGACYONE_2006 LEGACYONE_2006 is offline Offline
Newbie Poster

Re: Need Help

 
0
  #3
Apr 29th, 2006
Thank you. I see where my code was wrong
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC