Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to Java Array question

Status
Not open for further replies.

comboy

Instructor
May 23, 2003
226
Hi all just started studying Java and need a little help on arrays.

I'm trying design a program that will allow a user to enter a year (an int variable) and return <=values from two external arrays based on the year.

my arrays are as follows



public static final String[] names = {
"Catherine", "Joe", "Betty", "Dave", "Pete", "Kate", "Sean", "Linda"};

public static final int[] birthyear = {
1976, 1960, 1980, 1979, 1974,
1961, 1940, 1976};


What I really need to know is that when the user enters a year e.g. 1980 how do I get Java to select the third index as a starting point as currently I'm getting out of bounds errors when I try to return the index position based on the year variable as its obviously past the array length of 7.

Thanks in advance
 
The only way I've ever been able to do this satisfactorily is with a hashtabel.

_________________
Bob Rashkin
 
Why not make a class that contains the year and name as public data members.

Use an array of this class. That way when you know the index to the year, you know the name.

Code:
public class YearName
{
  public int year;
  public String name;
}

Code:
public class SomeApplication
{
  YearName[] yearName = new YearName[20];
}
 
You mean something like, this?

for (int i = 0; i < birthyear.length; i++) {
if (yearEntered == birthyear) {
System.out.println(names + " was born in " + yearEntered);
break;
}
}

 
Yeh tom thats what I mean, but I need to use a while loop according to the course guidelines.

I'll try and use your suggestion and see what I can come up with.

Bong, Kodr thanks for the suggestions but thats way above my knowledge at the moment, but thanks for them.

Thanks again guys will get back to ye with how I got on.
 
Ok close to what I need but what I actually need is that is the user enter a year such as 1975 I need the system to print out all values that match the year and are less than the year
e.g. year entered = 1975
results

Pete[name] was born in 1974[birthyear]
Kate was born in 1960
Sean was born in 1940

Thanks again for the help so far
 
Hi All,

Ok I've got the results to print as I want them to using the following code

Code:
{
Scanner input = new Scanner (System.in);
		
int year = 0;
int indexyear = 0;

System.out.print("Enter a year: ");
year = input.nextInt();
		
  if (year <=0)
{System.out.println("The year must be greater than 0"); AnotherGo();}	
		
 else if ( year >= 1)
{pline();System.out.println("The following elements were discovered by " + year);pline();

while (indexyear < Element.years.length)
{if (Element.years[indexyear] <= year)
 {System.out.println(Element.names[indexyear]  +  Element.years[indexyear]);}	
indexyear++;
}
AnotherGo();
}

I was just wondering how I would be able to count the number of elements in the result so that I can print them out.

e.g. System.out.println("Thats " + numelements + " elements");

Thanks in advance.
 
just add a counter, and then increment it inside the while loop.

{
Scanner input = new Scanner (System.in);

int year = 0;
int indexyear = 0;
int numelements=0;

...

...
while (indexyear < Element.years.length)
{if (Element.years[indexyear] <= year)
{System.out.println(Element.names[indexyear] + Element.years[indexyear]);indexyear++;}
numelements++;
}
 
Hi Guys thanks for the help got things sorted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top