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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
contactkarthiConnect With a Mentor Commented:
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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
All Courses

From novice to tech pro — start learning today.