Input numeric ranges with java.util.Scanner - java

I would input numeric ranges (int arrays with two elements) like this:
Enter a number: 3
Enter a range: -3 5
Enter a range: 0 4
Enter a range: 6 10
I use java.util.Scanner, but I do not know how to proceed:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number = input.nextInt();
for(int i=0;i<number;i++)
String str = input.nextLine();
}
}

I think, you just nead a solution for input a numeric array. Check out this: Java Scanner Array
You can read the whole line, then split it into a String array:
public static void main(String[] args) {
try (var scanner = new Scanner(System.in)) {
System.out.print("Enter the count of arrays: ");
int arrayCount = scanner.nextInt();
scanner.nextLine();
// System.out.print("Enter the length of an array: ");
int arraySize = 2;
int[][] ranges = new int[arrayCount][arraySize];
for (int i = 0; i < arrayCount; i++) {
System.out.printf("[%d] Enter the elements: ", i);
String line = scanner.nextLine();
String[] numbers = line.split("\\D+", arraySize + 1); // separator: 1+ non-digit
for (int j = 0; j < arraySize; j++) {
ranges[i][j] = Integer.parseInt(numbers[j]);
}
}
// Arrays.stream(ranges).map(Arrays::toString).forEach(System.out::println);
}
}
Or simply scan ints one-by-one:
public static void main(String[] args) {
try (var scanner = new Scanner(System.in)) {
System.out.print("Enter the count of arrays: ");
int arrayCount = scanner.nextInt();
// System.out.print("Enter the length of an array: ");
int arraySize = 2;
int[][] ranges = new int[arrayCount][arraySize];
for (int i = 0; i < arrayCount; i++) {
System.out.printf("[%d] Enter the elements: ", i);
for (int j = 0; j < arraySize; j++) {
ranges[i][j] = scanner.nextInt();
}
}
// Arrays.stream(ranges).map(Arrays::toString).forEach(System.out::println);
}
}

Related

Using while loop on a user specified condition

So, this below is my code:
public class StudentRun {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] names = new String[50];
int[] rolls = new int[50];
System.out.print("Do you want to register a student ?(yes/no): ");
String res = sc.nextLine();
while((res.toUpperCase()).equals("YES")) {
System.out.print("Enter the student's name: ");
String n = sc.nextLine();
for(int i=1; i<50; i++) {
names[i] = n;
}
System.out.print("Enter their roll number: ");
int r = sc.nextInt();
for(int j=0; j<50; j++) {
rolls[j] = r;
}
}
for(int a=0; a<50; a++) {
System.out.println(names[a]);
System.out.println(rolls[a]);
}
}
}
What I want is to happen is that, the program should keep registering students name and roll no. until the array is full or the user ends it. How do I do it ? I got that far
You need to have the "continue" question in the while loop, and you don't need the for loop every time you insert a name.
public class StudentRun {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] names = new String[50];
int[] rolls = new int[50];
int index = 0;
while(true) {
System.out.print("Do you want to register a student ?(yes/no): ");
String res = sc.nextLine();
if(res.toUpperCase().equals("NO") || index == 50)
break;
System.out.print("Enter the student's name: ");
String n = sc.nextLine();
names[index] = n;
System.out.print("Enter their roll number: ");
int r = sc.nextInt();
rolls[index] = r;
index++;
}
for(int i=0; i<50; i++) {
System.out.println(names[i]);
System.out.println(rolls[i]);
}
}
}
A common approach when using fixed sized arrays is to use a separate int variable to track the current index position for a new item, as well as the total used slots in the array:
import java.util.*;
class Main {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int size = 50;
String[] names = new String[size];
int[] rolls = new int[size];
int counter = 0;
String res = "";
do {
System.out.print("Do you want to register a student ?(yes/no): ");
res = sc.nextLine().toUpperCase();
if (res.equals("YES")) {
System.out.print("Enter the student's name: ");
names[counter] = sc.nextLine();
System.out.print("Enter their roll number: ");
rolls[counter] = sc.nextInt();
sc.nextLine(); // clear enter out of buffer;
counter++;
}
} while (counter < size && res.equals("YES"));
for(int a=0; a<counter; a++) {
System.out.print(names[a] + " : ");
System.out.println(rolls[a]);
}
}
}

Read, store and print user input

