This site requires JavaScript, please enable it in your browser!
Greenfoot back
someone1
someone1 wrote ...

2015/8/20

Storing data from a for loop

someone1 someone1

2015/8/20

#
I want to store data from a for loop and add my answer up. The thing is every time the for loop executes another iteration it loses the data from the first iteration, for example lets say i want to go shopping on two days this week, i want to retain the info from the first iteration and second and then add them how would i do this
import java.util.Scanner;
Scanner keyboard = new Scanner(System.in);
int noDays;
float sClothes, sShoes, sFood;
public void mainMethod {
System.out.println("How many days are you going shopping this week: ");
noDays = keyboard.nextInt();
for(int i = noDays;i > 0; i-- ) {
System.out.println("How much did you spend on clothes today: ");
sClosthes = keyboard.nextFloat();
System.out.println("How much did you spend on shoes today: ");
sShoes.keyboard.nextFloat();
System.out.println("How much did you spend on food today: ");
sFood.nextFloat
}


}
danpost danpost

2015/8/20

#
You first need to fix errors in the current code before continue on with summing up the amount spent on multiple days. Lines 12 and 14 are trying to get unknown states of 'float' values and line 14 is not even ended by a semi-colon. You will need another float field for the total spent and add to it the value of 'sFood' at the end and inside the 'for' loop.
someone1 someone1

2015/8/21

#
Ok i fixed the errors, but i dont get what ur saying about the sum, if i add another float field called total and add it to sFood wouldnt that just give me the value of sFood not the other, i want to add all the values from all iterations for example say i go shopping on two days, i spent $10 on clothes, $20 on shoes and $10 on food. On the second day i spend $20 on clothes, $40 on shoes and $10 on food, the total should be $110, I want to display the total to the user, I created a summing method, but eveytime I call it it only shows me the latest iteration not the total of both iterations. Could you please show me some code of how to fix this thanx
import java.util.Scanner;
Scanner keyboard = new Scanner(System.in);
int noDays;
float sClothes, sShoes, sFood;
public void mainMethod {
System.out.println("How many days are you going shopping this week: ");
noDays = keyboard.nextInt();
for(int i = noDays;i > 0; i-- ) {
System.out.println("How much did you spend on clothes today: ");
sClosthes = keyboard.nextFloat();
System.out.println("How much did you spend on shoes today: ");
sShoes = keyboard.nextFloat();
System.out.println("How much did you spend on food today: ");
sFood = keyboard.nextFloat();
getTotal();
}
public float getTotal() {
float total;
total = sFood + sShoes + sClothes;
return total;

}
 
 
}
danpost danpost

2015/8/21

#
Line 18 is declaring 'total' inside the method where it is lost when the method ends. Declare it with the others on line 4.
someone1 someone1

2015/8/21

#
Ok now what do i do, i tried this but it still just gives me the latest iteration value
import java.util.Scanner;
Scanner keyboard = new Scanner(System.in);
int noDays;
float sClothes, sShoes, sFood, total;
public void mainMethod {
System.out.println("How many days are you going shopping this week: ");
noDays = keyboard.nextInt();
for(int i = noDays;i > 0; i-- ) {
System.out.println("How much did you spend on clothes today: ");
sClosthes = keyboard.nextFloat();
System.out.println("How much did you spend on shoes today: ");
sShoes = keyboard.nextFloat();
System.out.println("How much did you spend on food today: ");
sFood = keyboard.nextFloat();
getTotal();
}
public float getTotal() {

total = sFood + sShoes + sClothes;
return total;
 
}
  
  
}
danpost danpost

2015/8/21

#
someone1 wrote...
Ok now what do i do, i tried this but it still just gives me the latest iteration value
Well, you probably need to add a line of code to display the total (after calling 'getTotal').
someone1 someone1

2015/8/21

#
I did, i used system.out.println to display the total, if i added static to my methods would that change the value of total because i tried using that, anyway the answer i got is still the total of the last iteration not the total of all the iterations
someone1 someone1

2015/8/21

#
I figured it out finally thanks any way
You need to login to post a reply.