Printing data file containing multiple matrixes - java

I'm running into a problem with a program that I'm writing for a class.
I was given a text file that contains three matrices and I need to create a loop that prints out all three of them in the output through various filters.
Here is what the file contains:
15 15
91 114 75 145 149 250 31 103 88 123 27 120 187 140 52
108 23 43 126 51 9 107 29 20 221 18 41 178 245 159
187 53 7 107 96 82 70 2 171 2 36 84 135 187 92
176 30 199 230 178 61 48 211 208 143 178 207 79 196 217
26 6 117 170 4 245 63 93 108 44 163 19 6 220 131
7 210 228 154 213 96 102 254 63 34 25 215 168 23 207
131 254 198 215 164 6 141 150 147 26 242 199 237 131 240
102 22 248 74 26 3 138 145 132 120 97 126 8 86 166
178 68 73 127 56 180 182 152 3 149 184 42 204 9 172
104 203 99 132 204 137 33 69 140 19 131 38 239 56 180
163 249 88 222 98 150 58 155 115 188 70 189 162 33 1
113 141 52 196 139 241 68 37 89 215 166 7 200 179 26
21 68 167 53 226 167 193 232 133 79 212 163 101 157 233
122 17 114 224 129 247 35 84 120 51 91 149 127 150 19
217 253 145 45 205 179 107 76 214 48 230 218 8 135 25
6 6
201 159 87 63 240 244
231 32 222 76 5 255
10 5 248 139 47 64
167 76 138 177 107 159
188 122 154 165 205 22
222 149 148 85 129 57
5 5
201 159 87 63 240
231 32 222 76 5
10 5 248 139 47
167 76 138 177 107
188 122 154 165 205
I was able to get a loop that read the file and prints out the first 15x15 array, but I need a loop that will print out the other three after it including the filters applied to the data.
That is:
Base image data:
(15x15 array)
(6x6 array)
(5x5 array)
Filtered data:
(15x15 filtered)
(6x6 filtered)
(5x5 filtered)
I'm able to print out the first array just fine, but I am racking my brain trying to find a way to loop the code to print out the other two arrays. Additionally, I have a second problem that when the filtered data is printed, it's all 0s like so:
Base Image Data
91 114 75 145 149 250 31 103 88 123 27 120 187 140 52
108 23 43 126 51 9 107 29 20 221 18 41 178 245 159
187 53 7 107 96 82 70 2 171 2 36 84 135 187 92
176 30 199 230 178 61 48 211 208 143 178 207 79 196 217
26 6 117 170 4 245 63 93 108 44 163 19 6 220 131
7 210 228 154 213 96 102 254 63 34 25 215 168 23 207
131 254 198 215 164 6 141 150 147 26 242 199 237 131 240
102 22 248 74 26 3 138 145 132 120 97 126 8 86 166
178 68 73 127 56 180 182 152 3 149 184 42 204 9 172
104 203 99 132 204 137 33 69 140 19 131 38 239 56 180
163 249 88 222 98 150 58 155 115 188 70 189 162 33 1
113 141 52 196 139 241 68 37 89 215 166 7 200 179 26
21 68 167 53 226 167 193 232 133 79 212 163 101 157 233
122 17 114 224 129 247 35 84 120 51 91 149 127 150 19
217 253 145 45 205 179 107 76 214 48 230 218 8 135 25
Image Filter1:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
The code I have it pretty extensive, so I'm having trouble pinpointing why it's returning 0s.
Here is my code:
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(new File("imagedata.txt"));
int r = scanner.nextInt();
int c = scanner.nextInt();
int [][] imageData = new int[r][c];
//for loop to create 2d array from image data
for(int row=0; row<imageData.length; row++) {
for(int col=0; col<imageData[row].length; col++) {
imageData[row][col] = scanner.nextInt();
}
}
System.out.println("Base Image Data");
for(int[] row : imageData) {
for(int num : row) {
System.out.print(num+" ");
}
System.out.println();
}
System.out.println("Image Filter1:");
int[][] filter1 = applyFilter1(imageData);//call return from applyFilter1
for(int[] row : filter1){
for(int num : row){
System.out.print(num+" ");
}
System.out.println();
}
}
public static int[][] applyFilter1(int[][] imageData){
int[][] filtered = new int[imageData.length][imageData[0].length];
for (int row=0; row<filtered.length;row++){
for (int col=0;col<filtered[row].length;col++){
int[] nebs = getNeighbors(row, col, imageData);
int sum = 0;
for(int i=0; i<nebs.length; i++){
sum = sum + nebs[i];
int average = sum / nebs.length;
filtered[row][col] = average;
}
}
}
return filtered;
}
public static int[] getNeighbors(int row, int col, int[][] imageData){
//find neighbors of current index
int [][] copyImage = new int[imageData.length][imageData[0].length];
try{
for(int r=0; r<imageData.length; r++){
for(int c=0; c<imageData[r].length; c++){
imageData[r][c] = copyImage[r][c];
}
}
} catch(Exception e){
System.out.println("Array copy not successful");
}
//handles the top row of the array
if(row==0){//if copyImage[0].length
if(col==0){//handles upper left corner of array
int[] nebs = {copyImage[row][col], copyImage[row+1][col],
copyImage[row][col+1]};
return nebs;
}
else if(col==copyImage[row].length-1){//handles upper right corner of array
int[] nebs = {copyImage[row][col], copyImage[row][col-1],
copyImage[row+1][col]};
return nebs;
}
else{//handles top row of array between corners
int[] nebs = {copyImage[row][col], copyImage[row][col-1],
copyImage[row+1][col], copyImage[row][col+1]};
return nebs;
}
}
//handles the bottom row of the array
else if(row==copyImage.length-1){//if the row is at max value
if(col==0){//handles botton left corner of array
int[] nebs = {copyImage[row][col], copyImage[row-1][col],
copyImage[row][col+1]};
return nebs;
}
else if(col==copyImage[row].length-1){//handles bottom right corner of array
int[] nebs = {copyImage[row][col], copyImage[row-1][col],
copyImage[row][col-1]};
return nebs;
}
else{//handles bottom row of array
int[] nebs = {copyImage[row][col], copyImage[row-1][col],
copyImage[row][col-1], copyImage[row][col+1]};
return nebs;
}
}
//handles leftmost column of array
else if(col==0){//if col=0 and row increases
int[] nebs = {copyImage[row][col], copyImage[row-1][col],
copyImage[row+1][col], copyImage[row][col+1]};
return nebs;
}
//handles rightmost column of array
else if(col==copyImage[row].length-1){//if col=max value and row increases
int[] nebs = {copyImage[row][col], copyImage[row-1][col],
copyImage[row+1][col], copyImage[row][col-1]};
return nebs;
}
//handles values in the body of the array
else{
int[] nebs = {copyImage[row][col], copyImage[row-1][col],
copyImage[row+1][col], copyImage[row][col-1],
copyImage[row][col+1]};
return nebs;
}
}
I'm still new to java, despite the amount of code in this program. So any guidance and feedback is welcome.

