We help IT Professionals succeed at work.

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

combustion007
on
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

Comment
Watch Question

CERTIFIED EXPERT

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

      }
CERTIFIED EXPERT

Commented:
the above code will let you get 20 numbers exactly

Author

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

Author

Commented:
Any help would highly be appreciated.

Thanks.
CERTIFIED EXPERT

Commented:
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

Author

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.
CERTIFIED EXPERT
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

Explore More ContentExplore courses, solutions, and other research materials related to this topic.