Spaces:
Running
Running
Create 18b. Add numbers to list 2
Browse files
Week 3: Objects, files and exceptions/18b. Add numbers to list 2
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write a program that asks the user to enter integers.
|
| 2 |
+
The program adds the numbers entered by the user to the list in the order in which they were entered.
|
| 3 |
+
|
| 4 |
+
When the user enters -1, the program terminates and prints the list.
|
| 5 |
+
|
| 6 |
+
If the user's input contains anything other than numbers, the program ignores the input.
|
| 7 |
+
|
| 8 |
+
Example execution:
|
| 9 |
+
Give a number: 2
|
| 10 |
+
Give a number: 3
|
| 11 |
+
Give a number: sdf
|
| 12 |
+
Give a number: 4
|
| 13 |
+
Give a number: 5
|
| 14 |
+
Give a number: forty
|
| 15 |
+
Give a number: -1
|
| 16 |
+
[2, 3, 4, 5]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
import java.util.Random;
|
| 21 |
+
import java.util.ArrayList;
|
| 22 |
+
import java.util.Scanner;
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
public class Test{
|
| 27 |
+
public static void main(String[] args){
|
| 28 |
+
final Random r = new Random();
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
ArrayList<Integer> list = new ArrayList<>();
|
| 33 |
+
// DONT RECREATE SCANNER EVERY ITERATION
|
| 34 |
+
Scanner reader = new Scanner(System.in);
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
while (true) {
|
| 38 |
+
|
| 39 |
+
try {
|
| 40 |
+
System.out.print("Give a number: ");
|
| 41 |
+
int user_num = Integer.valueOf(reader.nextLine());
|
| 42 |
+
|
| 43 |
+
// terminate if user enters '-1'
|
| 44 |
+
if (user_num == -1) {
|
| 45 |
+
break;
|
| 46 |
+
}
|
| 47 |
+
else {
|
| 48 |
+
list.add(user_num);
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
}
|
| 53 |
+
// ignore error and continue if user enters non-integer
|
| 54 |
+
catch (Exception e) {
|
| 55 |
+
continue;
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
System.out.println(list);
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
Test number 1
|
| 77 |
+
Give a number: 2
|
| 78 |
+
Give a number: 3
|
| 79 |
+
Give a number: 4
|
| 80 |
+
Give a number: x
|
| 81 |
+
Give a number: 5
|
| 82 |
+
Give a number: -1
|
| 83 |
+
[2, 3, 4, 5]
|
| 84 |
+
|
| 85 |
+
Test number 2
|
| 86 |
+
Give a number: six
|
| 87 |
+
Give a number: 10
|
| 88 |
+
Give a number: 12
|
| 89 |
+
Give a number: asdads
|
| 90 |
+
Give a number: 14
|
| 91 |
+
Give a number: 16q
|
| 92 |
+
Give a number: we
|
| 93 |
+
Give a number: -1
|
| 94 |
+
[10, 12, 14]
|
| 95 |
+
|
| 96 |
+
Test number 3
|
| 97 |
+
Give a number: 9
|
| 98 |
+
Give a number: 9
|
| 99 |
+
Give a number: nine
|
| 100 |
+
Give a number: nine
|
| 101 |
+
Give a number: 9
|
| 102 |
+
Give a number: 9
|
| 103 |
+
Give a number: 8
|
| 104 |
+
Give a number: 7
|
| 105 |
+
Give a number: too
|
| 106 |
+
Give a number: 2
|
| 107 |
+
Give a number: f2
|
| 108 |
+
Give a number: -1
|
| 109 |
+
[9, 9, 9, 9, 8, 7, 2]
|
| 110 |
+
|
| 111 |
+
|