java accept user input to 5 integers - java

Hi how in java we can limit numbers of integers a certain user input? Below is my code of accepting user inputs, how can i ensure that only accept 5 integers from users? Thanks
public static void main(String args[])
{
int a;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string");
a = in.nextInt();
System.out.println("You entered integer "+a);
}

public static void main(String args[])
{
int a[] = new int[5];
int i = 0;
Scanner in = new Scanner(System.in);
while(i<5)
{
System.out.println("Enter an integer");
a[i] = in.nextInt();
i++;
System.out.println("You entered integer "+a[i-1]);
}
}
For a[i] to be between 1 and 100
public static void main(String args[])
{
int a[] = new int[5];
int i = 0;
Scanner in = new Scanner(System.in);
while(i<5)
{
System.out.println("Enter an integer between 1 and 100");
a[i] = in.nextInt();
if(a[i]>=1 && a[i]<=100)
{
i++;
System.out.println("You entered integer "+a[i-1]);
}
}
}

Use a for loop that iterates only 5 times and take the input inside that for loop.

Declare a static field as count=0,
Check whether a>0
If it is , increment count by 1
When count is already 5, dont let the user input anything

Related

How to use a loop where integers are inputted?

I'm working on a question and I'm new to programming, so I'm not that familiar with a few concepts. The question asks the user to input an initial number, followed by a list of that many numbers. The program should then print back how many of the numbers entered were negative.
For example, I first input 5, followed by 5 other random numbers.
5
6,-9,28,-32,-1
The output should be
3
So far all I have is:
class main
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int input = scan.nextInt();
int c=0;
for(int i = 1; i <= input; i++)
{
System.out.println(i);
if(i<0)
{
c++;
}
}
System.out.println(c);
}
}
I'm really confused. Can someone offer an explanation as to how the code works?
You can read the positive integers inside for loop from the given inputs and then check if that each input integer is greater than or equal to zero:
scanner scan = new Scanner(System.in);
int input = scan.nextInt();
int c=0;
for(int i = 1; i <= input; i++) {
int num = scan.nextInt();
if(num>=0)
{
System.out.println(num);
}
}

How do I enter values from a loop into an array list?

this is my code:
public static void main(String[] args) {
System.out.println("Please enter the number of values you would like to enter: ");
Scanner scan = new Scanner(System.in);
int intNumberOfNumbers = scan.nextInt();
for (int i = 0; i < intNumberOfNumbers; i++) {
System.out.println("Please enter a value for index " + i + ":");
int intValue = scan.nextInt();
}
}
What I'm trying to do is create a scanner that asks how many values they want to enter and whatever that value is, that's how many times it asks for number input. The problem is after I ask the question how can I add the number to an array list?
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int numOfInput=sc.nextInt();
ArrayList<Integer> array=new ArrayList<Integer>();
while(numOfInput-->0){
array.add(sc.nextInt());
}
}
public static void main(String[] args) {
System.out.println("Please enter the number of values you would like to enter: ");
Scanner scan = new Scanner(System.in);
int intNumberOfNumbers = scan.nextInt();
ArrayList<Integer> myArray= new ArrayList<>();
for (int i = 0; i < intNumberOfNumbers; i++) {
System.out.println("Please enter a value for index " + i + ":");
int intValue = scan.nextInt();
myArray.add(intValue);
}
}

Java Scanner Continuous User input?

