parallel arrays in java

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 5770 Views - Last Post: 20 December 2011 - 03:10 PM Rate Topic: -----

#1 mynameisian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-December 11

parallel arrays in java

Posted 19 December 2011 - 05:46 PM

Hello,

I'm having serious trouble with this program.

I am supposed to create an application that allows the user to enter in a customer's account number, and should look-up a name and balance based on the account number entered in by the user. I have been given 3 lists of data (account numbers, names and balances) meant to be implemented into arrays.

Each array has 20 elements.

Here is what I have.


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package accountsearch;
import java.util.Scanner;
/**
 *
 * @author Ian
 */
public class AccountSearch {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
        String [] name = new String[20];
        double [] balance = new double[20];
        int [] account = new int[20];
        String tmp5pr;
        
        System.out.println("Hello, please enter an account number.");
        Scanner scanner = new Scanner (System.in);
        int i = scanner.nextInt();
        
        
        
        //declare array elements for array "name".
        
    name[0] = "Abula";
    name[1] = "Allen";
    name[2] = "Austin";
    name[3] = "Bailey";
    name[4] = "Balovich";
    name[5] = "Bates";
    name[6] = "Bettencourt";
    name[7] = "Blackburn";
    name[8] = "Boucher";
    name[9] = "Brown";
    name[10] = "Duncan";
    name[11] = "Estrada";
    name[12] = "Fernandez";
    name[13] = "Gallagher";
    name[14] = "Gonzales";
    name[15] = "Ha";
    name[16] = "Hernandez";
    name[17] = "Howard";
    name[18] = "Johnston";
    name[19] = "Nguyen";
      
// declare array elements for "balance".

    balance[0] = 970.12;
    balance[1] = 268.01;
    balance[2] = 3821.94;
    balance[3] = 3193.97;
    balance[4] = 444.21;
    balance[5] = 4253.79;
    balance[6] = 2916.99;
    balance[7] = 906.31;
    balance[8] = 1291.99;
    balance[9] = 2001.18;
    balance[10] = 2963.20;
    balance[11] = 1152.60;
    balance[12] = 1113.05;
    balance[13] = 3703.46;
    balance[14] = 2667.88;
    balance[15] = 3042.25;
    balance[16] = 72.04;
    balance[17] = 2229.66;
    balance[18] = 3130.92;
    balance[19] = 3004.99;

//declare array elements for "account"

    account[0] = 7221379;
    account[1] = 6853921;
    account[2] = 4215534;
    account[3] = 5551298;
    account[4] = 6224649;
    account[5] = 6502286;
    account[6] = 8448936;
    account[7] = 2883903;
    account[8] = 6752376;
    account[9] = 3564698;
    account[10] = 6373150;
    account[11] = 6375372;
    account[12] = 8736368;
    account[13] = 7218536;
    account[14] = 6681985;
    account[15] = 3909086;
    account[16] = 5221192;
    account[17] = 5901163;
    account[18] = 2427821;
    account[19] = 6331655;

 
    
}
}




I have been told that simply using while statements could help me at this point. I just do not know where to turn, any direction would be greatly appreciated.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: parallel arrays in java

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: parallel arrays in java

Posted 19 December 2011 - 05:59 PM

Search the account array for the account number. If found, the user's name and balance are at the same array index in the other arrays.
Was This Post Helpful? 0
  • +
  • -

#3 Air Legend  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 101
  • Joined: 28-November 11

Re: parallel arrays in java

Posted 19 December 2011 - 06:02 PM

View PostGregBrannon, on 19 December 2011 - 05:59 PM, said:

Search the account array for the account number. If found, the user's name and balance are at the same array index in the other arrays.


To build off of this, I would recommend a for loop rather than a while loop. Something such as:

for (int i=0; i<array.length; i++){
if (account[i] == ?){
// you found the account!
}
}


Was This Post Helpful? 0
  • +
  • -

#4 mynameisian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-December 11

Re: parallel arrays in java

Posted 19 December 2011 - 06:06 PM

View PostGregBrannon, on 19 December 2011 - 05:59 PM, said:

Search the account array for the account number. If found, the user's name and balance are at the same array index in the other arrays.


Thank you for the reply. Where is it legal to put the counter to search the array?

Also, could you please describe how the counter, when searching the account array, also displays the subsequent name and balance?

Thanks again.
Was This Post Helpful? 0
  • +
  • -

#5 Air Legend  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 101
  • Joined: 28-November 11

Re: parallel arrays in java

Posted 19 December 2011 - 06:07 PM

To answer you question, try looking at this site: here
Was This Post Helpful? 0
  • +
  • -

#6 mynameisian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-December 11

Re: parallel arrays in java

Posted 19 December 2011 - 06:12 PM

View PostAir Legend, on 19 December 2011 - 06:02 PM, said:

View PostGregBrannon, on 19 December 2011 - 05:59 PM, said:

