How can I count the user input with a loop using console...plz?

Hello Experts,

I have done lot of search have yet come across a reference regarding this question that I am stuck on. I would highly appreciate any input or help on this.

I am asking user to type in 20 numbers when prompted. How can I throw an error if number count is less than or more than 20. I tried using the while and do while and both don't pan out the way I expected. Here is my logic:

Thanks a million.
import java.util.Scanner;
 
public class CountNumbers
 
{
	public static void main( String args[] )
 
	{
	Scanner input = new Scanner( System.in );
 
		int count = 0;
 
		System.out.println("Please Enter Numbers");
 
		do
		{
			System.out.println("Please Enter Numbers");
			count++;
		}
		while(count < 21);
	}
}

Open in new window

combustion007Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

contactkarthiCommented:
public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            ArrayList al = new ArrayList();
            for (int i = 0; i < 20; i++) {
                  int int_val = scanner.nextInt();
                  al.add(int_val + "");
            }
            System.out.println(al);

      }
0
contactkarthiCommented:
the above code will let you get 20 numbers exactly
0
combustion007Author Commented:
Dear Friend,

Thank you very much for your help. I have tried your code and am getting Error. Please see the attached file. Error is about finding the add. method not found. Appreciate all your help.

Thanks a lot.


import java.util.Scanner;
import java.io.*;
 
public class ArrayList
 
{
	public static void main(String[] args)
	{
	            Scanner scanner = new Scanner(System.in);
	           ArrayList al = new ArrayList();
	            for (int i = 0; i < 20; i++)
	            {
	                  int int_val = scanner.nextInt();
	                  al.add(int_val + "");
	            }
	            System.out.println(al);
 
	 }
 }

Open in new window

error1.txt
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

combustion007Author Commented:
Any help would highly be appreciated.

Thanks.
0
contactkarthiCommented:
oops the code should be like

ArrayList is is collection class
import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;
 
public class CountNumbers
 
{
	public static void main(String[] args)
	{
	            Scanner scanner = new Scanner(System.in);
	           ArrayList al = new ArrayList();
	            for (int i = 0; i < 20; i++)
	            {
	                  int int_val = scanner.nextInt();
	                  al.add(int_val + "");
	            }
	            System.out.println(al);
 
	 }
 }

Open in new window

0
combustion007Author Commented:
Hello ContactKarthi,

Thanks a lot for all your help. I tried compiling your suggested code and getting these two messages:

Note: CountNumbers.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

I am unable to compile the class. I am running java 1.6 on Windows XP 64bit.

Thanks a bunch.
0
contactkarthiCommented:
oops.. a it is 1.6 the code should be like (you can specify the type of value in the arraylist  likeArrayList<Integer>)

import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;
 
public class CountNumbers
 
{
	public static void main(String[] args)
	{
	            Scanner scanner = new Scanner(System.in);
		ArrayList<Integer> al = new ArrayList<Integer>();
		for (int i = 0; i < 20; i++) {
			int int_val = scanner.nextInt();
			al.add(int_val);
		}
		System.out.println(al); 
	 }
 }
 
 
 
    

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.