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]);
}
}
}
Related
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);
}
}
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;
}
I'm a beginner in Java and working on a code that first requires user to enter total number of integers and next the integers themselves. Example input is:
4
1 4 3 2
The code will need to reverse the second input to the following:
2 3 4 1
My solution is as follow:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int arr[] = new int[n];
for(int arr_i=0; arr_i < n; arr_i++){
arr[arr_i] = in.nextInt();
}
for(int reverse_i=n-1; reverse_i>=0; reverse_i--){
System.out.print(arr[reverse_i]);
if(reverse_i != 0){System.out.print(" ");}
}
}
My question is related to the code to add a blank space " " in between the printed numbers. I wonder what other way I can use to get this done? Any suggestion is appreciated, thank you.
The easy way to reverse a string is using the StringBuilder class:
One option is to remove the spaces at the end of the string eg. remove last char
package stackoverflow.main;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
StringBuilder sb = new StringBuilder();
for(int arr_i = 0; arr_i < n; arr_i++){
sb.append(in.nextInt());
sb.append(" ");
}
sb.deleteCharAt(sb.length() - 1);
String normal = sb.toString();
String reversed = sb.reverse().toString();
System.out.println("normal: " + normal);
System.out.println("reversed: " + reversed);
}
}
Another option is to check whether you are at the last arr_i of your loop.
If so, then don't add a space
package stackoverflow.main;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
StringBuilder sb = new StringBuilder();
for(int arr_i = 0; arr_i < n; arr_i++){
sb.append(in.nextInt());
if (arr_i != 3
sb.append(" ");
}
String normal = sb.toString();
String reversed = sb.reverse().toString();
System.out.println("normal: " + normal);
System.out.println("reversed: " + reversed);
}
}
First reverse the array and then print it with a for loop.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int arr[] = new int[n];
for(int arr_i=0; arr_i < n; arr_i++)
{
arr[arr_i] = in.nextInt();
}
for(int i = 0; i < arr.length / 2; i++)
{
int temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
for(int i = 0; i < arr.length; i++)
{
System.out.print(arr[i]+" ");
}
}
}
It is all about output formatting. You may use this examples and become familiar with all possible approaches.
Your code can be improved in next two ways :
1) Use \t instead of Empty Space (\t is a tabulation)
2) Create a constant with output format like this private static final String output = "%d " and use it in output line like this : String.format(output, number) where number is your number that should be printed.
Here is my code snippet
public void m1(int a) // a value passed from main
{
for(int i=0;i<a;i++)
{
// Read "a" inputs from the user one by one
}
}
public static void main(String[] args)
{
int a;
// read value of a from user
m1(a)
}
Can U please tell me how to give this input in one line.
Like in the same line we need to provide the value of a and also should take a values from user.
eg:enter code here
a=6. 6 values from user
6 22 33 44 55 66
6 and the 6 inputs from the user should be in the same line (given by the user at the same time).
You could go this way:
public class Number {
static Scanner sc = new Scanner(System.in);
static int arr[];
public static void read(int a)
{
arr = new int[a];
for (int i = 0; i < a; i++) {
arr[i] = sc.nextInt();
}
}
public static void main(String args[]) {
System.out.print("Enter numbers here : ");
int a = sc.nextInt();
read(a);
// printing the array
for (int i = 0; i < a; i++) {
System.out.print(arr[i]+" ");
}
System.out.println("");
}
}
But better and cleaner way will be returning the array from read method:
public class Number {
static Scanner sc = new Scanner(System.in);
public static int[] read(int a)
{
int arr[] = new int[a];
for (int i = 0; i < a; i++) {
arr[i] = sc.nextInt();
}
return arr;
}
public static void main(String args[]) {
System.out.print("Enter numbers here : ");
int a = sc.nextInt();
int numbers[] = read(a);
// printing the numbers array
for (int i = 0; i < a; i++) {
System.out.print(numbers[i]+" ");
}
System.out.println("");
}
}
Input:
Enter numbers here : 4 1 2 3 4
Output:
1 2 3 4
Solve that your problem? I haven't understand exactly what you want, but with this you can do anything, or not?
public static void m1(String[] a) // a value passed from main
{
for (int i = 1; i < a.length; i++) {
// Read "a" inputs from the user one by one
System.out.println(a[i]);
}
}
public static void main(String[] args) {
m1(args);
}
public static void m1(int a) // a value passed from main
{
Scanner scan = new Scanner(System.in);
int arr[] = new int[a];
for(int i=0;i<a;i++)
{
arr[i]=scan.nextInt();
}
}
public static void main(String[] args)
{
int a=(int) 6.6;
// read value of a from user
m1(a);
}
Input:
1 -2 "nonumber" 34
Output:
1
-2
34
Code:
String line = scanner.nextLine();
Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher(line);
while (m.find()) {
System.out.println(m.group());
}
First, a couple of quick pointers:
- Name your methods something more practical than m1()
- Make sure you end your statements with a semi-colon ( e.g m1() )
- You need to define m1() as static, or otherwise instantiate the class which contains m1()
- Learn about Scanners and Arrays; you must import a library to use a Scanner object. ( import java.util.Scanner; )
public static void storeIntegers(int a){
//This is how you declare an array.
int[] someIntArray = new int[a];
//You must create a Scanner object to take in user input.
Scanner userInput = new Scanner(System.in);
for(int i = 0; i < a; i++){
someIntArray[i] = userInput.nextInt();
}
// Just to make sure it worked.
for(int e = 0; e < someIntArray.length; e++){
System.out.println(someIntArray[e]);
}
}// End storeIntegers()
public static void main(String[] args){
Scanner userInput = new Scanner(System.in);
System.out.println("How many numbers?");
int a = userInput.nextInt();
storeIntegers(a);
}// End main()
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
}
}