How to use one array on 2 different classes - java

I have 2 classes right now, the first class has the arraylist in it. But on the second class when I try to access the arraylist it keeps giving me the red line underneath saying that the variable doesn't exist.
Here is class one...
public class BankMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
BankMain main = new BankMain();
menu();
}
public static void cardNumbers(){
ArrayList<Integer> cardNum = new ArrayList<Integer>();
Scanner cards = new Scanner(System.in);
Scanner input = new Scanner(System.in);
Scanner keyboard = new Scanner(System.in);
System.out.println("Please select a 5 digit card number");
cardNum.add(input.nextInt());
System.out.println("Thank you! You're card number is " +cardNum);
System.out.println("Type 'c' to go back to main menu.");
String value = keyboard.next();
if(value.equalsIgnoreCase("c")){
menu();
}
else if (!keyboard.equals('c')){
System.out.println("Invalid Entry!");
}
}
public static void menu(){
System.out.println("What Would you like to do today?");
System.out.println();
System.out.println("Create Account = 1");
System.out.println("Login = 2");
System.out.println("Exit = 3");
query();
}
public static void query(){
Scanner keyboard = new Scanner(System.in);
double input = keyboard.nextInt();
if (input == 2){
BankMainPart2 main2 = new BankMainPart2();
System.out.println("Please enter your 5 digit card number.");
main2.loginCard();
}
else if (input == 1){
cardNumbers();
}
else if (input == 3){
System.out.println("Thank you, have a nice day!");
System.exit(0);
}
}
}
Here is the second class...
public class BankMainPart2 {
public static void loginCard(){
if (cardNum.contains(name)) {
}
}
}
I know I haven't entered anything in the if statement yet on the second class but I'm just trying to get my array list to work on both classes.

