read multiple inputs from same line in java - java

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()

Related

passing an array as an argument; Setting up Array in Java with user input with Scanner Class

I am trying to take user input, place it into my array, display the array and then print all the values larger than the "n" values the user provides. I think I am close, but I can't get the user input to go to the array. I keep getting an error in eclipse when I call the method (main at very bottom) the "arrayValues" cannot be resolved to a variable:
import java.util.Arrays;
import java.util.Scanner;
public class LargerThanN {
//initialize n
static int n;
static int arraySize;
//setup the array
static int [] integerArray = new int [] {};
public static void printGreaterThanN(int[] integerArray, int n) {
for (int i = 0; i < integerArray.length; i++) {
if (integerArray[i]>n) {
System.out.println(integerArray[i]);
}
}
}
public static int[] fillArrayWithUserInt() {
Scanner sc = new Scanner(System.in);
System.out.println("How big will the array be?");
int arraySize = sc.nextInt();
sc.nextLine(); // clears rest of input, including carriage return
int[] integerArray = new int[arraySize];
System.out.println("Enter the " + arraySize + " numbers now.");
for (int i = 0; i < integerArray.length; i++) {
integerArray[i] = sc.nextInt();
}
return integerArray;
}
/**
* This method prints the array to the standard output
* #param array
*/
private static void displayArray( int[] integerArray) {
for (int i = `0; i < integerArray.length; i++) {
System.out.print(integerArray[i] + " ");
}
}
public static void main(String[] args) {
int [] array ;
array = fillArrayWithUserInt();
Scanner sc = new Scanner(System.in);
fillArrayWithUserInt();
displayArray(array);
System.out.println("To which number would you like to compare the rest? Your n value is: ");
n = sc.nextInt();
printGreaterThanN(array, n);
but now my output looks like:
How big will the array be?
4
Enter the 4 numbers now.
1 2 3 4
How big will the array be?
3
Enter the 3 numbers now.
1 2 3
1 2 3 4
To which number would you like to compare the rest? Your n value is:
2
3
4
Heads up, the following code does nothing in java...
public void set(int n, int value) {
n = value;
}
You seem to written code like this in many functions where a value should be returned.
For example, the function definition :
static void fillArrayWithUserInt(int[] integerArray, int arraySize, int arrayValues, int n)
Should really be written as :
static int[] fillArrayWithUserInt()
It could be implemented as follows
public static int[] fillArrayWithUserInt() {
Scanner sc = new Scanner(System.in);
System.out.println("How big will the array be?");
int arraySize = sc.nextInt();
sc.nextLine(); // clears rest of input, including carriage return
int[] integerArray = new int[arraySize];
System.out.println("Enter the " + arraySize + " numbers now.");
System.out.println("What are the numbers in your array?");
for (int i = 0; i < integerArray.length; i++) {
integerArray[i] = sc.nextInt();
}
return integerArray;
}
The above function will ask the user for the array size. Create the array with the given size. Then prompt the user to fill the array with the correct number of values. The array created in this process is then returned.
All you must handle differently now is finding the value to compare. This must be done outside the fillArrayWithUserInt function.
Like so :
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] array = fillArrayWithUserInt();
displayArray(array);
System.out.println("To which number would you like to compare the rest? Your n value is: ");
int n = sc.nextInt();
printGreaterThanN(array, n);
}
Lastly, you should not need to declare any static variables at the top of your class.
These lines can all be deleted :
//initialize n
static int n;
static int arraySize;
//setup the array
static int [] integerArray = new int [] {};
Here is my solution check it out.
import java.util.Scanner;
public class LargerThanN {
static int[] integerArray = null;
static int n ;
public static void printGreaterThanN() {
for (int i = 0; i < integerArray.length; i++) {
if (integerArray[i] > n) {
System.out.println(integerArray[i]);
}
}
}
public static void fillArrayWithUserInt() {
Scanner sc = new Scanner(System.in);
System.out.println("How big will the array be?");
int arraySize = sc.nextInt();
sc.nextLine(); // clears rest of input, including carriage return
integerArray = new int[arraySize];
System.out.println("Enter the " + arraySize + " numbers now.");
System.out.println("What are the numbers in your array?");
for (int i = 0; i < integerArray.length; i++) {
integerArray[i] = sc.nextInt();
}
System.out.println("To which number would you like to compare the rest? Your n value is: ");
n = sc.nextInt();
}
/**
* This method prints the array to the standard output
*
* #param array
*/
private static void displayArray() {
for (int i = 0; i < integerArray.length; i++) {
System.out.print(integerArray[i] + " ");
}
}
public static void main(String[] args) {
fillArrayWithUserInt();
displayArray();
printGreaterThanN();
}
}

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]);
}
}
}

Java how to read a certain Number and display it as a figure

RightTriangle.java: Write code that reads in a number R from the user, and displays a figure with R rows of "$" characters as the following pattern. For instance, if the user enters a 4 for R, your program should display:
$$$$
$$$
$$
$
Heres my code currently.
import java.util.Scanner;
public class RightTriangle {
public static void main(String[] args) {
int R;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number: ");
R = sc.nextInt($);
System.out.println(R);
}
}
You could solve this task like so:
import java.util.Scanner;
public class triangle{
public static void main(String[] args){
int R;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number: ");
R = sc.nextInt();
int k = R;
for(int i=0; i<R; i++){
for(int j=k; j>0; j--){
System.out.print('$');
}
k = k - 1;
System.out.print('\n');
}
}
}
We use two for loops. The first for loop is used to print a newline after the nested for loop printed the correct amount of $ for that line. Note how we decrease the value of the inner loop counter inside the outer for loop to decrease the amount of $ printed each line.
Use a descending for-loop with the input as the index.
In each iteration, print the $ symbol i times. You could do this using a loop or using another way.
EDIT:
import java.util.Scanner;
public class RightTriangle {
public static void main(String[] args) {
int R;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number: ");
R = sc.nextInt(10);
for (int i = R; i >0; i--) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < i; j++) {
sb.append("$");
}
System.out.println(sb.toString());
}
}
}
A little late, but here it is anyway :)

comparision b/w elements of an array

in this code i want to take no. of testcases by keyboard input and rest is the same problem. only what i am doing is like. if i take 2 test cases then it shud be print the result based on both cases after taking the complete input. For example: INPUT testcases : 2 //case1// 5(no of building) 7 5 2 11 1 //case2// 3(no. of building) 1 2 3 OUTPUT 7//OUTPUT FOR 1ST CASE// 0//OUTPUT FOR 2ND CASE// HOPE, NOW PROBLEM IS CLEAR
import java.util.Scanner;
public class Komal {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("enter the test cases");
int T=sc.nextInt();
for(int i=1;i<=T;i++)
{
System.out.println("total no of building");
int n=sc.nextInt();
int ar[]=new int[n];
for(int j=0;j<n;j++)
{
System.out.println("enter the heights");
ar[j]=sc.nextInt();
}
for(int j=1;j<ar.length;j++)
{int sum=0;
if(ar[0]<ar[i])
{
break;
}else
{
sum += (ar[0]-ar[i]);
}
System.out.println(sum);
}
}
}
}
Here is the complete code. Luckily i had my laptop running and this isn't a very difficult program.
public class TillGreater{
public static void main(String args[]){
int[] ar = {5,4,2,7,1};
int sum=0;
for(int i = 1 ; i < ar.length;i++){
if(ar[0]<ar[i]){
break;
}else{
sum = sum + (ar[0]-ar[i]);
}
}
System.out.println(sum);
}
}
try this:
public static int SumUntilBigger(int[] a)
{
int sum=0;
for(int i=1;i<a.length;i++)
{
if(a[i]<=a[0])
sum+=a[0]-a[i];
else
return sum;
}
return sum;//will reach this statement if all the elements are bigger than the first one
}
this compares each element with the first element and when the element is bigger it just return the sum and exits from method, if all elements are bigger than the first one just return the sum of all differences.
you can use it in main like this:
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("enter the size of the array");
int size=s.nextInt();
int[] a=new int[size];
System.out.println("enter the array values");
for(int i=0;i<size;i++)
a[i]=s.nextInt();
System.out.println("The differences sum : "+SumUntilBigger(a));
}

Small addition to current code

import java.util.Scanner;
public class linecounter {
public static void main(String[] args) {
System.out.print("Enter a line of integers ");
Scanner chopper = new Scanner(System.in);
int x = chopper.nextInt();
while (chopper.hasNextInt()) {
System.out.println(chopper.nextInt());
}
}
}
I am in a CS1 class learning the basics of Java and have a quick question, on this code could anyone tell me how i could get it to keep count of how many integers were typed in?
Thank you
above your while loop, declare:
int count = 0;
then in your while loop use
count++;
This will start you at 0 and every time it increments the count
You could add a counter to the while loop.
int counter = 0;
while (chopper.hasNextInt()) {
counter++;
System.out.println(chopper.nextInt());
}
System.out.println(counter);
In the cases that you have integer numbers, double numbers and you only need count the integer numbers, you can use:
public class linecounter {
public static void main(String[] args) {
System.out.print("Enter a line of integers ");
Scanner chopper = new Scanner(System.in);
int x = chopper.nextInt();
int counter = 0;
while (chopper.hasNextInt()) {
System.out.println(chopper.nextInt());
String myCurrentArg = chopper.nextInt();
if(isInteger(myCurrentArg) ){
counter++;
}
}
System.out.println("The number of integer arguments are: " + counter);
}
public static boolean isInteger(String s) {
return isInteger(s,10);
}
}

Categories

Resources