I have to get 4 user input from the user one by one on the next line like
Sample input:
65
66
67
68
Then the output has to displayed like
You have entered:
65-A
66-B
67-C
68-D
the program i have return is this:
import java.util.Scanner;
public class ASCII {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the digits:");
int no = sc.nextInt();
char ch= (char) no;
System.out.println(no + "-" + ch);
}
}
the one thing could not get is the 4 input for the user could someone help with that
You should loop it;
int[] numbers = new int[4];
for (int i = 0; i < 4; i++) {
numbers[i] = sc.nextInt();
}
numbers[n-1] will return number in your case 0 < n < 5;
and you can create another loop to print them.
chars[] characters = {'A','B','C','D'};
for (int i = 0; i < 4; i++) {
System.out.println(Integer.toString(numbers[i]) + characters[i]);
}
for loops works like;
for (DoAtStart; Condition; DoAtEndOfARepeat) {
}
This would work for you :
public class ASCII {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the digits:");
int array[]=new int[4];
for(int i=0; i<4;i++) {
int no = sc.nextInt();
array[i]=no;
}
System.out.println("You have entered:");
for(int j=0;j<array.length;j++) {
char ch= (char) array[j];
System.out.println(ch+"-"+array[j]);
}
}
}

I have to print the array , but for each loop is not working

