traversing through arrays  
Author Message
Bonux





PostPosted: 2004-7-17 15:39:00 Top

java-programmer, traversing through arrays Suppose I've got the following code


public class EmployeeData
{
public static void main(String [] args)
{
String [] [] employeeInfo = { { "Alex" ,"Joe" , "James" , "Tom"}
,
{" Manager" , "Clerk" , "Analyst"
,"Manager"} };

double[] salary = { 2000 ,540 , 6321 ,1999.99};



// how do I traverse through that multidimensional array to display the
employee name and title and those who earn more than 2000

// I was trying to use a for loop ( int i = 0 ; i< employeeInfo.length ;
++i)
then use the if statement but it doesnot work ,

Anyone can help me on this problem

thanks
boen


 
Ryan Stewart





PostPosted: 2004-7-17 20:32:00 Top

java-programmer >> traversing through arrays "Bonux" <email***@***.com> wrote in message
news:cdbh72$2cj$email***@***.com...
> Suppose I've got the following code
>
> public class EmployeeData
> {
> public static void main(String [] args)
> {
> String [] [] employeeInfo = { { "Alex" ,"Joe" , "James" , "Tom"}
> ,
> {" Manager" , "Clerk" , "Analyst"
> ,"Manager"} };
>
> double[] salary = { 2000 ,540 , 6321 ,1999.99};
>
>
>
> // how do I traverse through that multidimensional array to display the
> employee name and title and those who earn more than 2000
>
> // I was trying to use a for loop ( int i = 0 ; i< employeeInfo.length ;
> ++i)
> then use the if statement but it doesnot work ,
>
> Anyone can help me on this problem
>
You've lost part of your post there. Why would you need an if?
for (int i = 0; i < employeeInfo[0].length; i++) {
System.out.print("Name: " + employeeInfo[0][i]);
System.out.print(" Title: " + employeeInfo[1][i]);
System.out.println(" Salary: " + salary[i]);
}

This code assumes a few things, and is very inflexible and bug-prone. You'd
be *much* better off using an Employee object:
public class Employee {
protected String name;
protected String title;
protected double salary;

// Appropriate constructors and methods
}


 
Bonux





PostPosted: 2004-7-17 21:12:00 Top

java-programmer >> traversing through arrays yes this part you told me is Ok , But How I display the employees name and
title who earn more than 2000 , ?

"Ryan Stewart" <email***@***.com> wrote in message
news:email***@***.com...
> "Bonux" <email***@***.com> wrote in message
> news:cdbh72$2cj$email***@***.com...
> > Suppose I've got the following code
> >
> > public class EmployeeData
> > {
> > public static void main(String [] args)
> > {
> > String [] [] employeeInfo = { { "Alex" ,"Joe" , "James" , "Tom"}
> > ,
> > {" Manager" , "Clerk" , "Analyst"
> > ,"Manager"} };
> >
> > double[] salary = { 2000 ,540 , 6321 ,1999.99};
> >
> >
> >
> > // how do I traverse through that multidimensional array to display the
> > employee name and title and those who earn more than 2000
> >
> > // I was trying to use a for loop ( int i = 0 ; i< employeeInfo.length ;
> > ++i)
> > then use the if statement but it doesnot work ,
> >
> > Anyone can help me on this problem
> >
> You've lost part of your post there. Why would you need an if?
> for (int i = 0; i < employeeInfo[0].length; i++) {
> System.out.print("Name: " + employeeInfo[0][i]);
> System.out.print(" Title: " + employeeInfo[1][i]);
> System.out.println(" Salary: " + salary[i]);
> }
>
> This code assumes a few things, and is very inflexible and bug-prone.
You'd
> be *much* better off using an Employee object:
> public class Employee {
> protected String name;
> protected String title;
> protected double salary;
>
> // Appropriate constructors and methods
> }
>
>


 
 
Woebegone





PostPosted: 2004-7-17 23:29:00 Top

java-programmer >> traversing through arrays "Bonux" <email***@***.com> wrote in message
news:cdc4me$7pc$email***@***.com...
> yes this part you told me is Ok , But How I display the employees name and
> title who earn more than 2000 , ?
>

Look closely at the indexes in Ryan's example. I suspect you're treating
your 2 * 4 table as though it was 4 * 2. You should be able to figure out
the correct "if" statement by reviewing the loop statement he provided.

--
Sean.


 
 
Bonux





PostPosted: 2004-7-18 0:10:00 Top

java-programmer >> traversing through arrays Unfortunately after having tried the code various times, Im unable to solve
it , I was thinking if I have to print the employees name who earn more
than 2000 , I have to use a for loop , eg for salary > 2000 , but how to do
it?
"Woebegone" <email***@***.com> wrote in message
news:eBbKc.45886$email***@***.com...
> "Bonux" <email***@***.com> wrote in message
> news:cdc4me$7pc$email***@***.com...
> > yes this part you told me is Ok , But How I display the employees name
and
> > title who earn more than 2000 , ?
> >
>
> Look closely at the indexes in Ryan's example. I suspect you're treating
> your 2 * 4 table as though it was 4 * 2. You should be able to figure out
> the correct "if" statement by reviewing the loop statement he provided.
>
> --
> Sean.
>
>


 
 
Roedy Green





PostPosted: 2004-7-18 0:22:00 Top

java-programmer >> traversing through arrays On Sat, 17 Jul 2004 11:39:16 +0400, "Bonux" <email***@***.com>
wrote or quoted :

>// I was trying to use a for loop ( int i = 0 ; i< employeeInfo.length ;

your problem is employeeInfo.length is 2. Try using the length of the
salary array or hard coding in the number to start.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
 
Ryan Stewart





PostPosted: 2004-7-18 0:30:00 Top

java-programmer >> traversing through arrays "Bonux" <email***@***.com> wrote in message
news:cdcf4q$afh$email***@***.com...
*fixed top post*
> "Woebegone" <email***@***.com> wrote in message
> news:eBbKc.45886$email***@***.com...
> > "Bonux" <email***@***.com> wrote in message
> > news:cdc4me$7pc$email***@***.com...
> > > yes this part you told me is Ok , But How I display the employees name
> and
> > > title who earn more than 2000 , ?
> > >
> >
> > Look closely at the indexes in Ryan's example. I suspect you're treating
> > your 2 * 4 table as though it was 4 * 2. You should be able to figure
out
> > the correct "if" statement by reviewing the loop statement he provided.
> >
> Unfortunately after having tried the code various times, Im unable to
solve
> it , I was thinking if I have to print the employees name who earn more
> than 2000 , I have to use a for loop , eg for salary > 2000 , but how to
do
> it?
>
Please don't top post. You seem to lack even a basic understanding of
programming. Try here for a basic Java tutorial:
http://java.sun.com/docs/books/tutorial/


 
 
Tony Morris





PostPosted: 2004-7-18 10:09:00 Top

java-programmer >> traversing through arrays "Bonux" <email***@***.com> wrote in message
news:cdbh72$2cj$email***@***.com...
> Suppose I've got the following code
>
>
> public class EmployeeData
> {
> public static void main(String [] args)
> {
> String [] [] employeeInfo = { { "Alex" ,"Joe" , "James" , "Tom"}
> ,
> {" Manager" , "Clerk" , "Analyst"
> ,"Manager"} };
>
> double[] salary = { 2000 ,540 , 6321 ,1999.99};
>
>
>
> // how do I traverse through that multidimensional array to display the
> employee name and title and those who earn more than 2000
>
> // I was trying to use a for loop ( int i = 0 ; i< employeeInfo.length ;
> ++i)
> then use the if statement but it doesnot work ,
>
> Anyone can help me on this problem
>
> thanks
> boen
>

It's a common fallacy among newcomers and "non-newcomers" that Java supports
multi-dimensional arrays. Some argue incorrectly that this fact is purely
academic, but the obstacle ithat is currently preventing you from solving
your problem is believing this common myth (meaning it's also practical). I
strongly suggest you disbelieve it for a moment so that you can gain a
correct understanding.

Examples of languages that supports true multi-dimensional arrays are COBOL
and C# (which also supports arrays in the same way as Java, but makes an
explicit distinction between them).

http://www.xdweb.net/~dibblego/java/faq/answers.html#q45
http://java.sun.com/docs/books/tutorial/java/data/arrays.html

Good luck.

--
Tony Morris
http://xdweb.net/~dibblego/