944,156 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1027
  • Java RSS
Apr 29th, 2006
0

Need Help

Expand Post »
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)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LEGACYONE_2006 is offline Offline
3 posts
since Apr 2006
Apr 29th, 2006
0

Re: Need Help

Java Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 29th, 2006
0

Re: Need Help

Thank you. I see where my code was wrong
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LEGACYONE_2006 is offline Offline
3 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Pacman
Next Thread in Java Forum Timeline: Client-server messenger





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC