I'm trying to initialize an array of objects in a for loop, each object with a different value that comes from the console, I have a for loop to do this and everything is ok until I see the objects, all the objects have the values of the second input, I have tried a lot of different things but I can't see what's wrong, I'm 17 years old and a total beginner, I'm not a native English speaker so sorry if is hard to understand, sorry if the solution is obvious. Please help me, Thank you.
This is my main class code.
import java.util.Scanner;
public class StockDriver {
static Stocks stockObj[];
static int NumStocks;
static String Symbol;
static String ComName;
static int Shares;
public static void main(String[] args) throws Exception {
Scanner cin = new Scanner(System.in);
System.out.println("How many stocks do you want to create?");
//Stocks input
try { //Try-Catch to handle Imput Mismatch Exeption
NumStocks = cin.nextInt();
cin.nextLine();
System.out.println("You entered: " NumStocks);
} catch (Exception e) {
System.out.println("That is an improper value. The program has been stoped.");
cin.close();
System.err.println(e "in Stock");
}
System.out.println("You will now enter in the company symbol, name and their share quantity. The price will be randomly generated.");
//Initialitation of stock objects
stockObj = new Stocks[NumStocks];
//For loop to ask for the imput of all stock, forced to a maximum of 10 stocks
for(int i = 0; i < NumStocks && i < 10; i ){
stockObj[i] = new Stocks(Symbol,ComName,Shares);
//Symbol input
System.out.println("Please enter the three character symbol: ");
try { //Try-Catch to handle Imput Mismatch Exeption
Symbol = cin.nextLine();
} catch (Exception e) {
System.out.println("That is an improper value. The program has been stoped.");
cin.close();
System.err.println(e "in Symbol");
}
//Company Name input
System.out.println("Please enter the conpany name:");
try { //Try-Catch to handle Imput Mismatch Exeption
ComName = cin.nextLine();
} catch (Exception e) {
System.out.println("That is an improper value. The program has been stoped.");
cin.close();
System.err.println(e "in Name");
}
//Number of Shares input
System.out.println("Please enter the total shares:");
try { //Try-Catch to handle Input Mismatch Exeption
Shares = cin.nextInt();
cin.nextLine();
} catch (Exception e) {
System.out.println("That is an improper value. The program has been stoped.");
cin.close();
System.err.println(e "in Shares");
}
// stockObj[i] = new Stocks(Symbol,ComName,Shares);
}
for( int i = 0; i < NumStocks;i ){
System.out.println("this is " i);
System.out.println(stockObj[i].getSymbol());
}
}
}
This a class for my program.
public class Stocks {
private static String Symbol;
private static String ComName;
private static int Shares;
private static double price;
Stocks(String symbol, String comName, int shares) {
Symbol = symbol;
ComName = comName;
Shares = shares;
}
//Method to get symbol
public String getSymbol(){
return Symbol;
}
//Method to get Company Name
public String getComName(){
return ComName;
}
//Method to get the random price between $1.00 and $200.99
public double randomPrice(){
double price = (Math.random() * (200.99 - 1.00)) 1.00;
Stocks.price = price;
return price;
}
//Method to get Shares
public int getShares(){
return Shares;
}
//Method to get the total value of the stock
public double totalValue(){
return price * Shares;
}
}
In the next for loop I ask for three inputs the number of times that was entered before, what I tried to do is to store the inputs the first time of the loop and create a object with this inputs before the loop ask for the inputs again and the inputs change. I have verified and the program does store different inputs so this is no the problem. The problem is that when a take a look at each object have the same values although there were different inputs, the input that is stored in all objects is one before the last one or the last one. I would like understand what's going on and know a way to store the different inputs in the objects, thank you very much and sorry for the long reading.
for(int i = 0; i < NumStocks && i < 10; i ){
//stockObj[i] = new Stocks(Symbol,ComName,Shares);
//Symbol input
System.out.println("Please enter the three character symbol: ");
try { //Try-Catch to handle Imput Mismatch Exeption
Symbol = cin.nextLine();
} catch (Exception e) {
System.out.println("That is an improper value. The program has been stoped.");
cin.close();
System.err.println(e "in Symbol");
}
//Company Name input
System.out.println("Please enter the conpany name:");
try { //Try-Catch to handle Imput Mismatch Exeption
ComName = cin.nextLine();
} catch (Exception e) {
System.out.println("That is an improper value. The program has been stoped.");
cin.close();
System.err.println(e "in Name");
}
//Number of Shares input
System.out.println("Please enter the total shares:");
try { //Try-Catch to handle Input Mismatch Exeption
Shares = cin.nextInt();
cin.nextLine();
} catch (Exception e) {
System.out.println("That is an improper value. The program has been stoped.");
cin.close();
System.err.println(e "in Shares");
}
stockObj[i] = new Stocks(Symbol,ComName,Shares);
}
CodePudding user response:
All the fields in Stocks class are static, that is, they exist per class -- not per instance, and therefore the same values are shared across all instances when they are created.
To fix the issue, just remove static modifier, and change the field names to be camelCase to comply with Java naming conventions:
public class Stocks {
private String symbol;
private String comName;
private int shares;
private double price;
// ...
// update getters/setters/constructor according to the updated field names
}
