0

I'm working on a project to reverse arrays and print the largest of all elements. For simplicity I have reduced this problem just to printing two arrays the same way they have been written down. The maximum length of an array is 10. The arrays are given by the user, the integers are seperated by spaces and the two arrays are seperated with an enter.

Example

MY CODE

package testing;

import java.io.PrintStream;
import java.util.Locale;
import java.util.Scanner;

public class Testing {
    
    PrintStream out;
    
    Testing() {
        out = new PrintStream(System.out);
    }

    int[] fillArray(Scanner in) {
        int[] k = new int[10];
        int position = 0;
        in.useDelimiter(" ");
        while(in.hasNextInt()) {
            k[position] = in.nextInt();
            position++;
        }
        return k;
    }
    
    void printArray(int[] array) {
        for(int i = 0; i<10; i++) {
            System.out.printf("%d ", array[i]);
        }
        System.out.println();
    }
    
    void start() {
        Scanner in = new Scanner(System.in);
        int[] a = new int[10];
        int[] b = new int[10];
        a = fillArray(in);
        b = fillArray(in);
        printArray(a);
        printArray(b);
    }

    public static void main(String[] args) {
        Locale.setDefault(Locale.US);
        new Testing().start();
    }
}

When I run this code I get this, why won't the second array just print what I want? (This is probably a scanner thing which I'm not able to solve). Undesired result

  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Fleckinger Oct 28 '21 at 18:21
  • Thanks for the help, string.split is a very powerful function! – ruben zwaan Nov 03 '21 at 17:52

0 Answers0