import java.util.Scanner;
public class TeacherCoins {
public static void main(String[] args){
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k =sc.nextInt();
int [] arr= new int[k-1];
System.out.print("Enter array :");
for(int i=0;i<=(k-1);i++)
{
Scanner sc1 = new Scanner(System.in);
arr[i] =sc1.nextInt();
}
for(int element :arr)
{
System.out.println("Print array");
System.out.println(element);
}
}
I am giving output using Scanner class. But is not printing the array.
You don't need to declare the Scanner again inside the loop. Another thing that you should do to be sure of your code, is to have this condition on the loop if i < arr.length. Lastly, I moved the "Print array" message outside the last for.
public class TeacherCoins {
public static void main(String[] args){
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k =sc.nextInt();
int [] arr = new int[k];
System.out.print("Enter array :");
for(int i=0; i<arr.length; i++) {
arr[i] =sc1.nextInt();
}
System.out.println("Print array");
for(int element :arr){
System.out.println(element);
}
}
}
There are two problems with this code,
1. You don't need to create a new Scanner object for each user input.
2. You are declaring an array size of k-1 and then asking user input k times.
public static void main(String[] args) {
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k = sc.nextInt();
int[] arr = new int[k]; // To store k elements, you need k sized array
System.out.print("Enter array :");
for (int i = 0; i <= (k - 1); i++) {
// Scanner sc1 = new Scanner(System.in); / / Not required here
arr[i] = sc.nextInt();
}
System.out.println("Print array");
for (int element : arr) {
System.out.println(element);
}
}
There is only one problem in your code you are declaring the size of array as k-1 instead declare it for k elements.You just need to give each inputs in new line. For more refer this :Scanner class.
And your for each loop is correct and working.
import java.util.Scanner;
public class TeacherCoins {
public static void main(String[] args){
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k =sc.nextInt();
int [] arr= new int[k];//it was giving java.lang.ArrayIndexOutOfBoundsException
System.out.print("Enter array :");
for(int i=0;i<=(k-1);i++)
{
Scanner sc1 = new Scanner(System.in);//not required
arr[i] =sc1.nextInt();//use arr[i] =sc.nextInt();
}
for(int element :arr)
{
System.out.println("Print array");
System.out.println(element);
}
}
}
Above code will work. But you don't need new scanner objects for taking input, creating only single object will work.
import java.util.Scanner;
public class TeacherCoins {
public static void main(String[] args) {
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k = sc.nextInt();
int[] arr = new int[k];
System.out.print("Enter array :");
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
sc.close();
System.out.println("Print array");
for (int element : arr) {
System.out.println(element);
}
for (int i = 0; i < arr.length; i++) {
int count = 0;
for (int j = 0; i < arr.length; j++) {
arr[i] = arr[j];
count++;
}
System.out.println(arr[i] + " " + count);
}
}
}

Not output of the array value when used scanner

Below is the code. After entering the array, the console just goes blank and does not further output the array:
import java.util.Scanner;
public class advancedArrays {
public static void main(String[] args) {
System.out.println("Provide us the size of the array:");
Scanner scanner = new Scanner(System.in);
int value = scanner.nextInt();
int i = 0;
int[] array = new int[value];
System.out.println("Enter the array:");
Scanner input = new Scanner(System.in);
while(input.hasNextInt()) {
array[i] = input.nextInt();
i++;
}
System.out.println("Array entered:");
for(i=0;i<value;i++)
{
System.out.println(array[i]);
}
input.close();
scanner.close();
}
}
Output:
Provide us the size of the array:
5
Enter the array:
1 2 3 4 5
you are stuck in the "reading the array" part because of this
while (input.hasNextInt()) {
array[i] = input.nextInt();
hint: you know the size of the array, then why don't you do a for loop same as you did for printing out the array's content?? like:
for (int j = 0; j < value; j++) {
array[i] = scanner.nextInt();
i++;
}
The problem is that with the while loop that you keep it waiting for more integers, it is better to convert to a normal for loop with condition on the entered array size value.
Also there is no need to use two scanner objects
public static void main(String[] args) {
System.out.println("Provide us the size of the array:");
Scanner scanner = new Scanner(System.in);
int value = scanner.nextInt();
int i = 0;
int[] array = new int[value];
System.out.println("Enter the array:");
for (int j = 0; j < value; j++) {
if (scanner.hasNextInt()) {
array[i] = scanner.nextInt();
i++;
}
}
System.out.println("Array entered:");
for (i = 0; i < value; i++) {
System.out.println(array[i]);
}
scanner.close();
}
package Main;
import java.util.Scanner;
public class advancedArrays {
public static void main(String[] args) {
System.out.println("Provide us the size of the array:");
Scanner scanner = new Scanner(System.in);
int value = scanner.nextInt();
int i = 0;
int[] array = new int[value];
System.out.println("Enter the array:");
Scanner input = new Scanner(System.in);
while(input.hasNextInt()) {
array[i] = input.nextInt();
i++;
//Changed Code
if (i == value) {
break;
}
}
System.out.println("Array entered:");
for(i=0;i<value;i++)
{
System.out.println(array[i]);
}
input.close();
scanner.close();
}
}
Fixed code, you were stuck reading your inputs for your array because you never checked if your inputs were the length of your array.
Fixed code.
if (i == value) {
break;
}

How do I add user input to an Array in java?

public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
int i;
int n;
String a;
System.out.println("Enter the Class:");
a = user_input.next();
System.out.println("Enter the number of Students:");
n = user_input.nextInt();
for (i= 1; i <= n; i++) {
String g = a + i;
System.out.println(g);
}
}
This is my program. It gets user input for the Class and prints the Roll Number for the students.
For example: If the class is 10A and the number of students is 10, it prints a series like 10A1 , 10A2, 10A3 ... 10A10
How do I get the program to store these as elements in an array?
For example:
array[0] = 10A1;
array[1] = 10A2;
array[2] = 10A3;
etc.
Your code should look like this:
public static void main (String args[])
{
Scanner user_input = new Scanner(System.in);
int i;
int n;
String a;
System.out.println("Enter the Class:");
a = user_input.next();
System.out.println("Enter the number of Students:");
n = user_input.nextInt();
String []strings = new String[n]; // Creating an are of string with the given number
for(i= 0; i < n ;){
strings[i] = a + ++i; // Storing strings on to the array !
System.out.println(strings[i-1]);
}
}
You can just edit each index in your current for loop:
String[] arr;
for(i=0; i < n ; i++){
int j = i+1;
String g = a + j;
System.out.println(g);
arr[i] = g;
}
So all your printed g's will be part of the array arr.
First, declare a String array of the appropriate size.
Second, in your for loop, assign the strings you are currently printing, to positions in the array.
String[] things = new String[n];
for (i=1; i <= n; i++) {
String g = a + i;
System.out.println(g);
things[i-1] = g;
}
The strings are now in an array.
Following code is modified, for storing values in array.
public static void main(String[] args) {
// TODO code application logic here
Scanner user_input = new Scanner(System.in);
int i;
int n;
String a;
System.out.println("Enter the Class:");
a = user_input.next();
System.out.println("Enter the number of Students:");
n = user_input.nextInt();
String[] arr = new String[n]; // create string array of size n.
for(i= 1; i <= n ; i++){
String g = a + i;
System.out.println(g);
arr[i-1]=g; // assign your g veriable vale to array index
}
for(String s : arr){
System.out.println(s); // print your array
}
}

Categories

Resources