You can simply read this file line-by-line, split each string into an array of numbers, then format them as a three-character string, concatenate them back and output. No matter how many matrices are in this file.
List<String> list = List.of(
"91 114 75 145 149 250 31 103 88 123 27 120 187 140 52",
"108 23 43 126 51 9 107 29 20 221 18 41 178 245 159",
"187 53 7 107 96 82 70 2 171 2 36 84 135 187 92");
list.stream()
.map(str -> Arrays
// split string into an array of numbers
.stream(str.split("\s+"))
// format as a three-character string
.map(s -> String.format("%3s", s))
// concatenate into a single string
.collect(Collectors.joining(" ")))
// output line by line
.forEach(System.out::println);
Output:
91 114 75 145 149 250 31 103 88 123 27 120 187 140 52
108 23 43 126 51 9 107 29 20 221 18 41 178 245 159
187 53 7 107 96 82 70 2 171 2 36 84 135 187 92

Related

Write and Read binary file

I have a test: Write number from 50000 to 60000 to file with any format, then read this data to original number (file size limit 20kb) and cannot use writeShort() method to write or read. But i cannot read file to original number, my code below:
This is write
DataOutputStream out = new DataOutputStream(new FileOutputStream("D:\\mydata2.txt"));
for (int i = 50000; i <= 60000; i++) {
out.write(i);//write() just write 8-bit
}
out.close();
This is read
DataInputStream in = new DataInputStream(new FileInputStream("D:\\mydata2.txt"));
int i=0;
while (in.available() > 0) {
System.out.print(in.readUnsignedByte()+" ");
}
in.close();
Output like this:
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
255 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
181 182 183 184 185 186 187 188 189
Here's your clue for the 'hard part'. You must do the rest:
byte[] buf = new byte[2];
// (In loop)
buf[0] = (byte)(n >> 8);
buf[1] = (byte)(n & 0xFF);
This is the correct way to do it:
You do not write short, but you write the bytes of it. And the same logic for read too.
DataOutputStream out = new DataOutputStream(new FileOutputStream("C:\\New\\mydata2.txt"));
for (int i = 50000; i <= 60000; i++)
{
ByteBuffer dbuf = ByteBuffer.allocate(2);
dbuf.putShort((short) i);
byte[] bytes = dbuf.array();
out.writeByte(bytes[0]);
out.writeByte(bytes[1]);
}
out.flush();
out.close();
DataInputStream in = new DataInputStream(new FileInputStream("C:\\New\\mydata2.txt"));
byte[] buf = new byte[2];
while (in.available() > 0)
{
buf[0] = in.readByte();
buf[1] = in.readByte();
// Since Short keyword cannot be used, we could use the following way to get the numbers:
int num = (0xff & buf[0]) << 8 |
(0xff & buf[1]);
System.out.print(num +" ");
}
in.close();
Check this to understand the last conversion with shift operators.

