Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

java arrays

Posted on 2003-02-24
9
Medium Priority
?
206 Views
Last Modified: 2010-03-31
Hello have what is probably a small problem, I have a java array class with this code in it:-

  int NUMBER_OF_YEARS = 5;
    String yearsWorked[] = {"First Year", "Five Years", "10 Years", "Fifteen Years", "Twenty Years"};
    double[] total_Wages = new double[NUMBER_OF_YEARS];
   
    /**
     * arrays class fills the array with the users input
     */
    public void Arrays() throws IOException
    {
        double sum = 0;
        for (int i = 1; (i <=NUMBER_OF_YEARS); i++)
        {
            System.out.println("Enter amount you expect to earn after "+yearsWorked[i-1]);
           
            //create an instance of the readline class
            UserInput wages_Earned = new UserInput();
            double wage =  wages_Earned.readDouble();
           
            sum+= wage;
            total_Wages[i-1] = wage;

what I want to do is refrence a specific location from another class I have tried using:-
arrayInput = arrayInput.total_Wages[4];
but this does not work any ideas much apreciated thanks.
0
Comment
Question by:Bennett
  • 5
  • 3
9 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 8009924
what's the error?

I take it arrayInput is an instance of the Class which contains the above method?
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 800 total points
ID: 8009926
you could make total_Wages data member public:

ie: public double[] total_Wages = ...


but the best practice is to create a getter:

public double[] getTotalWages() { return total_Wages; }


then you can do (assuming arrayInput is your class):

double[] totWages = arrayInput.getTotalWages();
myVal = totWages[4];
0
 

Author Comment

by:Bennett
ID: 8010017
yes arrayInput is an instance of the class containing the method.
the error I get is
found double required Arrays
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:bobbit31
ID: 8010051
> arrayInput = arrayInput.total_Wages[4];

this line doesn't make any sense.


if arrayInput is the instance of your class, your are trying to assign total_Wages[4] (a double) to your instance which is of type Arrays.
0
 

Author Comment

by:Bennett
ID: 8010361
sorry yeah youre right the line is
wages = arrayinput.totalWages[4]
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8010425
so is it working now?

if not, what is wages? it should be a double.
if so, please accept a comment as answer so this can be moved to PAQ (previously asked questions)
0
 

Author Comment

by:Bennett
ID: 8010565
sorry yeah youre right the line is
wages = arrayinput.totalWages[4]
0
 

Author Comment

by:Bennett
ID: 8010575
Ok its not a race, thanks for the help, you can have the points now. ;-)
0
 

Author Comment

by:Bennett
ID: 8010579
Thanks very much for the help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question