RSS Forums RSS
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 719 | Replies: 2
Reply
Join Date: Apr 2006
Posts: 3
Reputation: LEGACYONE_2006 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
LEGACYONE_2006 LEGACYONE_2006 is offline Offline
Newbie Poster

Help Need a little help

  #1  
Apr 16th, 2006
I have the following program working. It takes an array of numbers and returns the index value of each number. However, what I want it to do is to get an integer from input from a user and return the index value of just that integer. If the integer is not in the array I want it to return -1.

I know I can get the user to enter input by using :

x = stdin.nextInt();

Scanner stdin = new Scanner (System.in);

int x = stdin.nextInt();

I don't know how to incorporate it to work in the following code:

import java.util.Scanner;
import java.lang.Math;

class Arrays {
   public static void main (String [] args){
	
	  int A []= {56, 418, 93, 11, 444};
	  
	  }
	  
	  
static int find (int [] A, int N){

    for (int index = 0; index <A.length; index++){
         return index;
}
return -1;
}
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2004
Posts: 733
Reputation: Phaelax is on a distinguished road 
Rep Power: 6
Solved Threads: 31
Phaelax Phaelax is offline Offline
Master Poster

Re: Need a little help

  #2  
Apr 17th, 2006
I suggest you go read up on programming basics. You know how to retrieve an element from an array?

int x = myArray[index];

To make sure the index value is within bounds of the array, make sure the index is between 0 and array.length-1, inclusive.
Reply With Quote  
Join Date: Mar 2006
Posts: 20
Reputation: SeekAnswers is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
SeekAnswers SeekAnswers is offline Offline
Newbie Poster

Re: Need a little help

  #3  
Apr 17th, 2006
It's simple, try this and please remember to indent your codes so that it makes your program much more readable and it's a good habit...(",)

Oh, don't forget the comments...


import java.lang.Math;

class Arrays {
public static void main (String [] args) {
int A[]= {56, 418, 93, 11, 444};
int Result;

Scanner stdin = new Scanner (System.in);
int x = stdin.nextInt();

Result = find(x);

if(Result == -1)
System.out.println("\nThe number entered is not found in the
array.\n");
else
System.out.println("\nThe index of the array element entered is: "
+ Result);
}

static int find (int A[], int N) {
for(int index = 0; index < A.length; index++)
if(N == A[index])
return index;

return -1;
} // Ends the For-Loop.
} // Ends the "find" method.
} // Ends the "Main" method.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:16 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC