Reputation: 63
Please help to resolve the below inquiry. There are 2 classes (Main & Account)
In main class, I am getting scanner input value and passing this value as a integer array to account class. In Account class, I need to read this array and display the values.
Main class and Main function:(I need to get input one by one and display the appended values)
System.out.println("Enter the array value");
Array arrayobj = new Array();
int value = scan.nextInt();
Integer[] valuearr = new Integer[1];
valuearr[0] = value;
arrayobj.add(valuearr);
Account class : (Add method)
public void add(Integer[] a) {
System.out.println("The values are")
}
*My input and output should be like this..
##*
Enter your choice:
1. Add
1 ----------------------selecting 1 option
Enter the array value
1
The values are (Output)
1
Do you want to continue?
Yes
Enter your choice:
1. Add
1 ----------------------selecting 1 option
Enter the array value
2
The values are (Output)
1
2 ##
Upvotes: 0
Views: 50
Reputation: 1375
May be this will help you. You are trying to add an array of size 1 to a list. You must have a global list in account class to where you add numbers into. And, values must be printed from it. If you want an array to come from main class, you can use add all as below.
class Account {
ArrayList<Integer> numbers= new ArrayList<>();
public void add(Integer[] a) {
numbers.addAll(Arrays.asList(a));
System.out.println("The values are")
for (Integer num : numbers) {
System.out.println(num);
}
}
}
If you want to just append a number and print, can use numbers.add(int_value);
Upvotes: 1
Reputation: 251
I recommend you to use arrayList instead of array
ArrayList<Integer> arrayobj = new ArrayList<Integer>();
Add and print function would be.
public void add(Integer[] a) {
arrayobj.add(a);
System.out.println("The values are:"+arrayobj );}
Upvotes: 0