0

So I am super new in terms of coding and java, I just started this week but am already stuck at a pretty simple problem. I am supposed to first check that there are only five integers that the user inputs (on the same line) - which I have tried to do and maybe succeeded?

But then, if the user does input five integers, I am supposed to make that input into an array with integers and I just have no idea how to do that. I tried to figure it out on my own but I simply didn't understand what to do. Could anyone explain this to me, I'd be very grateful!

Here is my (probably very lacking) code:

import java.util.Scanner;

public class Try2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String inputR = in.nextLine(); 
        int lengthArgs = 5;
        int nummer = (inputR.split(" ").length);    
        if(nummer == lengthArgs) {
         //???
        }
        else {
          System.out.println("Wrong");
        }
    }
}
2
  • Your question has already been answered :) please look at; stackoverflow.com/questions/8348591/… Commented Feb 23, 2020 at 17:52
  • 1) I assume you're probably using an IDE like Eclipse or IntelliJ. Please familiarize yourself with its debugger. First task: confirm that inputR.split(" ") gives you 5 elements. 2) Change "nummer" to number (misspellings just look bad). 3) Use new int[]. 4) Use Integer.praseInt() Commented Feb 23, 2020 at 17:53

4 Answers 4

1

You are already splitting the input into an array, but not retaining it. Try this:

  Scanner in = new Scanner(System.in);
    String inputR = in.nextLine(); 
    int lengthArgs = 5;
    String[] values inputR.split(" ");

    if(values.length == lengthArgs) {
        int[] numbers = new int[values.length];
        for(int i = 0;i < lengthArgs;i++) {
          // Note that this is assuming valid input
          // If you want to check then add a try/catch 
          numbers[i] = Integer.parseInt(values [i]);
       }
   }else {
   System.out.println("Wrong");
Sign up to request clarification or add additional context in comments.

Comments

1

Another idea, not so simple as array, but more elegant is with using Collection of Integers, like this:

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    String inputR = in.nextLine();
    String inputs[] = inputR.split(" ");

    if(inputs.length != 5) {
        System.out.println("Invalid number of input");
    }

    List<Integer> integers = new ArrayList<>();
    for (String s : inputs) {
        Integer number = Integer.parseInt(s);
        integers.add(number);
    }
    System.out.println("Got following numbers in my input: "  + integers);
}

Comments

0
import java.util.Arrays;
import java.util.Scanner;

public class Test {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int lengthArgs = 5;

    String[] enteredChars;
    do {
        System.out.println("enter " + lengthArgs + "  numbers");
        enteredChars = in.nextLine().split(" ");
    } while (enteredChars.length != lengthArgs);

    int[] array = new int[lengthArgs];
    for (int i = 0; i < lengthArgs; i++) {
        try {
            array[i] = Integer.parseInt(enteredChars[i]);
        } catch (NumberFormatException e) {
            e.printStackTrace();// You have to do make sure that than you again have to enter all x numbers
        }
    }

    System.out.println("Your array " + Arrays.toString(array));
}
}

Comments

0

You can just do this.

 Scanner in  = new Scanner(System.in);
     int arr[] = new int [5];
      for(int i = 0; i < 5; i++){
       int a = in.nextInt();
       arr[i] = a;
       }

      for(int i : arr)
          System.out.print(i + " ");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.