Spaces:
Running
Running
"" + someInt; Integer.parseInt(str)
Browse files
Week 3: Objects, files and exceptions/7. Convert to a number list
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write the method
|
| 2 |
+
|
| 3 |
+
ArrayList<Integer> numList(ArrayList<String> list)
|
| 4 |
+
|
| 5 |
+
which takes a list of strings as its parameter.
|
| 6 |
+
Strings containing numbers.
|
| 7 |
+
|
| 8 |
+
The method returns a new list of integer type, with the contents of the strings converted to integers.
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
Example method call:
|
| 12 |
+
public static void main(String[] parameters){
|
| 13 |
+
ArrayList<String> list = new ArrayList<>();
|
| 14 |
+
list.add("2");
|
| 15 |
+
list.add("10");
|
| 16 |
+
list.add("-999");
|
| 17 |
+
|
| 18 |
+
System.out.println(numList(list));
|
| 19 |
+
}
|
| 20 |
+
Program outputs:
|
| 21 |
+
[2, 10, -999]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
==========================================
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
import java.util.Random;
|
| 29 |
+
import java.util.ArrayList;
|
| 30 |
+
import java.util.Arrays;
|
| 31 |
+
import java.util.stream.Collectors;
|
| 32 |
+
|
| 33 |
+
public class Test{
|
| 34 |
+
public static void main(String[] args){
|
| 35 |
+
final Random r = new Random();
|
| 36 |
+
|
| 37 |
+
testaa(new int[]{4,20,3,6,7,8,9,11});
|
| 38 |
+
testaa(new int[]{1,5,2,4,3,6,4,7,5,8,6,3,9,5,3});
|
| 39 |
+
testaa(new int[]{-5,-4,-1,-2,-3,-6,-7});
|
| 40 |
+
|
| 41 |
+
int pituus = r.nextInt(10) + 10;
|
| 42 |
+
int[] l = new int[pituus];
|
| 43 |
+
for (int i=0; i<pituus; i++) {
|
| 44 |
+
l[i] = (100 - r.nextInt(200));
|
| 45 |
+
}
|
| 46 |
+
testaa(l);
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
// testaa used by main
|
| 52 |
+
public static void testaa(int[] l) {
|
| 53 |
+
ArrayList<String> lista = new ArrayList<>();
|
| 54 |
+
|
| 55 |
+
// convert from int to string element
|
| 56 |
+
for (int a : l) {
|
| 57 |
+
lista.add("" + a);
|
| 58 |
+
}
|
| 59 |
+
System.out.println("Testing with list ");
|
| 60 |
+
System.out.print("");
|
| 61 |
+
tulosta(lista);
|
| 62 |
+
|
| 63 |
+
// convert BACK from string to integer element
|
| 64 |
+
System.out.println("As integers: " + numList(lista));
|
| 65 |
+
System.out.println("");
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
// tulosta used by testaa
|
| 69 |
+
public static void tulosta(ArrayList<String> lista) {
|
| 70 |
+
System.out.print("[\"");
|
| 71 |
+
// so the joined string becomes
|
| 72 |
+
// [" from above
|
| 73 |
+
// 1", "2", "3
|
| 74 |
+
// ", " is the 'delimiter'
|
| 75 |
+
// "] from below
|
| 76 |
+
System.out.print(String.join("\", \"", lista));
|
| 77 |
+
System.out.println("\"]");
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
// numList used by testaa
|
| 84 |
+
public static ArrayList<Integer> numList(ArrayList<String> list) {
|
| 85 |
+
ArrayList<Integer> list2 = new ArrayList<>();
|
| 86 |
+
// convert string to integer element
|
| 87 |
+
// https://www.geeksforgeeks.org/java/integer-valueof-vs-integer-parseint-with-examples/
|
| 88 |
+
for (String s : list) {
|
| 89 |
+
list2.add(Integer.parseInt(s));
|
| 90 |
+
}
|
| 91 |
+
return list2;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
Testing with list
|
| 105 |
+
["4", "20", "3", "6", "7", "8", "9", "11"]
|
| 106 |
+
As integers: [4, 20, 3, 6, 7, 8, 9, 11]
|
| 107 |
+
|
| 108 |
+
Testing with list
|
| 109 |
+
["1", "5", "2", "4", "3", "6", "4", "7", "5", "8", "6", "3", "9", "5", "3"]
|
| 110 |
+
As integers: [1, 5, 2, 4, 3, 6, 4, 7, 5, 8, 6, 3, 9, 5, 3]
|
| 111 |
+
|
| 112 |
+
Testing with list
|
| 113 |
+
["-5", "-4", "-1", "-2", "-3", "-6", "-7"]
|
| 114 |
+
As integers: [-5, -4, -1, -2, -3, -6, -7]
|
| 115 |
+
|
| 116 |
+
Testing with list
|
| 117 |
+
["-30", "27", "-21", "-93", "-24", "-4", "-26", "66", "82", "-14", "-60", "88", "-94", "10", "-49", "58", "-49", "-75"]
|
| 118 |
+
As integers: [-30, 27, -21, -93, -24, -4, -26, 66, 82, -14, -60, 88, -94, 10, -49, 58, -49, -75]
|
| 119 |
+
|
| 120 |
+
|