The code looks very naive. A very simple answer to your question is
You have not declared any cardNum in BankMainPart2 as global variable or in loginCard as local variable, how do you think it will be available in the loginCard method?
ArrayList<Integer> cardNum = new ArrayList<Integer>();
is local to cardNumbers method.
How can you access it from other class?
A local variable cannot be accessed from outside the method, so first thing, make cardNum class level variable
Make the variable public if you want other classes to be able to access it directly, else make the variable private and create getter method (setter if required).
You can also send the variable when calling the method as argument
If this is class level variable, make it static and use Classname.variable.
--Edit--
As you have asked for details let me give you a quick overview of the different approaches.
A variable declared inside a method is local. as name suggest "local", no one but the method knows there is such a variable. No other method in the class knows about existence of this variable, let alone some outside class.
I say you can make it static, but static should strictly be used for class level storage, not object level. Say a list which is modified by multiple objects of the same class (I hope you know concepts of objects, else go to the basics otherwise it will not be clear). Now as per your example, I guess this is not what you want.
A public variable is generally no - no, only in few cases it will be useful (for example in android programming where performance is utmost important). Normally we will create a variable and provide getter setters. A getter or setter is used normally when we want to give access to the variable, which again does not look like what you want.
Last, the variable is private to you class, but if you want some method to do something about it, you can pass it as argument, this looks the case for you.
Step by step
take the variable out of method and add to class level, note that I removed static from method names
public class BankMain {
private ArrayList<Integer> cardNum = new ArrayList<Integer>();
// rest of code as it is
..
..
BankMain main = new BankMain();
//change
main.menu();
//no need foe static
public void cardNumbers(){
//no need here now
//ArrayList<Integer> cardNum = new ArrayList<Integer>();
Scanner cards = new Scanner(System.in);
Scanner input = new Scanner(System.in);
..
..
//public static void menu(){
public void menu(){
//send the list
//I see there are confusion at times regarding calling of static method.
//please note objectname.staticMethod() or classname.staticMethod() is one
//and same thing. Just that classname.staticMethod() is more clear
BankMainPart2.loginCard(cardNum);
}
and
public class BankMainPart2 {
public static void loginCard(ArrayList<Integer> cardNum){
if (cardNum.contains(name)) {
}
}
}

Your method, BankMainPart2.loginCard has not context of "cardNum", it doesn't know what it is (type or value).
In order for the method to be able to act on the array list, you must pass a reference to it, something like...
public class BankMainPart2 {
public static void loginCard(ArrayList<Integer> cardNum){
if (cardNum.contains(name)) {
}
}
}

make the cardnum arraylist as an instance variable in BankMain class and extend BankMain in BankMainClass2 and using reference of BankMain you would be able to access cardNum like this
Class BankMain {
public ArrayList<String> cardNum = new ArrayList<String>();
}
Class BankMain2 extends BankMain {
public void method() {
BankMain2 main = new BankMain2();
sysout(main.cardNum.size());
}
}
but the above scenario would only work when cardNum ArrayList in BankMain class is either marked public,protected or default(Nomodifier). it wouldnt work if its marked as private and other non access modifier such as static and final

You can try any one of these
1.Declare the Arraylist as public then import the first class and use the cardNum in the second class
2.Make the cardNum a static var and use it directly in second class as BankMain.cardNum
3.Pass the Arraylist as argument to the second class.

The key problem is in the the way you are trying to create your classes. Your current problem can be solved by answer given by #MadProgrammer. But you should definitly have a look into the Object Oriented Programming Concepts. This section on How to identify and design a Class? should give you some clear pointers.

Related

call variable from another class

static int input;
Scanner scn = new Scanner(System.in);
public BusGenerator(Depot depot)
{
this.depot = depot;
}
public int getinput()
{
return input;
}
public void run()
{
System.out.println("Enter Number of Buses:" );
input = scn.nextInt();
}
I have a class called BusGenerator and from here i ask the user about the number of Bus and the system scans it and save it in the variable called "Input".
I have another class called Depot and i want to call the variable "Input" from the Class depot. Is there a way to do that?
As the code is, you can simply use BusGenerator.input in the Depot class to refer to it (as pointed out in the comments).
However, since you've already defined a getter for this variable, it might be more consistent to make input private and refer to it with the public getter/setter methods.

How do I know what type of variable to use for an instance of an object?

package RPG;
import java.util.Random;
import java.util.Scanner;
public class Beginnings
{
public static boid main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Pick a class:");
System.out.println("Fighter: Deals more damage");
System.out.println("Mage: Has more health");
System.out.println("Theif: Deals less damage but has more health");
String Class = scan.nextLine();
if (Class.matches(".*Fighter.*"));
Fighter user = new Fighter();
else if (Class.matches(".*Mage.*"));
Mage user = new Mage();
else if (Class.matches(".*Thief.*"));
Thief user = new Thief();
else
Human user = new Human();
user.Name();
user.Explore();
/*When I run the code, it highlights the first "user" and says "variable declaration not allowed here"
I don't know how to fix this.
*/
You've got a few spelling mistakes that might be causing problems
public static boid main (String[] args)
should be
public static void main (String[] args)
and
System.out.println("Theif: Deals less damage but has more health");
should probably be
System.out.println("Thief: Deals less damage but has more health");
Futhermore, capital case Class() and lowercase class are already things in the Java SDK as well. You should use a different variable name. I've used characterStr in my example below. Standard practice dictates that you should use camelcase for naming variables and upper case for naming Java classes. When instantiating a Class() object, people generally instantiate it as Class clazz = new Class().
Moving forward, I think you should create a super class for your different User Classes. Set up something like this:
public abstract class Character
public class Fighter extends Character
public class Thief extends Character
public class Mage extends Character
public class Human extends Character
This way, the code in your main() can use your Character() object
public static void main(String[] args)
{
// Prompt the user for the character they want
Scanner scan = new Scanner(System.in);
System.out.println("Pick a class:");
System.out.println("Fighter: Deals more damage");
System.out.println("Mage: Has more health");
System.out.println("Theif: Deals less damage but has more health");
// Get the user's response
String characterStr = scan.nextLine();
Character user; // You might have to initialize this. Did not test
if (characterStr.matches(".*Fighter.*"))
{
user = new Fighter();
}
else if (characterStr.matches(".*Mage.*"));
{
user = new Mage();
}
else if (characterStr.matches(".*Thief.*"));
{
user = new Thief();
}
else
{
user = new Human();
}
user.Name();
user.Explore();
}
First problem : You cannot declare a variable in the then of the if statement if you don't put the declaration between parenthesis. The compiler doesn't accept it.
Second problem : you should declare a single variable before the if-else-if if you want at the end call a method on the instantiated instance as you do :
user.Name();
user.Explore();
In each if you should instantiate the suitable child User class and assign the it to the user variable declared above.
In Java, method should begin with lowercase. I modified it.
The code supposes that User is the parent class and class you instantiate in condition statements are subclasses of User.
User should define a name() method and a explore() method.
User user = null;
if (Class.matches(".*Fighter.*")){
user = new Fighter();
}
else if (Class.matches(".*Mage.*")){
user = new Mage();
}
else if (Class.matches(".*Thief.*")){
user = new Thief();
}
else{
user = new Human();
}
user.name();
user.explore();
Assuming the 'user' is a player who has a 'class' (ie fighter / mage). I would make a separate player class and store that information.
class Player {
String classType; // where you would store the player's class information
int hitPoints;
}
The type of the variable has to be a common base class of all objects you potentially assign to it. Since the class Object is a base class of all other classes, you could do this:
Object user = new Fighter();
But it is probably not a good idea.
You should probably derive Fighter, Mage etc. from a base class, say Character. Instead of a base class you can also use an interface.

Call method from main and pass Array from one class to another

I am facing a roadblock with static methods.
How do I call this method ?
How do Pass the array to another class , so I can edit the array.
Thank you
import java.util.Scanner;
class getArray {
public static void change(String x[]){
Scanner keyboard = new Scanner(System.in);
String dayName[] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
String[] day = new String[7];
for(int i=0;i<7;i++){
System.out.print(dayName[i]+ " ");
day[i] = keyboard.nextLine();
String str = (dayName[i]+" "+day[i]);
x[i] = str;
}
System.out.println(" ");
for(int j=0;j<7;j++){
System.out.println(x[j]);
}
}
}
class toParse{//would parse the integer out from String[x]
}
class averageTemp{//calculate average of weather
}
public class UniSeven2 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
}
As pointed out in the comment, try not using static method. That said:
import java.util.Scanner;
class getArray {
public String change(String x[]){
Scanner keyboard = new Scanner(System.in);
String dayName[] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
String[] day = new String[7];
String str;
for(int i=0;i<7;i++){
System.out.print(dayName[i]+ " ");
day[i] = keyboard.nextLine();
str = (dayName[i]+" "+day[i]);
x[i] = str;
}
return str;
}
}
You can now access the "modified" String from whatever class you want.
Static methods belong to the Class itself, rather than the instances created by the class. Hence, when calling static method from outside it's class you would call it at the class level.
So, say we have the following..
MyClass {
public static void myStaticMethod(int[] myIntArray) {
//Do something
}
public void myNonStaticMehtod(int[] myIntArray) {
//Do something
}
}
When calling these methods from another class they would be called in the following ways..
Non static method
Because the non static method belongs to a given instance of a class, we must first make an instance of the class and call the method from the instance.
int[] myIntArray = new int[4];
MyClass myClass = new MyClass();
myClass.myNonStaticMethod(myIntArray);
Static method
The Static method belongs to the Class itself and should be called from the class level rather than from an instance of the class.
int[] myIntArray = new int[4];
MyClass.myStaticMethod(myIntArray);
On an unrelated note, the classes you are creating seem that they should be methods instead. Thinking in terms of object oriented programming, a class is the framework for an object, such as a car or person. The methods of that class should represent some action that these objects can perform. for example car.speedUp() speeds up a car, or car.getSpeed(), which gets the current speed of the car.
Hope this was helpful!
Edit: Added arrays as method parameters to help answer your second question.

Where to put methods scanner concretely(Java)

so my question is very short, I have a java constructor, and a java class that has to use the constructor to build an object.I need to ask the user for arguments that are required to build the object.Normally, do I put the required scanner(to make user input arguments) in the correct constructor methods or I ask these directly in the class methods thats use the constructor?For example, having construc.java(wich is the constructor)and contains methods like:¸
public void setNumber(int JNumber){
if(JNumber>=0){
Number = JNumber;
and a file called caller.java thats contain methods like:
public void add();
construc test = new construc(string,int,int,string,string); //instance to use the constructor methods
So basically im wondering where to put this code part that ask for the number to assign the the object:
Scanner thenum = new Scanner(System.in);
System.out.print("Entrez la quantité: \n");
int ob1num = thenum.nextInt();
ob1num = JNumber;
setNumber(JNumber);
I am a little bit confuse in Java(and beginner).Thank you!
This depends on how you want to use your setNumber() method. If you also want to use it to set numbers that are not based on the users input, putting the Scanner outside of the method is advised. Personally I would have the Scanner outside of the method to make which would make the method more versatile. If you need to scan multiple numbers, maybe put the scanning part in its own method that returns an int based on the users input.
class YourClass {
YourClass() {
//Initialize
setNumber();
}
}
public static main(String[] args) {
//Create new YourClass object and set value from user input
YourClass object = new YourClass();
}
With getter and setter methods available in the MyClass.Your main class methods can read values
Scanner thenum = new Scanner(System.in);
System.out.print("Entrez la quantité: \n");
int ob1num = thenum.nextInt();
And pass value to constructor
MyClass(int JNumber,String JString){
this.JNumber = JNumber;
this.JString = JString;
}

Running totals to work throughout classes in java?

I am current writing a program that includes a test. When the user clicks submit it either prints out correct or incorrect and then goes to a different class. As well as doing this i want if the answer is correct to add 1 to a variable.
The thing i can't work out is how to do this in different classes since 1 or 0 will need to be added for everything question which are saved in different classes but in the same project.
Is there any reason that each question is a separate class? It seems that you could have a single Question class which hold instance variables, such as
public class Question{
private String text; //the question itself
private String[] choices; //the choices if this is a multiple-choice question
private int answer; //the index in choices that is the correct answer
//constructor, accessors, mutators
public String toString(){
String retval = this.text+"\n";
for(int x=0;x<choices.length;x++){
char c = 'a'+x; //this will give characters going alphabetically from 'a'
retval+=c+") "+choices[x]+"\n";
}
return retval;
}
}
Then you could have a Test class with the main method.
public class Test{
public static void main(String args[]){
Question[] questions = {
new Question("What is 1+1?", new String[]{"2", "3", "4"}, 0),
//other questions here
}
int total=0;
Scanner input = new Scanner(System.in);
for(Question q: questions){
System.out.println(q.toString());
int ans = input.nextLine().charAt(0)-'a';
if(q.getAnswer()==ans){
total++;
}
}
}
}
Does this sort of do what you want?
This counter does not have a context within each of the individual classes. It only has context within the code you have managing these tests you are running. So, within this manager class, you have one variable that you increment each time a test completes and you detect it to be correct.
You want a different class with a public final static class and variable.
Something like this:
public class Counter {
private static int count=0;
public static int add() {
return count++;
}
}
You may want a getter as well.
Whatever class has a reference to the questions should loop through them and sum up a total of correct questions. If your questions don't inherit from the same class create an interface named Question that has a isAnswerRight() method that you can call, or something similar.

Categories