2

I'm very new to this and I was stuck on solving a problem that has to do with arrays and I need help. So the program Im supposed to write is that first the user inputs a number (int). Then the user can input any number (int) and each time the user enters a number, it prints out an array of those numbers in a sorted order. When the user inputs "end" in string, then the program ends and shows the whatever the array of sorted numbers it was. The thing is I don't know how the user can input a string when I declared that the scanner would take int. When I put in a string, it shows an error. I am a beginner so please don't use anything complicated like "something Exception" and things like that. I've only learned loops and just got in to learning Arrays. Thank you.

import java.util.*;
import java.util.Scanner;

public class Homework3 {
 public static void main(String[] args) {
  Scanner scan = new Scanner(System.in);
  System.out.print("Type :");
  int numbers = scan.nextInt();
  int [] a = {numbers};
  System.out.println(Arrays.toString(a));

   while ( a.length < 5 ) {
     System.out.print("Type: ");
     int number = scan.nextInt();
     int[] b = Arrays.copyOf(a, a.length + 1);
     b[b.length-1] = number;
     a = b;
     System.out.println(Arrays.toString(a));
}
   Arrays.sort(a);
   System.out.println(Arrays.toString(a));
 }
}
dwk963
  • 49
  • 6
  • 1
    Read data as String in a loop. If the entered String is `"end"` (use `equalsIgnoreCase()`), then break. Else match using `\\d+` then convert it into a number (`Integer.parseInt()`) – TheLostMind Sep 29 '15 at 09:00
  • What does the first user input correspond to ? No. of numbers the user is about to enter, right ? – CodeWalker Sep 29 '15 at 09:05
  • The scanner class has a lot of good methods: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html how about: if (scan.hasNext("end")){ ... } – Matt Pinkston Sep 29 '15 at 09:07

4 Answers4

3

Then why do you read the input as int? Instead of you I would read the data as String and convert it to an int when needed:

String input = scan.nextLine();

Now I can validate that

If the two conditions meet, I continue to read the input. Otherwise, if the two condition doesn't meet, then it's not a numeric type, and not the "end" String, what I do depends on the logic of my program. The last case is the "end" String, there I exit from the loop.

Community
  • 1
  • 1
Maroun
  • 94,125
  • 30
  • 188
  • 241
0

Read input as a string check if its "end" string else try to parse the string into an integer value

Modify your loop code like this

  while ( a.length < 5 ) {
         System.out.print("Type: ");
         String input = scan.nextLine();
         int number;
         if(input.equals("end"))
             break;
         else {
            try {
                number = Integer.parseInt(input);
                 int[] b = Arrays.copyOf(a, a.length + 1);
                 b[b.length-1] = number;
                 a = b;
                 System.out.println(Arrays.toString(a));
            } catch (Exception e) {
                System.out.println("invalid argument");
                break;
            }
Awais Ahmad
  • 427
  • 3
  • 17
0

This will do it for you!

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;


public class Help {

public static void main(String[] args) {

    ArrayList<Integer> userArray = new ArrayList<>();

    System.out.println("Enter some numbers!");

    Scanner scanner = new Scanner(System.in);
    String input = scanner.nextLine();

    while (!input.equalsIgnoreCase("END")) {

        int element = Integer.valueOf(input);
        userArray.add(element);
        for (Integer integer : userArray) {
            System.out.println(integer);
        }
        input = scanner.nextLine();

    }   
        Collections.sort(userArray);
        for (Integer integer : userArray) {
            System.out.println(integer);
        }

    }

}

enter image description here

CodeWalker
  • 2,281
  • 4
  • 23
  • 50
0

Also, you could have the user start out with saying how many numbers there will be. Then, it asks for that many numbers. The first number, the size, would define how large the array is.