How to Put a Scanner Input into an Array... for Example a Couple of Numbers

How to put a Scanner input into an array... for example a couple of numbers

You could try something like this:

public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
double[] numbers = new double[5];

for (int i = 0; i < numbers.length; i++)
{
System.out.println("Please enter number");
numbers[i] = input.nextDouble();
}
}

It seems pretty basic stuff unless I am misunderstanding you

How to use java.util.Scanner class to read input?

Check this out

 public static void main(String args[]) {

Scanner sc = new Scanner (System.in);
System.out.println ("Type nine float numbers two-dimensional array of similar type and size with line breaks, end by -1:");
float[][] a = new float[3][3];
for (int i=0; i<3; i++){
for (int j=0; j<3; j++){
String line = sc.nextLine();
if ("-1".equals(line)){
break;
}
a[i][j]=Float.parseFloat(line);
}
}

System.out.println("\n The result is:");

try {
float b[][] = clone(a);
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
} catch (Exception e) {
System.out.println("Error!!!");
}
}

You should define a new Scanner (sc) and then loop 3 x 3 times until you read all input.
If the user enters -1 the loop ends. Note that you do not need to exit when 9 numbers are entered.

Also each user input is read as a line and not as a token, and then is parsed into a Float. If the user enters a String that is not a float number then it throws an Exception.

Sample:

Type nine float numbers two-dimensional array of similar type and size with line breaks, end by -1:
1.1
1.2
1.3
2.1
2.2
2.3
3.1
3.2
3.3

The result is:
1.1 1.2 1.3
2.1 2.2 2.3
3.1 3.2 3.3

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

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 [] {};

using scanner to input data into an array

however if my array had 1000 numbers, this process would have become tiresome. Is there an easy way to input numbers without typing input scanner for each package

Yes, by using loops. Use a for-loop or a while loop.

int[] array = new int[5];
for(int x=0; x<array.length; x++) //Prompt as many times as the array size
array[x] = scanner.nextInt();

Generally, use a for-loop when you are certain how many times it will iterate, and a while loop when you are not certain how many times the loop would run.

Best way to add an input enter part(scanf) into a java function returns array

I'd recommend a re-evaluation of your general design before adding user input. Using a global array variable is unnecessary and arbitrarily restricts the ability of the max function to perform work on anything other than the hard-coded global array. max should accept a parameter array to operate on; this enables reusability.

Your max function's logic seems accurate but will crash the program on empty input arrays.

When handling user input, you may want to be able to respond to variable sizes. For this, using an ArrayList is the easiest way to go.

I also recommend initializing loop counters such as i in loop scope.

Here's a possible rewrite on the way to dynamic arrays that you can use:

import java.util.*;

class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int arr[] = new int[8];

for (int i = 0; i < arr.length; i++) {
System.out.print("Enter an integer: ");
arr[i] = in.nextInt();
}

System.out.println("Largest in given array is " + max(arr));
}

public static int max(int arr[]) {
if (arr.length == 0) {
return -1;
}

int max = arr[0];

for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}

return max;
}
}

Output:

Enter an integer:  1
Enter an integer: 2
Enter an integer: 3
Enter an integer: 6
Enter an integer: 5
Enter an integer: 3
Enter an integer: 4
Enter an integer: 1
Largest in given array is 6

How do I read in values for the elements of an array from a Scanner passed in?

'read in values for the dates in the array from the scanner passed in' simply means to pass in an already instantiated scanner object into a method and use it to read in user input.

The method signature may look like this:

public boolean doTheThing(Scanner sc){
//Use a loop to read in user input with sc
//return true/false
}

The "passed in" part refers to the Scanner sc parameter of the method signature.

Let's take a look at the javadocs for scanner:

  • https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

Of particular interest are:

  1. hasNextInt()
  2. nextInt()

With hasNextInt() you can check your input stream scanner has an int. Then you can use nextInt() to grab it. This only works for getting one such int, so you'll need to encase it into a loop.

I hope this helps!

Passing an uncertain amount of Int's through an array

Instead of this (which creates a storeGrades array with no grades):

    int sum = 0;

for (int i = 0; i < numberOfGrades; i++) {
System.out.println("Please enter grade " + (i + 1) + ": ");
int userGradeNumbers = keyboard.nextInt();
sum += userGradeNumbers;
}

int storeGrades[] = new int[userGradeNumbers];

do this:

    int storeGrades[] = new int[numberOfGrades];
int sum = 0;

for (int i = 0; i < numberOfGrades; i++) {
System.out.println("Please enter grade " + (i + 1) + ": ");
int grade = keyboard.nextInt();
storeGrades[i] = grade;
sum += grade;
}

After the above for-loop, storeGrades will contain the individual grades.



Related Topics



Leave a reply



Submit