Program Displays the number of Mersenne wrong

hi guys the problem that im having with this code is that the number of mersenne primes is supposed to be 4 but it outputs 8. i know this because the number of Mersenne primes between 1 and 1000 are 3, 7, 31, and 127 but it also outputs 15, 63, 255, and 511. which i dont know why. is there also a way to output the counts of Primes per 100 numbers? thanks
public static void main(String[] args) {
System.out.println("Prime numbers between 1 and 1000 are\n");
String message = "";
String message2 = "";
// loop numbers from 1 to 1,000, printing only the primes
int counter2 = 0;
int counter = 0;
for( int number s= 2; number <= 1000; number ++){
if(isPrime(number)) {
counter++;
if(counter % 10 == 0) {
System.out.println(number);
}
else
System.out.print(number + " ");
}
}
for(int number2 = 2; number2 <= 1000; number2 ++){
if(isMersenne(number2)){
counter2++;
System.out.print(number2 + "*");
}
}
System.out.print( "\n\n The number of primes between 1 and 1000 are " + counter);
System.out.print( "\n The number of Mersennes is " + counter2);
}
public static boolean isPrime( int number ){
// initialize the boolean variable prime to true
boolean isPrime = true;
for(int divisor = 2; divisor <= number /2; divisor++) {
if(number % divisor == 0) {
isPrime = false;
}
}
return isPrime;
}
public static boolean isMersenne(int number){
// declare and initialize variable powTwo to 2
int powTwo = 2;
boolean mersenne = false;
while(powTwo <= number){
powTwo *= 2;
if(powTwo - number == 1)
// if the prime is one less than a power of 2, return true
mersenne = true;
}
return mersenne;
} // end method isMersenne
}
this is the output of my program
Prime numbers between 1 and 1000 are
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113
127 131 137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223 227 229
233 239 241 251 257 263 269 271 277 281
283 293 307 311 313 317 331 337 347 349
353 359 367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457 461 463
467 479 487 491 499 503 509 521 523 541
547 557 563 569 571 577 587 593 599 601
607 613 617 619 631 641 643 647 653 659
661 673 677 683 691 701 709 719 727 733
739 743 751 757 761 769 773 787 797 809
811 821 823 827 829 839 853 857 859 863
877 881 883 887 907 911 919 929 937 941
947 953 967 971 977 983 991 997 3*7*15*31*63*127*255*511*
The number of primes between 1 and 1000 are 168
The number of Mersennes is 8
Your problem is that isMersenne is just checking whether the number is one less than a power of two. It's not also checking that it's a prime.
You need to change your isMersenne method so that it calls isPrime after checking whether the number is one less than a power of two. If isPrime returns false, then isMersenne should also return false.

Different conversion from BigIntger to byte[] if I use C# and Java

