I'm new to Java. I am tasked with creating a menu program, one option is to generate a username in the form of first initial and surname.
The method is stringOperation(String f, String s)
Variables are fName and sName.
Here is the code. I have highlighted the areas I need help with. The rest of the code is OK, I think. This is a section of the pseudocode that explains what is required:
stringOperation(String f, String s)
3.1.1 Assign first character of first initial to variable using f.substring(start position, length of string).
3.1.2 Concatenate first initial with users surname.
3.1.3 print username to console.
import java.util.Scanner; // imports scanner class
public class Assessment {
public static void main(String[] args) { //main method
menu(); //call menu method
}
public static void menu() { //method to display menu options
int choice;
String fName;
String sName;
Scanner sc = new Scanner(System.in);
//displays menu options
System.out.println("Welcome");
System.out.println("1. Username");
System.out.println("2. Factorial");
System.out.println("3. Area of triangle");
System.out.println("4. Circumference of circle");
System.out.println("5. Exit");
//asks for user input
do {
System.out.println("Enter your first name");
fName = sc.next();
System.out.println("Enter your surname");
sName = sc.next();
System.out.println("Thank you. Now enter a selection (1-5):");
choice = sc.nextInt();
//menu loop
switch (choice) {
case 1:
**stringOperation(String fName, String sName);**
break;
case 2:
numberFactorial();
break;
case 3:
areaTriangle();
break;
case 4:
circumferenceCircle();
break;
}
}while (choice!=5);
}
**//stringOperation method
private static void stringOperation(String f, String s) {
String initial = f.substring(0,1);
String username = initial + s;
System.out.println("Your username is " + initial + s);
}**
public static void numberFactorial() { //method to calculate factorial of a number
//variables
int number;
int factorial = 1;
int i;
//input
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number: ");
number = sc.nextInt();
//for loop
for (i = 1; i <= number; i++) {
factorial = factorial * i;
}
System.out.println("Factorial of " + number + " is " + factorial);
}
public static void areaTriangle ()//method to calculate area of a triangle
{ //input
Scanner sc = new Scanner(System.in);
//variables
double width;
double height;
double area;
//input
System.out.println("Enter the width: ");
width = sc.nextInt();
System.out.println("Enter height: ");
height = sc.nextInt();
area = (height * width) / 2;
System.out.println("The area is :" + area);
}
public static void circumferenceCircle ()//method to calculate circumference of a circle
{ //variables
double radius;
double circumference;
Scanner sc = new Scanner(System.in);
System.out.println("Enter radius: ");
radius = sc.nextDouble();
circumference = Math.PI * 2 * radius;
System.out.println("The circumference is : " + circumference);
}
}
If you want to make the method cleaner you could do something like this
private static void stringOperation(String f, String s) {
System.out.println("Your username is " + f.substring(0,1) + s)
}
If you need to refer back to the new username then have a global variable that your method can set to refer to later like this.
private static void stringOperation(String f, String s) {
Assessment.username = f.substring(0,1) + s;
System.out.println("Your username is " + Assessment.username);
}
Related
I want to ask the user through a scanner class but I want this scanner to be in a method named readInput() and the output on a different method named writeOutput() using gett-setter
this is my code below:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LaboratoryExercise2 gro = new LaboratoryExercise2();
String[] SecondQuestion;
System.out.println("Select the item your purchasing.");
String Product1 = sc.nextLine();
System.out.println("Enter the Quantity and price separated by SPACE.");
SecondQuestion = sc.nextLine().split(" ");
int Quantity1 = Integer.parseInt(SecondQuestion[0]);
double Price1 = Double.parseDouble(SecondQuestion[1]);
double Amount1 = 0;
Amount1 = Quantity1 * Price1;
int Quantity = 0 + Quantity1;
double Amount = 0 + Amount1;
double Price = 0 + Price1;
I want the output of this to show on a different method
gro.setGrocery(Price, Quantity, Amount);
System.out.println("You've selected " + gro.getitemQuantity() + " " + Product1 + " " + "at " + " " +
gro.getitemPrice() + " each");
System.out.println("Amount due is " + gro.getamountDue());
This is my whole code:
import java.util.Scanner;
public class LaboratoryExercise2 {
private double itemPrice;
private int itemQuantity;
private double amountDue;
public void setGrocery(double newitemPrice, int newitemQuantity, double newamountDue) {
itemPrice = newitemPrice;
itemQuantity = newitemQuantity;
amountDue = newamountDue;
}
public double getitemPrice() {
return itemPrice;
}
public int getitemQuantity() {
return itemQuantity;
}
public double getamountDue() {
return amountDue;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LaboratoryExercise2 gro = new LaboratoryExercise2();
String[] SecondQuestion;
System.out.println("Select the item your purchasing.");
String Product1 = sc.nextLine();
System.out.println("Enter the Quantity and price separated by SPACE.");
SecondQuestion = sc.nextLine().split(" ");
int Quantity1 = Integer.parseInt(SecondQuestion[0]);
double Price1 = Double.parseDouble(SecondQuestion[1]);
double Amount1 = 0;
Amount1 = Quantity1 * Price1;
int Quantity = 0 + Quantity1;
double Amount = 0 + Amount1;
double Price = 0 + Price1;
gro.setGrocery(Price, Quantity, Amount);
System.out.println("You've selected " + gro.getitemQuantity() + " " + Product1 + " " + "at " + " " +
gro.getitemPrice() + " each");
System.out.println("Amount due is " + gro.getamountDue());
}
}
Piling all your code into main is a bad idea. Java is object oriented, and main, being static, isn't.
The solution for sharing data between methods is usually to make a field.
class Main {
public static void main(String[] args) throws Exception {
new Main().go();
}
private Scanner scanner;
void go() throws Exception {
scanner = new Scanner(System.in);
scanner.useDelimiter("\\R");
int quantity = askInt("Enter the quantity: ");
}
private int askInt(String prompt) {
while (true) {
System.out.print(prompt);
if (scanner.hasNextInt()) return scanner.nextInt();
scanner.next(); // eat the non-int token
System.out.println("ERROR: Please enter an integer number.");
}
}
}
That's what virtually all command line java apps should look like: a one-liner main method, no use of static anywhere except main, a scanner field, set with the proper delimiter (\\R, which means .nextX() always reads one line worth of input; use .next() to read an entire line. Don't call .nextLine(), ever. nextLine() and all the other next methods interact in nasty ways that make scanner useless or at least unwieldy and bugprone for command line 'prompting', which why you do it this way.
Hi I was wondering if someone could help me make it so when I choose an option in my code when the program runs, it skips all the other options that weren't chosen and only uses the code inputted by the user. So only the specific parts in the code should be printed but I don't know how to skip over code. There is probably things I should remove and then things I should add but I need help.
package test;
import java.util.Scanner;
import java.text.NumberFormat;
public class Test
{
static Scanner input = new Scanner( System.in );
public static void main(String[] args)
{
//Variables
String Option;
int a;
int Base;
int Height;
int Length;
int Width;
int Radius;
int r;
int b;
double Area;
//Menu
System.out.println("Welcome to the Area Calculator");
System.out.println("");
System.out.println("1. Square");
System.out.println("2. Rectangle");
System.out.println("3. Circle");
System.out.println("4. Triangle");
System.out.println("5. Quit");
System.out.println("Please enter the number of the shape you would like to calculate the area for.");
Option=input.next();
}
public static void Sqaure(String[] args)
{
System.out.println("Please enter the length of one side.");
int a=input.nextInt();
System.out.println("Please enter the length of one side.");
double area = a*a;
System.out.println("The area of the given shape is " + area + " square units.");
}
public static void Rectangle(String[] args)
{
System.out.println("Please enter the length.");
int Length=input.nextInt();
System.out.println("Please enter the width.");
int Width=input.nextInt();
double area = Length*Width;
System.out.println("The area of the given shape is " + area + " square units.");
}
public static void Circle(String[] args)
{
System.out.println("Please enter the radius.");
int r=input.nextInt();
double Radius = r*r;
double area = Math.PI * Radius;
System.out.println("The area of the given shape is " + area + " square units.");
}
{
System.out.println("Please enter the base length.");
int b=input.nextInt();
double Base = b * .5;
System.out.println("Please enter the height lenth.");
int Height=input.nextInt();
double area = Base * Height;
System.out.println("The area of the given shape is " + area + " square units.");
}
}
So, it is clear to me you're very new with Java and you're learning! So I went ahead and made a complete working version of what you are trying to do.
Although it is a simple program Ill go through what each section does.
Prompt Method
Asks the user which area they want to find, this method has the direct answer to your question in the form of a series of if/if else statements. It gets the input from the user and then goes through the series of if/if else statements, seeing if the value entered matches any of them, if it does then it runs that section of code and skips the rest. If a number other then 1-4 is entered, it quits the program (see the else statement), see this page for more info on if statements. I opted for a bunch of if statements but there is a better, cleaner way, but it is a little confusing if you are brand new, see here to learn about those.
Area Methods
These are all pretty much the same except for he way they calculate the areas. They first prompt the user for the required measurements for that shape and assign those entered values to variables, then it prints out the resulting area after running the values through a calculation, then the program stops.
Main Method
All the main method is doing is calling prompt, that keeps it clean, you don't really ever want to do anything but call other methods or create objects in the main method, so most people put the nitty gritty somewhere else and call it.
Below is the working code
Go ahead and paste into a doc and run it, just be aware that I did not bother to add any error catching to keep it simple so keep it to whole numbers without decimals (also called ints, eg. 1, 2, 4, not 4.3, 5.4, etc.)
Side note:
In your methods you have (String args[]) in the brackets, this is not needed, in fact it will cause trouble, that is only used in the main method and is a relatively advanced way of providing input to the program, for now you can keep to leaving the brackets empty.
import java.util.*;
public class Area
{
public static void main(String args[])
{
prompt(); //Calls prompt method
}
//Prompt Method
public static void prompt()
{
int option;
Scanner input = new Scanner(System.in);
//Ask the user which shape they want to find the area of
System.out.println("Select from the following shapes to calulate the area:");
System.out.println("Rectangle -- 1\nCircle -- 2\nTriangle -- 3\nQuit -- 4\n");
System.out.print("Your option: ");
option = input.nextInt();
System.out.println();
//THIS IS THE PART THAT DIRECTLY PERTAINS TO YOUR QUESTION
if(option == 1)
{
rectangle();
}
else if(option == 2)
{
circle();
}
else if(option == 3)
{
triangle();
}
else
{
System.out.println("Program stopped by user"); //For stopping the program when they want to quit instead
System.exit(0);
}
}
//Rectangle Method
public static void rectangle()
{
double h;
double b;
Scanner input = new Scanner(System.in);
System.out.println("RECTANGLE");
System.out.print("Enter Height: ");
h = input.nextDouble();
System.out.print("Enter Base: ");
b = input.nextDouble();
System.out.print("\nArea of Rectangle = " + (h*b) + "\n");
}
//Circle Method
public static void circle()
{
double pi = 3.14;
double radius;
Scanner input = new Scanner(System.in);
System.out.println("CIRCLE");
System.out.print("Enter radius: ");
radius = input.nextDouble();
System.out.print("\nArea of Circle = " + (pi*(radius*radius)) + "\n");
}
//Triangle Method
public static void triangle()
{
double h;
double b;
Scanner input = new Scanner(System.in);
System.out.println("TRIANGLE");
System.out.print("Enter Height: ");
h = input.nextDouble();
System.out.print("Enter Base: ");
b = input.nextDouble();
System.out.print("\nArea of Triangle = " + ((h*b)/2) + "\n");
}
}
package test;
import java.util.Scanner;
import java.text.NumberFormat;
public class Test
{
static Scanner input = new Scanner( System.in );
public static void main(String[] args)
{
//Variables
String Option;
int a;
int Base;
int Height;
int Length;
int Width;
int Radius;
int r;
int b;
double Area;
String [] inputString = new String [50];
//Menu
System.out.println("Welcome to the Area Calculator");
System.out.println("");
System.out.println("1. Square");
System.out.println("2. Rectangle");
System.out.println("3. Circle");
System.out.println("4. Triangle");
System.out.println("5. Quit");
System.out.println("Please enter the number of the shape you would like to calculate the area for.");
Option=input.next();
switch (Option) {
case "1": Sqaure(inputString);
break;
case "2": Rectangle(inputString);
break;
case "3": Circle(inputString);
break;
default: someExtraFig(inputString);
break;
}
}
public static void Sqaure(String[] args)
{
System.out.println("Please enter the length of one side.");
int a=input.nextInt();
System.out.println("Please enter the length of one side.");
double area = a*a;
System.out.println("The area of the given shape is " + area + " square units.");
}
public static void Rectangle(String[] args)
{
System.out.println("Please enter the length.");
int Length=input.nextInt();
System.out.println("Please enter the width.");
int Width=input.nextInt();
double area = Length*Width;
System.out.println("The area of the given shape is " + area + " square units.");
}
public static void Circle(String[] args)
{
System.out.println("Please enter the radius.");
int r=input.nextInt();
double Radius = r*r;
double area = Math.PI * Radius;
System.out.println("The area of the given shape is " + area + " square units.");
}
public static void someExtraFig(String ...s)
{
System.out.println("Please enter the base length.");
int b=input.nextInt();
double Base = b * .5;
System.out.println("Please enter the height lenth.");
int Height=input.nextInt();
double area = Base * Height;
System.out.println("The area of the given shape is " + area + " square units.");
}
}
I am receiving errors during compilation. It's expecting a .class. I don't think it should require one. I'm pretty new at coding so forgive my ignorance. I also would like some guidance on how to nullify Case when the user inputs C or F so they can put c or f and not get an error messages.
This is my code:
import java.util.Scanner;
import java.util.InputMismatchException;
public class TempCALC
{
public static void main(String[] args)
{
System.out.println("This Program will allow the user to calculate temperature.");
calculateTemp();
}
private static void calculateTemp() {
int F;
int C;
F=0;
C=1;
Scanner input = new Scanner (System.in);
System.out.println("Please enter a F to convert Fahrenheit to Celsius.");
System.out.println("Please enter a C to convert Celsius to Fahrenheit.");
int option = input.nextInt();
if (int=0) {
System.out.println("Please enter a temperature in degrees Fahrenheit.");
ftoc();
} else if (int= 1) {
System.out.println("Please enter a temperature in degrees Celsius.");
ctof();
} else {
System.out.println("ERROR PLEASE ENTER A F OR A C TO PROCEED!");
}
}
private static void ftoc() {
Scanner input = new Scanner(System.in);
Double celsius = input.nextDouble();
System.out.println(celsius + "celsius is" + ((celsius * 9 / 5.0) + 32) + "Fahrenheite");
calculatetemp();
}
private static void ctof() {
Scanner input = new Scanner(System.in);
Double Fahrenheit = input.nextDouble();
System.out.println(Fahrenheit + "Fahrenheit is" + ((Fahrenheit - 32) * (5 / 9.0)) + "Celsius");
calculatetemp();
}
private static void print(String string); {
System.out.println("\n" + string);
}
}
if (int=0)
This is not valid Java syntax. int is a type not a variable. You meant to write:
if(option == 0)
Notice the == (comparison) instead of = (assignment).
This is the flow you meant to implement:
if (option == 0) {
System.out.println("Please enter a temperature in degrees Fahrenheit.");
ftoc();
} else if (option == 1) {
System.out.println("Please enter a temperature in degrees Celsius.");
ctof();
} else {
System.out.println("ERROR PLEASE ENTER A F OR A C TO PROCEED!");
}
If you wish for the user to enter f or c instead to mention the degrees, you will need to use:
String option = input.next();
To get a String instead of an Integer.
And then:
if (option.equals('F') { ... }
else if(option.equals('C') { ... }
else { ... }
If you want your input to be case-insensitive take a look at toLowerCase or toUpperCase and apply it to your needs (there is an answer here that shows its use).
int is reserved key word in java you can not used it with expression to compare,you should use his value like this:
int option = input.nextInt();
if (option ==0) {
Not if (int=0) { and = is used to assign but == is used to check equality.
You first have to check the validation in your ifs statements, as the previous answers mentioned.
Also, I think that your actual conversion formulas are swapped.
Then, in order to receive either 'C' or 'c':
Read a String, instead of an int:
String option = input.next();
And then convert all the input to lowercase:
if (option.toLowerCase().equals("f"))
Here is the complete example:
public class TempCALC {
public static void main(String[] args) {
System.out.println("This Program will allow the user to calculate temperature.");
calculateTemp();
}
private static void calculateTemp() {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a F to convert Fahrenheit to Celsius.");
System.out.println("Please enter a C to convert Celsius to Fahrenheit.");
String option = input.next();
if (option.toLowerCase().equals("f")){
System.out.println("Please enter a temperature in degrees Fahrenheit.");
ftoc();
}else if (option.toLowerCase().equals("c")){
System.out.println("Please enter a temperature in degrees Celsius.");
ctof();
}else{
System.out.println("ERROR PLEASE ENTER A F OR A C TO PROCEED!");
}
}
private static void ftoc() {
Scanner input = new Scanner(System.in);
Double celsius = input.nextDouble();
System.out.println(celsius + "celsius is" + ((celsius * 9 / 5.0) + 32) + "Fahrenheite");
calculatetemp();
}
private static void ctof() {
Scanner input = new Scanner(System.in);
Double Fahrenheit = input.nextDouble();
System.out.println(Fahrenheit + "Fahrenheit is" + ((Fahrenheit - 32) * (5 / 9.0)) + "Celsius");
calculatetemp();
}
private static void print(String string){
System.out.println("\n" + string);
}
private static void calculatetemp(){
System.out.println("\nInside calculateTemp");
}
}
This question already has answers here:
How to use Scanner to accept only valid int as input
(6 answers)
Closed 7 years ago.
This is my current code. What I would like to do is limit the users input to only int. If a double is entered, I'd like a line printed "Please enter a whole number." I've tried to play around with scanner.hasNextint but haven't had much success. Is there a way to ignore double as an input entirely and have it round?
Thanks in advance!
public class BMI extends DecimalFormat{
public static void main(String[] args) {
int weight;
int height;
double bodyMassIndex;
DecimalFormat dfWithTwoDecimalPlaces;
Scanner scanner;
scanner = new Scanner(System.in);
System.out.print("What is your weight in kilograms (kg): ");
weight = scanner.nextInt();
System.out.print("What is your height in centimeters(cm): " );
height = scanner.nextInt();
bodyMassIndex = (weight / Math.pow(height/100.0, 2.0) );
dfWithTwoDecimalPlaces = new DecimalFormat ("0.00");
System.out.print("Your Body Mass Index is: " + dfWithTwoDecimalPlaces.format(bodyMassIndex));
}
}
Create a user defined method getNextInt() as bellow:
/**get next integer*/
public static int getNextInt(Scanner scanner) {
while (!scanner.hasNextInt()) {
scanner.next();
}
return scanner.nextInt();
}
public static void main(String[] args) {
int weight;
int height;
double bodyMassIndex;
DecimalFormat dfWithTwoDecimalPlaces;
Scanner scanner;
scanner = new Scanner(System.in);
System.out.print("What is your weight in kilograms (kg): ");
weight = getNextInt(scanner);//call user-defined method
System.out.print("What is your height in centimeters(cm): " );
height = getNextInt(scanner);//call user-defined method
bodyMassIndex = (weight / Math.pow(height/100.0, 2.0) );
dfWithTwoDecimalPlaces = new DecimalFormat ("0.00");
System.out.print("Your Body Mass Index is: " + dfWithTwoDecimalPlaces.format(bodyMassIndex));
}
You can add a message inside while loop like this:
while (!scanner.hasNextInt()) {
scanner.next();
System.out.println("Please enter a whole number");//message
}
1.use try {} catch{} block (reference java exception handling)
public class MySource extends DecimalFormat{
public static void main(String[] args) {
int weight;
int height;
double bodyMassIndex;
DecimalFormat dfWithTwoDecimalPlaces;
Scanner scanner;
scanner = new Scanner(System.in);
while(true) {
try {
System.out.print("What is your weight in kilograms (kg): ");
weight = scanner.nextInt();
System.out.print("What is your height in centimeters(cm): " );
height = scanner.nextInt();
break;
}catch(InputMismatchException e) {
System.out.println("Please enter a whole number.");
scanner.nextLine();
}
}
bodyMassIndex = (weight / Math.pow(height/100.0, 2.0) );
dfWithTwoDecimalPlaces = new DecimalFormat ("0.00");
System.out.print("Your Body Mass Index is: " + dfWithTwoDecimalPlaces.format(bodyMassIndex));
}
}
Hey guys just need help on how to finish this up.
Code Snippet:
import java.util.Scanner;
public class CreateLoans implements LoanConstants {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//set the program here
float prime;
float amountOfLoan = 0;
String customerFirstName;
String customerLastName;
String LoanType;
System.out.println("Please Enter the current prime interest rate");
prime = sc.nextInt() / 100f;
//ask for Personal or Business
System.out.println("are you after a business or personal loan? Type business or personal");
LoanType = sc.next();
//enter the Loan amount
System.out.println("Enter the amount of loan");
amountOfLoan = sc.nextInt();
//enter Customer Names
System.out.println("Enter First Name");
customerFirstName = sc.next();
System.out.println("Enter Last Name");
customerLastName = sc.next();
//enter the term
System.out.println("Enter the Type of Loan you want. 1 = short tem , 2 = medium term , 3 = long term");
int t = sc.nextInt();
}
}
I need to display the records I have asked and store the object into an array.
so this where I'm stuck. I need to do this in a loop 5 times and by the end display all records in an array, if that makes sense?
Try this way :
import java.util.Scanner;
public class CreateLoans {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Loan[] loans = new Loan[5];
for(int i=0;i<5;i++) {
loans[i] = new Loan();
System.out.println("Please Enter the current prime interest rate");
float prime = sc.nextInt();
prime = (float)(prime/100f);
loans[i].setPrime(prime);
//ask for Personal or Business
System.out.println("are you after a business or personal loan? Type business or personal");
String loanType = sc.next();
loans[i].setLoanType(loanType);
//enter the Loan amount
System.out.println("Enter the amount of loan");
float amountOfLoan = sc.nextFloat();
loans[i].setAmountOfLoan(amountOfLoan);
//enter Customer Names
System.out.println("Enter First Name");
String customerFirstName = sc.next();
loans[i].setCustomerFirstName(customerFirstName);
System.out.println("Enter Last Name");
String customerLastName = sc.next();
loans[i].setCustomerLastName(customerLastName);
}
//Display details
for(int i=0;i<5;i++) {
System.out.println(loans[i]);
}
}
}
class Loan {
private float prime;
private float amountOfLoan = 0;
private String customerFirstName;
private String customerLastName;
private String LoanType;
public float getPrime() {
return prime;
}
public void setPrime(float prime) {
this.prime = prime;
}
public float getAmountOfLoan() {
return amountOfLoan;
}
public void setAmountOfLoan(float amountOfLoan) {
this.amountOfLoan = amountOfLoan;
}
public String getCustomerFirstName() {
return customerFirstName;
}
public void setCustomerFirstName(String customerFirstName) {
this.customerFirstName = customerFirstName;
}
public String getCustomerLastName() {
return customerLastName;
}
public void setCustomerLastName(String customerLastName) {
this.customerLastName = customerLastName;
}
public String getLoanType() {
return LoanType;
}
public void setLoanType(String loanType) {
LoanType = loanType;
}
#Override
public String toString() {
return "First Name : " + customerFirstName + "\n" +
"Last Name : " + customerLastName + "\n" +
"Amount of Loan : " + amountOfLoan + "\n" +
"Loan type : " + LoanType + "\n" +
"Prime : " + prime + "\n\n";
}
}
Create a Loan class and put all necessary details as private members into it and override toString() method.
Make a ArrayList and add all the variables inside that list
ArrayList arrlist = new ArrayList();
arrlist.add(prime);
arrlist.add(LoanType);
arrlist.add(amountOfLoan);
arrlist.add(customerFirstName );
arrlist.add(customerLastName);
arrlist.add(t);
and display the ArrayList
System.out.println(arrlist);
Example of a loop
int[] nums = new int[5];
String[] names = new String[5];
Scanner input = new Scanner(System.in);
for (int i = 0; i < 5; i++){
System.out.println("Enter a number: ");
int number = input.nextInt();
// insert into array
nums[i] = number;
System.out.println("Enter a name: ");
String name = input.nextLne();
// insert into array
names[i] = name;
}
Everything you want to be looped 5 times, you can put inside the loop. Whatever values you want to store, you can do that in the loop also.