Search the account array for the account number. If found, the user's name and balance are at the same array index in the other arrays.


To build off of this, I would recommend a for loop rather than a while loop. Something such as:

for (int i=0; i<array.length; i++){
if (account[i] == ?){
// you found the account!
}
}



Thanks!

So from here, I would like to implement user interaction. Is this just a matter of using a Scanner/Keyboard dynamic within this 'for' loop?

Appreciate it!
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: parallel arrays in java

Posted 19 December 2011 - 06:14 PM

I'm not sure what you mean by "where is it legal?"

I suggest you write a method that returns the index of the account number, something like:

public int getAccountIndex( int accountNumber )
{
  int index = -1;

  for ( int i = 0 ; i < account.length ; i++ )
  {
    if ( account[i] == accountNumber )
    {
      index = i;
    }
  }
  return index;
}

Then use that index to print the corresponding values in the other arrays.
Was This Post Helpful? 0
  • +
  • -

#8 Air Legend  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 101
  • Joined: 28-November 11

Re: parallel arrays in java

Posted 19 December 2011 - 06:18 PM

Well, since you already received the account number above (unless you want the program to search for more than once account, in this case we would use a slightly different approach) you would keep your code as it is. I would recommend changing your variable holding the id from "i" to maybe "id" (just because it can get confusing later). To continue that code, something like this could accomplish what you were asking:

for (int i=0; i<array.length; i++){
	if (account[i] == id){
	  System.out.println(name[i]);
          System.out.println(balance[i]);
        }
}


View PostGregBrannon, on 19 December 2011 - 06:14 PM, said:

I'm not sure what you mean by "where is it legal?"

I suggest you write a method that returns the index of the account number, something like:

public int getAccountIndex( int accountNumber )
{
  int index = -1;

  for ( int i = 0 ; i < account.length ; i++ )
  {
    if ( account[i] == accountNumber )
    {
      index = i;
    }
  }
  return index;
}

Then use that index to print the corresponding values in the other arrays.


In other words, this could be condensed into what I wrote above. I think (considering that there are only two other numbers needing to be returned) it would be inefficient to write a separate method.

This post has been edited by Air Legend: 19 December 2011 - 06:16 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: parallel arrays in java

Posted 19 December 2011 - 06:41 PM

There are dozen of variants of that type of loop. That is what I would use
   int i = 0;
   for (; i < array.length; i++){
	if (account[i] == id)
           break;
   }
   if(i == array.length) {
     ... account was not found
     return;
   }

   // continue here with account[i]


Was This Post Helpful? 1
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10188
  • View blog
  • Posts: 37,629
  • Joined: 27-December 08

Re: parallel arrays in java

Posted 20 December 2011 - 07:47 AM

Please avoid using parallel arrays in favor of Object-Oriented Programming, which is the standards adherent practice. My tutorial Moving Away From Parallel Arrays covers this in more detail.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: parallel arrays in java

Posted 20 December 2011 - 08:03 AM

Mac, there is noting about parallel arrays in this thread bside it's title :)
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10188
  • View blog
  • Posts: 37,629
  • Joined: 27-December 08

Re: parallel arrays in java

Posted 20 December 2011 - 08:06 AM

Actually, parallel arrays are in the OP's code. Regardless of the assignment, it is still good to at least be aware of better practices. :)
Was This Post Helpful? 0
  • +
  • -

#13 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1049
  • View blog
  • Posts: 4,458
  • Joined: 09-June 09

Re: parallel arrays in java

Posted 20 December 2011 - 10:02 AM

One of the main features of Java is that it has a "simple" object model, this would be a great time to take advantage of that.
Was This Post Helpful? 0
  • +
  • -

#14 mynameisian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-December 11

Re: parallel arrays in java

Posted 20 December 2011 - 12:01 PM

Hi everyone, thanks so much for the feedback. I think I'm almost through with this.

I put in the for statement and tailored it to fit my code. The for statement below searches the "account" array based on user input.

  

int i = 0;
  for (; i < array.length; i++){
if (account[i] == id)
    System.out.println("Account holders name is" + name);
    System.out.println("Account holders balance is" + balance);
          break;
  }
  if (i == array.length) {
    System.out.println("... account was not found");
    return;
  }
// placed on top of the parallel arrays mentioned earlier in this thread



The user should enter in a valid account number present within the array. It is then supposed to return the name and balance of the user.

I'm not sure what I'm missing.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: parallel arrays in java

Posted 20 December 2011 - 12:35 PM

First in your print statements it will be name[i] and balance[i] not just name and balance as name and balance are array
Now we are back to macosxnerd101 point :) Your method cannot return and a name and a balance.
A name if it is a Strung method, a balance if it is a double method

So better to look at Mac's tutorial already mentionned and learn how tou build an Account object that will contain: name, balance, account nummber

Your method can then return that Account object
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2