0

I need a little help about showing all inputted arrays.

float[] month; 
month = new float[12];     

for (int counter=0; counter<12; counter++) {         
    float revenueMonths = Float.parseFloat(JOptionPane.showInputDialog("Revenue month of " + (counter + 1)));
    month[counter] = revenueMonths;        
    totalRev = totalRev + month[counter];                        
 }

As you can see from my code, it is asking the user to input the revenue of each month. The value of months for revenue is not declared, it will be declared by the user inputs.

So, if a user inputs each month revenue as:

month 1 : 100 
month 2 : 100 
till 12 : 100 

I want to print out the revenues of each month

month 1 : 100 
month 2 : 100
m 3 : 100 
till 12 : 100 

How do I do that? Thank you!

1

2 Answers 2

0

You have all the data in the array month, why not just print them out?

for (int i = 0; i < month.length; ++i) {
        System.out.println("Month " + (i + 1) + ": " + month[i]);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

This will print 1 based array index and revenue per month on separate lines. It will a different starting string for the last month as you would like. You would run this after your existing for loop.

for (int counter = 0; counter < month.length-1; counter++) {
    System.out.format("month %d : %f\n", counter+1, month[counter]);
}
System.out.format("till %d : %f\n", month.length, month[month.length-1]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.