0

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 ##
4
  • Please add a tag so we know which language you are using. Commented Sep 4, 2018 at 1:06
  • Hi, Thanks so much for the response. I wanted to use Array not list in my problem statement. Kindly help me if there is anyway to print as an array Commented Sep 4, 2018 at 2:36
  • @Pavithra why are you using an integer array to send only 1 number? Commented Sep 4, 2018 at 5:37
  • Hi Gimhani, I have a problem statement to send as an array not as a value. Commented Sep 4, 2018 at 13:08

2 Answers 2

1

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);

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Gimhani,Thanks for the response. I have tried the above code in Account class but i was able to print only one value and its not printed the previously entered values. Is it due to the array size is 1 in main class as following : 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);
@Pavithra Hi why are you using an Array type object? I thought you need to have an Account object and then call its add method.
0

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 );}

1 Comment

Hi Orgil, Thanks so much for the response. I have added array to the list as you advised but i was able to print the values whatever i entered and its not printed the previous entered values. public void add(Integer[] a) { for(int i=0;i<a.length;i++) { list.add(a[i]); } System.out.println("The values are"); for(Integer result : list) System.out.println(result);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.