If have the following BigInteger:
1379080579050447841330186236235223160927998000398161138225875482305250883605652677639242794753995315199229112894647269426499088162253680340518114657361569012095908691691924534414360438924914998
If I convert to byte[] using ToByteArray in C# I get a result but if I convert the same BigInteger in Java I get a different result. How can I reach the same result? I need the byte[] since I need to remove the padding and then transform the byte array resulting into a string
As pointed out in the comments, the reason you're seeing different values between the byte[] from the C# BigInteger and the Java BigInteger is that the byte order is reversed: C# is little-endian and Java is big-endian.
(See Java vs. C#: BigInteger hex string yields different result? for more details).
As you can see, the order of the bytes in the array is exactly reversed:
C#
BigInteger bi = BigInteger.Parse("1379080579050447841330186236235223160927998000398161138225875482305250883605652677639242794753995315199229112894647269426499088162253680340518114657361569012095908691691924534414360438924914998");
byte[] ba = bi.ToByteArray();
for (int i = 0; i < ba.Length; i++) {
Console.Write(ba[i] + " ");
}
// 54 49 48 50 47 52 48 47 49 50 32 111 110 114 111 105 103 32 44 98 117 108 67
// 32 97 116 105 118 69 32 44 101 108 97 117 113 115 97 80 32 101 110 111 105 108
// 103 101 86 32 111 116 110 101 118 69 32 44 52 57 57 49 47 56 48 47 49 50 32 44
// 105 115 115 111 82 32 111 99 114 97 77
Java
BigInteger bi = new BigInteger("1379080579050447841330186236235223160927998000398161138225875482305250883605652677639242794753995315199229112894647269426499088162253680340518114657361569012095908691691924534414360438924914998");
byte[] ba = bi.toByteArray();
for (int i = 0; i < ba.length; i++) {
System.out.print(ba[i] + " ");
}
// 77 97 114 99 111 32 82 111 115 115 105 44 32 50 49 47 48 56 47 49 57 57 52 44
// 32 69 118 101 110 116 111 32 86 101 103 108 105 111 110 101 32 80 97 115 113 117
// 97 108 101 44 32 69 118 105 116 97 32 67 108 117 98 44 32 103 105 111 114 110 111
// 32 50 49 47 48 52 47 50 48 49 54
If you want a Java byte[] to hold the bytes in the same order as C#, you will need to reverse its contents.
You can just write a simple helper method that reverses the order of the bytes in the array:
public static void reverse(byte[] array) {
for (int i = 0, j = array.length - 1; i < j; i++, j--) {
byte b = array[i];
array[i] = array[j];
array[j] = b;
}
}
Or if you have access to 3rd-party libraries, you can use org.apache.commons.lang3.ArrayUtils.reverse(byte[]).
That would give you a byte[] in Java holding the bytes in the same order as C#:
Java
BigInteger bi = new BigInteger("1379080579050447841330186236235223160927998000398161138225875482305250883605652677639242794753995315199229112894647269426499088162253680340518114657361569012095908691691924534414360438924914998");
byte[] ba = bi.toByteArray();
reverse(ba);
for (int i = 0; i < ba.length; i++) {
System.out.print(ba[i] + " ");
}
// 54 49 48 50 47 52 48 47 49 50 32 111 110 114 111 105 103 32 44 98 117 108 67
// 32 97 116 105 118 69 32 44 101 108 97 117 113 115 97 80 32 101 110 111 105 108
// 103 101 86 32 111 116 110 101 118 69 32 44 52 57 57 49 47 56 48 47 49 50 32 44
// 105 115 115 111 82 32 111 99 114 97 77

How to use if statements to correctly format the date of the array list from File .txt

This is Data.txt:
Station ID: This is the unique ID for the weather station that recorded the data.
YearMonthDay: 8 digit number that uses 4 digits each for year (first two), month (middle two) and day (last 2).
Tmax: Maximum temperature for the date
Tmin: Minimum temperature for the date
Snow: Amount of snowfall for the date
Precip: Total precipitation for the day
14939 20150801 89 63 0 0
14939 20150802 96 71 0 0
14939 20150803 86 67 0 0
14939 20150804 89 67 0 0.17
14939 20150805 83 70 0 0
14939 20150806 82 63 0 0.03
14939 20150807 93 67 0 0
14939 20150808 88 69 0 0.36
14939 20150809 89 74 0 0
14939 20150810 85 63 0 0
14939 20150811 87 58 0 0
14939 20150812 88 59 0 0
14939 20150813 90 62 0 0
14939 20150814 92 68 0 0
14939 20150815 92 64 0 0
14939 20150816 94 69 0 0
14939 20150817 78 68 0 1.72
14939 20150818 75 56 0 0.36
14939 20150819 77 53 0 0.08
14939 20150820 85 52 0 0
14939 20150821 86 64 0 0
14939 20150822 91 65 0 0.01
14939 20150823 76 50 0 0
14939 20150824 82 46 0 0
14939 20150825 82 51 0 0
14939 20150826 85 55 0 0
14939 20150827 87 66 0 0.31
14939 20150828 80 64 0 0.74
14939 20150829 74 61 0 0
14939 20150830 78 62 0 0
14939 20150831 88 62 0 0
14939 20140801 86 63 0 0
14939 20140802 90 59 0 0.06
14939 20140803 93 66 0 0
14939 20140804 87 64 0 0
14939 20140805 93 67 0 0
14939 20140806 83 71 0 0.21
14939 20140807 81 68 0 0
14939 20140808 83 70 0 0
14939 20140809 85 68 0 0.55
14939 20140810 85 68 0 0.08
14939 20140811 80 57 0 0
14939 20140812 81 51 0 0
14939 20140813 87 53 0 0
14939 20140814 89 57 0 0
14939 20140815 76 69 0 0.23
14939 20140816 87 69 0 0
14939 20140817 92 67 0 0.31
14939 20140818 91 62 0 0.01
14939 20140819 93 66 0 0
14939 20140820 96 74 0 0
14939 20140821 90 71 0 0.23
14939 20140822 93 73 0 0.18
14939 20140823 95 69 0 0.21
14939 20140824 91 70 0 0
14939 20140825 83 64 0 0
14939 20140826 81 63 0 1.45
14939 20140827 77 67 0 0.65
14939 20140828 82 69 0 2.66
14939 20140829 75 67 0 0
14939 20140830 87 64 0 0
14939 20140831 92 66 0 0.71
14939 20130801 89 62 0 0.69
14939 20130802 84 65 0 0.06
14939 20130803 80 62 0 0
14939 20130804 79 61 0 0
14939 20130805 86 70 0 0
14939 20130806 88 67 0 0
14939 20130807 85 66 0 0
14939 20130808 79 65 0 0
14939 20130809 83 60 0 0
14939 20130810 84 54 0 0
14939 20130811 87 61 0 0.01
14939 20130812 85 65 0 0.09
14939 20130813 80 60 0 0
14939 20130814 78 56 0 0
14939 20130815 70 63 0 0.25
14939 20130816 75 62 0 0
14939 20130817 80 60 0 0
14939 20130818 81 52 0 0
14939 20130819 87 63 0 0
14939 20130820 91 70 0 0
14939 20130821 92 69 0 0
14939 20130822 86 68 0 0.01
14939 20130823 92 71 0 0
14939 20130824 94 75 0 0
14939 20130825 96 73 0 0
14939 20130826 97 75 0 0
14939 20130827 98 72 0 0
14939 20130828 95 67 0 0
14939 20130829 98 68 0 0
14939 20130830 100 70 0 0
14939 20130831 96 72 0 0
14939 20120801 100 72 0 0
14939 20120802 96 70 0 0.11
14939 20120803 99 69 0 0
14939 20120804 84 59 0 0
14939 20120805 89 55 0 0
14939 20120806 97 60 0 0
14939 20120807 102 69 0 0
14939 20120808 103 67 0 0
14939 20120809 87 65 0 0
14939 20120810 82 55 0 0
14939 20120811 83 51 0 0.01
14939 20120812 84 63 0 0
14939 20120813 80 57 0 0
14939 20120814 87 57 0 0
14939 20120815 94 58 0 0
14939 20120816 77 49 0 0
14939 20120817 82 44 0 0
14939 20120818 78 45 0 0
14939 20120819 83 46 0 0
14939 20120820 89 48 0 0
14939 20120821 94 53 0 0
14939 20120822 93 58 0 0
14939 20120823 93 66 0 0
14939 20120824 77 68 0 0.13
14939 20120825 76 65 0 0.05
14939 20120826 85 62 0 0
14939 20120827 93 57 0 0
14939 20120828 97 60 0 0
14939 20120829 98 63 0 0
14939 20120830 100 66 0 0
14939 20120831 99 58 0 0
14939 20110801 104 75 0 0
14939 20110802 95 76 0 0
14939 20110803 89 69 0 0
14939 20110804 86 71 0 0.13
14939 20110805 85 71 0 0.45
14939 20110806 92 67 0 0.57
14939 20110807 88 68 0 0.02
14939 20110808 87 66 0 0.13
14939 20110809 84 63 0 0
14939 20110810 80 62 0 0
14939 20110811 85 56 0 1.06
14939 20110812 84 62 0 0.56
14939 20110813 83 62 0 0
14939 20110814 83 57 0 0
14939 20110815 79 65 0 0.64
14939 20110816 77 67 0 0
14939 20110817 85 65 0 0
14939 20110818 90 65 0 0.66
14939 20110819 84 63 0 0.05
14939 20110820 80 66 0 0
14939 20110821 85 61 0 0
14939 20110822 88 71 0 0
14939 20110823 95 76 0 0
14939 20110824 87 59 0 0
14939 20110825 84 55 0 0
14939 20110826 87 62 0 0
14939 20110827 86 61 0 0
14939 20110828 81 64 0 0
14939 20110829 82 57 0 0
14939 20110830 84 65 0 2.62
14939 20110831 92 66 0 0
14939 20100801 90 64 0 0
14939 20100802 94 74 0 0
14939 20100803 91 73 0 0
14939 20100804 88 70 0 0.44
14939 20100805 87 66 0 0
14939 20100806 87 66 0 0
14939 20100807 92 68 0 0
14939 20100808 98 75 0 0
14939 20100809 95 72 0 0
14939 20100810 96 74 0 0
14939 20100811 99 69 0 0
14939 20100812 100 67 0 0
14939 20100813 95 70 0 0.02
14939 20100814 92 63 0 0
14939 20100815 86 57 0 0
14939 20100816 90 59 0 0.27
14939 20100817 69 64 0 0.18
14939 20100818 84 65 0 0
14939 20100819 95 65 0 0
14939 20100820 89 72 0 0.09
14939 20100821 95 67 0 0
14939 20100822 94 64 0 0
14939 20100823 94 66 0 0.27
14939 20100824 81 56 0 0.7
14939 20100825 85 52 0 0
14939 20100826 87 57 0 0
14939 20100827 87 54 0 0
14939 20100828 91 58 0 0
14939 20100829 93 63 0 0
14939 20100830 94 73 0 0
14939 20100831 94 68 0 0.84
14939 20090801 78 58 0 0
14939 20090802 94 54 0 0
14939 20090803 94 65 0 0
14939 20090804 88 66 0 0.07
14939 20090805 85 63 0 0.04
14939 20090806 84 59 0 0
14939 20090807 97 73 0 0.04
14939 20090808 102 76 0 0
14939 20090809 83 66 0 0.11
14939 20090810 88 59 0 0
14939 20090811 89 57 0 0
14939 20090812 96 66 0 0
14939 20090813 93 65 0 0
14939 20090814 93 62 0 0
14939 20090815 84 67 0 0.95
14939 20090816 81 63 0 0.6
14939 20090817 83 57 0 0
14939 20090818 84 58 0 0
14939 20090819 83 66 0 0.33
14939 20090820 77 59 0 0
14939 20090821 74 53 0 0
14939 20090822 79 48 0 0
14939 20090823 83 55 0 0
14939 20090824 91 63 0 0
14939 20090825 86 70 0 0
14939 20090826 77 66 0 1.06
14939 20090827 78 58 0 0
14939 20090828 78 52 0 0
14939 20090829 72 51 0 0
14939 20090830 71 48 0 0
14939 20090831 73 42 0 0
14939 20080801 93 71 0 0
14939 20080802 99 73 0 0
14939 20080803 102 70 0 0
14939 20080804 93 75 0 0
14939 20080805 85 65 0 0
14939 20080806 86 63 0 0
14939 20080807 90 60 0 0
14939 20080808 90 61 0 0
14939 20080809 81 66 0 0.24
14939 20080810 88 59 0 0.26
14939 20080811 84 65 0 0.4
14939 20080812 87 63 0 0.02
14939 20080813 91 65 0 0
14939 20080814 86 66 0 0
14939 20080815 84 62 0 0
14939 20080816 84 58 0 0
14939 20080817 86 55 0 0
14939 20080818 89 60 0 0
14939 20080819 88 60 0 0
14939 20080820 87 57 0 0
14939 20080821 84 66 0 0
14939 20080822 95 70 0 0
14939 20080823 83 63 0 0
14939 20080824 83 61 0 0.46
14939 20080825 83 56 0 0
14939 20080826 82 59 0 0
14939 20080827 87 64 0 0.4
14939 20080828 90 57 0 0
14939 20080829 89 50 0 0
14939 20080830 91 54 0 0
14939 20080831 93 69 0 0
14939 20070801 89 68 0 0.28
14939 20070802 90 66 0 0
14939 20070803 90 67 0 0.16
14939 20070804 96 76 0 0
14939 20070805 92 74 0 0
14939 20070806 93 73 0 0.21
14939 20070807 85 72 0 0.44
14939 20070808 90 72 0 1.34
14939 20070809 96 74 0 0
14939 20070810 96 67 0 0.46
14939 20070811 97 72 0 0
14939 20070812 95 72 0 0.12
14939 20070813 100 70 0 0
14939 20070814 98 75 0 0
14939 20070815 92 71 0 0
14939 20070816 83 72 0 0.11
14939 20070817 85 71 0 0.1
14939 20070818 93 73 0 0
14939 20070819 91 73 0 0
14939 20070820 96 70 0 0.39
14939 20070821 95 70 0 0
14939 20070822 88 67 0 0.96
14939 20070823 86 67 0 0.55
14939 20070824 75 63 0 0.01
14939 20070825 81 58 0 0
14939 20070826 89 68 0 0
14939 20070827 95 75 0 0
14939 20070828 94 67 0 0.65
14939 20070829 76 62 0 0.02
14939 20070830 80 57 0 0
14939 20070831 83 56 0 0
14939 20060801 105 79 0 0
14939 20060802 85 65 0 0
14939 20060803 90 58 0 0
14939 20060804 91 54 0 0
14939 20060805 102 70 0 0.1
14939 20060806 91 72 0 0.4
14939 20060807 82 67 0 0.09
14939 20060808 86 66 0 1.71
14939 20060809 100 74 0 0
14939 20060810 90 74 0 0.04
14939 20060811 91 69 0 0
14939 20060812 90 70 0 0
14939 20060813 82 71 0 0.11
14939 20060814 83 58 0 0
14939 20060815 85 53 0 0
14939 20060816 87 64 0 0.49
14939 20060817 90 70 0 0.14
14939 20060818 88 69 0 0.6
14939 20060819 78 61 0 0.09
14939 20060820 81 57 0 0
14939 20060821 89 60 0 0
14939 20060822 87 63 0 0
14939 20060823 91 63 0 0
14939 20060824 95 68 0 0
14939 20060825 83 68 0 0.02
14939 20060826 80 63 0 0
14939 20060827 76 62 0 0.26
14939 20060828 67 63 0 0
14939 20060829 80 59 0 0
14939 20060830 82 52 0 0
14939 20060831 83 60 0 0
How I correct all of the YearMonthDay (20150801) to August 2015
Try this:
Date date = new SimpleDateFormat("yyyyMMdd").parse("20150829");
String ds = new SimpleDateFormat("MMMM yyyy").format(date);
System.out.println(ds);
This will show: August 2015

Issue with "deck shuffling" program: getting unexpected zeroes in results Java

My assignment is to create a card deck shuffling program, but I am having an issue with the algorithm I have made to shuffle it. The idea is to take a card at random from the deck (values[]) and place it into the new shuffled deck (shuffled[]). However my program is spitting out shuffles that have too many zeroes in it. The original array contains int values 0-51. The first zero also always stays in the first slot for some reason. Here's my code:
public static void selectionShuffle(int[] values) {
int[] shuffled = new int[52];
for (int k = 51; k > 0;) {
int r = (int)(Math.random() * (double)51);
if (values[r] != -1) {
shuffled[k] = values[r];
values[r] = -1;
k--;
}
}
for (int i = 0; i < values.length; i++) {
values[i] = shuffled[i];
}
}
Results of 8 consecutive random selection shuffles
1: 0 22 40 43 6 14 31 4 47 1 36 41 0 3 24 12 5 39 27 23 11 28 50 38 7 18 16 32 17 20 21 2 8 13 15 46 19 26 9 48 25 34 45 42 10 33 29 49 30 44 37 35
2: 0 17 34 2 26 12 4 13 38 27 20 29 40 28 47 0 48 9 6 43 46 33 23 1 19 3 49 41 7 39 30 8 25 32 10 24 0 16 45 36 14 37 42 11 44 15 50 21 31 18 22 5
3: 0 23 2 44 20 38 45 46 47 27 50 7 26 28 0 21 24 37 11 19 40 10 1 29 36 14 30 12 25 22 16 6 39 0 0 8 18 33 9 48 42 43 34 13 32 17 41 15 49 4 3 31
4: 0 32 49 0 13 41 25 46 18 2 28 23 7 40 0 47 0 29 45 22 21 27 8 30 1 19 4 26 37 14 44 20 15 39 50 12 10 11 36 34 42 0 24 6 3 17 33 16 48 38 9 43
5: 0 29 26 13 1 15 0 20 47 9 17 21 30 34 28 0 22 18 0 3 6 2 0 38 12 48 23 27 11 16 42 32 39 40 33 0 37 44 50 41 46 49 8 24 10 7 14 25 19 45 36 4
6: 0 7 32 26 36 33 28 27 15 39 47 30 45 23 0 0 42 50 17 40 22 48 0 20 37 14 21 49 10 19 9 18 2 16 0 3 8 24 41 1 0 38 13 0 29 44 12 46 11 25 34 6
7: 0 9 0 21 29 18 48 33 45 20 15 24 44 46 47 0 36 2 39 28 0 12 0 50 19 42 32 27 8 38 37 23 0 11 25 13 10 3 0 34 26 40 17 0 41 7 30 14 1 22 16 49
8: 0 46 41 20 8 38 9 36 40 3 14 26 33 44 10 47 24 27 29 16 28 32 0 18 39 48 0 34 12 0 30 17 0 23 15 22 13 0 25 7 45 0 37 11 21 42 50 19 2 0 0 1
I think the issue is in the below line of code. If you don't initialize all values of the array greater than or equal to 0, it will not change the value. Since the default int value is 0, that is what is coming across.
for (int k = 51; k > 0;) {
should be
for (int k = 51; k >= 0;) {
Fill a deck with 52 cards
Parse the deck 51 times, grabbing the card at rand() * (numOfCardsRemainingInDeck)
Shift all cards from 'plucked' position to endOfArray one position 'to the left'.
After 51 iterations, the last card remaining is stuck into the last position of your result deck.
The problem is that your algorithm takes 51 cards out of 52, leaving shuffled[0] at 0. You can see that that is what's happening by looking at the results of the shuffles: position zero is always set to zero, while the remaining positions are set to random values.
One of the cards remains unshuffled from the first iteration; zero takes its place.
The next iteration can shuffle that zero, too, so now you have three zeros. The next iteration may add yet another zero, and so on.
To fix this issue, make sure that the loop goes all the way to zero, i.e.
for (int k = 51; k >= 0;) {
// ^
You also need to multiply random by 52, because otherwise you would never hit the last element of the array:
int r = (int)(Math.random() * (double)52);
You could also make a new random number generator, and call nextInt(52) on it.
Note that hitting that last element may take a while. An alternative solution would be to run the loop 51 times, and then put the last remaining value that has not been reset to -1 into position zero.
I would suggest using a List for this action, instead of an array.
If you use a list<cards>, then you could use Collections.shuffle
Randomly permute the specified list using the specified source of randomness. All permutations occur with equal likelihood assuming that the source of randomness is fair.
This implementation traverses the list backwards, from the last element up to the second, repeatedly swapping a randomly selected element into the "current position". Elements are randomly selected from the portion of the list that runs from the first element to the current position, inclusive.
This method runs in linear time. If the specified list does not implement the RandomAccess interface and is large, this implementation dumps the specified list into an array before shuffling it, and dumps the shuffled array back into the list. This avoids the quadratic behavior that would result from shuffling a "sequential access" list in place.
public static void shuffle(List<?> list, Random rnd) {
int size = list.size();
if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
for (int i=size; i>1; i--)
swap(list, i-1, rnd.nextInt(i));
} else {
Object arr[] = list.toArray();
// Shuffle array
for (int i=size; i>1; i--)
swap(arr, i-1, rnd.nextInt(i));
// Dump array back into list
ListIterator it = list.listIterator();
for (int i=0; i<arr.length; i++) {
it.next();
it.set(arr[i]);
}
}
}
More info/Source
public class Card {
String name; // for simplicity, just the card's name
public Card(String name) {
this.name = name;
}
public String getName() {return name;}
}
// ...
List<Card> deck = new ArrayList<Card>();
initDeck(); // adds like 52 new cards to the deck
Collections.shuffle(deck); // shuffle the deck
Card drawn = deck.get(0); // draw the first card
deck.remove(drawn); // remove it from the deck
Found here

Categories