combustion007
asked on
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.
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);
}
}
the above code will let you get 20 numbers exactly
ASKER
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.
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);
}
}
error1.txt
ASKER
Any help would highly be appreciated.
Thanks.
Thanks.
oops the code should be like
ArrayList is is collection class
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);
}
}
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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);
}