For java practice, i am trying to create a program that reads integers from the keyboard until a negative one is entered.
and it prints the maximum and minimum of the integer ignoring the negative.
Is there a way to have continuous input in the same program once it runs? I have to keep running the program each time to enter a number.
Any help would be appreciated
public class CS {
public static void main(String []args) {
Scanner keys = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = keys.nextInt();
while(true)
{
if(n>0)
{
System.out.println("Enter again: ");
n = keys.nextInt();
}
else
{
System.out.println("Number is negative! System Shutdown!");
System.exit(1);
}
}
}
}
Here is a part of my code - It works, but i think there is an easier way of doing what i want but not sure how!
import java.util.Scanner;
public class ABC {
public static void main(String []args) {
int num;
Scanner scanner = new Scanner(System.in);
System.out.println("Feed me with numbers!");
while((num = scanner.nextInt()) > 0) {
System.out.println("Keep Going!");
}
{
System.out.println("Number is negative! System Shutdown!");
System.exit(1);
}
}
}
You could do something like:
Scanner input = new Scanner(System.in);
int num;
while((num = input.nextInt()) >= 0) {
//do something
}
This will make num equal to the next integer, and check if it is greater than 0. If it's negative, it will fall out of the loop.
A simple loop can solve your problem.
Scanner s = new Scanner(System.in);
int num = 1;
while(num>0)
{
num = s.nextInt();
//Do whatever you want with the number
}
The above loop will run until a negative number is met.
I hope this helps you

Program that reads num and prints from 1 to num

i need help in logic, i need the program to read an integer from user and then prints all the integers from 1 to num1. here's what i got :
import java.util.Scanner;
public class test
{
public static void main(String []args)
{
Scanner scan = new Scanner(System.in);
int num1;
int num2;
System.out.println("Enter any number:");
num1 = scan.nextInt();
while (num1<=num2) {
System.out.println(num+1);
}
}
}
Try this out:
import java.util.Scanner;
class test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number;
System.out.println("Enter any number:");
// Note : The below statement will fail if user does not enter integer value
number = scan.nextInt();
// You can use while loop as well but for loop provides cleaner approach for iteration
for (int i = 1; i <= number; i++) {
// Print numbers sequentially from 1 to number
System.out.println(i);
}
}
}
Program that reads num and prints from 1 to num
Try,
System.out.println("Enter any number:");
num1 = scan.nextInt();
int i=1;
while (i<=num1) {
System.out.println(i);
i++;
}
do like this
int num1=0;
int num2=0;
System.out.println("Enter any number:");
num1 = scan.nextInt();
while (num2 <= num1) {
System.out.println(num2);
num2++;
}
thanks alot guys! its been couple of years since last i coded a java program so im a little rusty! here's my final code :
import java.util.Scanner;
public class test
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
int num;
int a=1;
System.out.println("Enter any number:");
num=scan.nextInt();
while (a<=num)
{
System.out.println(a);
a++;}
}}

need scanner input for array

does anyone know how to set a user input for an array, I cant find the command anywhere. my array 'grades' have 20 locations. im not so sure about 'grades.length' function but I think it prompts 20 times. BUT I added a while statement to override BUT ITS TOTALLY IGNORING THE FOR STATEMENT. if I could set user input for array I could get rid of the while statement...
program has to accept grade for number of students the user inputs btw..
import java.util.Scanner;
public class gradesaverage {
public static void main(String[] args) {
int [] grades = new int [20];
int i;
int numStudents;
System.out.print("Enter number of students: ");
Scanner scanint = new Scanner (System.in);
numStudents = scanint.nextInt();
for ( i = 1; i <= grades.length; ++i)
{
System.out.println("Enter grade: ");
grades[i] = scanint.nextInt();
}
while(i <= numStudents );
}
}
Not sure what you mean, but assuming all input is correct,
int [] grades = new int [numStudents ];
Should work if you move this line after declaration and assignment of numStudents. There is no problem in java with variable length arrays.
Also note - your iterator i starts from 1, while in java arrays start from 0.
public static void main(String[] args) {
int i;
int numStudents;
System.out.print("Enter number of students: ");
Scanner scanint = new Scanner (System.in);
numStudents = scanint.nextInt();
int [] grades = new int [numStudents]; //the size we wanted
for ( i = 0; i < grades.length; ++i) //starting from 0, not 1.
{
System.out.println("Enter grade: ");
grades[i] = scanint.nextInt();
}
//print the array - for checking out everyting is ok
System.out.println(Arrays.toString(grades));
}

Categories

Resources