Using priority queue with File Reader - java

I want to order numbers between the range of 1-100 with File Reader. It's very weird otput. My code:
import java.io.*;
import java.util.*;
public class Main {
public static String strLine;
public static int order = 1;
public static void main(String[] args) {
Queue<String> queue = new PriorityQueue<String>();
try{
FileReader fr = new FileReader("File.txt");
BufferedReader br = new BufferedReader(fr);
while ((strLine = br.readLine()) != null) {
queue.offer(strLine);
}
br.close();
} catch (IOException e){
System.out.println("File not found");
}
while (!queue.isEmpty()){
System.out.println(order + ".Number: " + queue.poll());
order++;
}
} }
File.txt:
81
56
42
3
49
100
61
76
74
79
6
90
34
93
22
71
72
63
67
18
14
5
91
39
53
86
40
45
60
55
48
62
8
4
80
11
87
47
15
46
97
66
43
58
41
54
33
96
82
44
98
27
85
10
32
95
35
24
7
30
83
9
31
99
25
29
57
36
69
50
28
59
12
37
78
73
70
89
65
51
77
88
20
17
21
94
64
19
26
23
92
52
2
16
68
75
38
1
84
13
output:
1.Number: 1
2.Number: 10
3.Number: 100
4.Number: 11
5.Number: 12
6.Number: 13
7.Number: 14
8.Number: 15
9.Number: 16
10.Number: 17
11.Number: 18
12.Number: 19
13.Number: 2
14.Number: 20
15.Number: 21
16.Number: 22
17.Number: 23
18.Number: 24
19.Number: 25
20.Number: 26
21.Number: 27
22.Number: 28
23.Number: 29
24.Number: 3
25.Number: 30
26.Number: 31
27.Number: 32
28.Number: 33
29.Number: 34
30.Number: 35
31.Number: 36
32.Number: 37
33.Number: 38
34.Number: 39
35.Number: 4
36.Number: 40
37.Number: 41
38.Number: 42
39.Number: 43
40.Number: 44
41.Number: 45
42.Number: 46
43.Number: 47
44.Number: 48
45.Number: 49
46.Number: 5
47.Number: 50
48.Number: 51
49.Number: 52
50.Number: 53
51.Number: 54
52.Number: 55
53.Number: 56
54.Number: 57
55.Number: 58
56.Number: 59
57.Number: 6
58.Number: 60
59.Number: 61
60.Number: 62
61.Number: 63
62.Number: 64
63.Number: 65
64.Number: 66
65.Number: 67
66.Number: 68
67.Number: 69
68.Number: 7
69.Number: 70
70.Number: 71
71.Number: 72
72.Number: 73
73.Number: 74
74.Number: 75
75.Number: 76
76.Number: 77
77.Number: 78
78.Number: 79
79.Number: 8
80.Number: 80
81.Number: 81
82.Number: 82
83.Number: 83
84.Number: 84
85.Number: 85
86.Number: 86
87.Number: 87
88.Number: 88
89.Number: 89
90.Number: 9
91.Number: 90
92.Number: 91
93.Number: 92
94.Number: 93
95.Number: 94
96.Number: 95
97.Number: 96
98.Number: 97
99.Number: 98
100.Number: 99
Process finished with exit code 0

Since it is a queue of Strings it is sorted alphabetically. If you want to keep it as Strings, you can use a custom comparator that converts the Strings to ints and compares those:
// This constructor requires an initial capacity before the comparator
Queue<String> queue = new PriorityQueue<String>(100, new Comparator<String>() {
public int compare(String s1, String s2) {
return Integer.parseInt(s1) - Integer.parseInt(s2);
}
});
Another option is to change the type of the queue to Integer and convert the input:
Queue<Integer> queue = new PriorityQueue<Integer>();
...
while ((strLine = br.readLine()) != null) {
queue.offer(Integer.parseInt(strLine));
}

Related

Finding exit from a maze

I wrote this code for a programming assignment given in a course about graphs on Coursera. enter code here. It passes on the test cases given in the question description but fails on one when submitted I'm new to graphs. Can some one help me find the error in this code?
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Iterator;
public class Reachability {
private static boolean[] visited;
private static int reach(ArrayList<Integer>[] adj, int x, int y) {
if(x == y){
return 1;
}
visited[x] = true;
Iterator itr = adj[x].iterator();
while(itr.hasNext()){
x = (int)itr.next();
if(!visited[x]){
return reach(adj,x,y);
}
}
return 0;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
ArrayList<Integer>[] adj = (ArrayList<Integer>[])new ArrayList[n];
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<Integer>();
}
for (int i = 0; i < m; i++) {
int x, y;
x = scanner.nextInt();
y = scanner.nextInt();
adj[x - 1].add(y - 1);
adj[y - 1].add(x - 1);
}
int x = scanner.nextInt() - 1;
int y = scanner.nextInt() - 1;
visited = new boolean[n];
System.out.println(reach(adj, x, y));
}
}
Given below is the failed test case.
Failed case #6/16: (Wrong answer)
Input:
100 100
27 96
6 9
81 98
21 94
22 68
76 100
8 50
38 86
71 75
32 93
16 50
71 84
6 72
22 58
7 19
19 76
44 75
24 76
31 35
11 89
42 98
63 92
37 38
20 98
45 91
23 53
37 91
76 93
67 90
12 22
43 52
23 56
67 68
1 21
17 83
63 72
30 32
7 91
50 69
38 44
55 89
15 23
11 72
28 42
22 69
56 79
5 83
55 73
13 72
7 93
20 54
21 55
66 89
2 91
18 88
26 64
11 61
28 59
12 86
42 95
17 82
50 66
66 99
40 71
20 40
5 66
92 95
32 46
7 36
44 94
6 31
19 67
26 57
53 84
10 68
28 74
34 94
25 61
71 88
10 89
28 52
72 79
39 73
11 80
44 79
13 77
30 96
30 53
10 39
1 90
40 91
62 71
44 54
15 17
69 74
13 67
24 69
34 96
21 50
20 91
42 46
Your output:
0
Correct output:
1
The problem is that you do an early return from reach() if y cannot be reached via the first unvisited neighbor of x:
return reach(adj,x,y);
What you want to do instead is:
If y can be reached then return 1.
If y cannot be reached then continue with the next neighbor
That would read:
if (reach(adj, x, y) == 1)
return 1;
Unrelated comment: You may want to use Iterator<Integer> instead of the raw type Integer. This avoids the cast and compiler warnings.

JOptionPane and Graphs

I would like to know how to give JOptionPane the ability to scroll through a histogram. I managed to get JOptionPane to display my histogram, the only problem is you can't scroll through it. The window stretches to the bottom of the screen and displays only half of the graph.
Note: This only happens when reading large input files that contain 300+ integers between 0 and 130. The program will not recognize anything else.
Input Values:
54
38
42
40
34
51
54
58
61
55
54
42
40
34
51
54
54
54
38
60
42
40
54
54
54
54
54
54
54
54
54
54
54
54
54
54
54
32
28
24
18
9
4
22
31
38
34
41
32
28
24
18
31
38
34
41
32
28
24
18
31
38
34
41
32
28
31
38
35
51
34
41
56
63
59
66
48
46
58
41
56
63
51
59
48
46
58
41
56
63
53
52
58
48
49
58
41
56
63
51
52
59
58
66
63
71
69
70
72
67
66
63
71
74
75
69
73
78
72
67
63
71
74
59
56
69
70
78
72
66
71
74
69
70
78
72
67
63
59
58
57
64
71
72
67
63
59
64
71
73
79
75
78
72
67
63
59
63
71
73
75
78
72
67
63
57
73
77
75
78
72
67
71
73
77
75
78
81
83
87
84
91
92
90
84
76
83
82
89
78
81
83
87
84
91
92
90
84
78
85
82
89
96
91
78
81
83
86
82
91
92
90
84
85
82
89
92
96
91
95
97
98
91
95
97
93
87
85
94
89
92
96
93
96
97
98
98
100
102
95
97
93
87
88
89
93
84
89
92
95
95
95
97
94
91
87
84
80
90
82
80
73
75
70
74
74
75
70
66
63
71
69
70
72
67
66
63
71
55
59
53
58
52
67
63
71
74
59
56
69
70
58
62
56
51
54
49
60
68
62
67
63
59
58
57
64
44
38
42
40
34
51
54
55
54
42
40
34
51
54
54
54
38
42
31
38
34
41
32
28
24
18
31
38
34
41
32
28
24
18
31
38
34
41
32
28
31
28
35
41
34
37
26
23
29
40
45
46
39
31
40
38
29
34
36
import javax.swing.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class CoolWeather
{
static JFileChooser selecter;
static Scanner in;
public static void main(String[] args) throws FileNotFoundException
{
//Get Input File
File inputFile;
selecter = new JFileChooser(".");
int status = selecter.showOpenDialog(null);
if(status != JFileChooser.APPROVE_OPTION)
{
JOptionPane.showMessageDialog(null, "Closing Program");
System.exit(0);
}
inputFile = selecter.getSelectedFile();
JOptionPane.showMessageDialog(null, "Opening: " + inputFile.getName());
//Creates Array
int[] temps = readData(inputFile);
//Prints Histogram
showMessage(temps);
}
//The Following Method Creates and Populates An Array With Data
//From The Input File
public static int[] readData(File inputFile) throws FileNotFoundException
{
in = new Scanner(inputFile);
in.useDelimiter("[^0-9/s]+");
int[] temps = new int[131];
int count = 0;
int num;
do
{
num = in.nextInt();
count++;
temps[num]++;
}
while (in.hasNextInt());
JOptionPane.showMessageDialog(null, "The Number Of Entries Read: " + count);
return temps;
}
public static void showMessage(int[] temps)
{
String output = "Temp\tCount\tVisual";
// for each array element, output a bar in histogram
for ( int counter = 0; counter < temps.length; counter++ )
{
if (temps[counter] > 0)
{
output += "\n" + counter + "\t" + temps[ counter ] + "\t";
// print bar of asterisks
for ( int stars = 0; stars < temps[ counter ]; stars++ )
{output += "*";}
}
} // end outer for
JTextArea outputArea = new JTextArea();
outputArea.setText( output );
JOptionPane.showMessageDialog( null, outputArea, "CoolWeather Histogram", JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
This is the JOptionPane I use for UI when handling exceptions, but the principle is the same - JTextArea in JScrollPane
JLabel label = new JLabel(e.getLocalizedMessage() + ":");
label.setFont(getFont().deriveFont(Font.BOLD));
label.setAlignmentX(JLabel.LEFT_ALIGNMENT);
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
textArea.setFont(getFont());
textArea.setTabSize(2);
textArea.setText(writer.toString());
SwingUtilities.invokeLater(() -> textArea.setCaretPosition(0));
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
scrollPane.setPreferredSize(new Dimension(500, 200));
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(label);
panel.add(Box.createVerticalStrut(5));
panel.add(scrollPane);
JOptionPane.showMessageDialog(this, panel, ApplicationInfo.getAppName(), JOptionPane.ERROR_MESSAGE);
JTextArea outputArea = new JTextArea();
outputArea.setText(output);
outputArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(outputArea);
scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
scrollPane.setPreferredSize(new Dimension(500, 200));
JOptionPane.showMessageDialog( null, scrollPane, "CoolWeather Histogram", JOptionPane.INFORMATION_MESSAGE);
System.exit( 0 );

Unable to read Inputs from Console in java

I need to read input test cases from console into a 2D array and a 1D array but unable to find the error in my code
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static final int SIZE = 5;
public static int card[][];
public static int ball[];
public static void read()throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count=0;
for(int row = 0; row < SIZE && count < 25; row++){
String line= br.readLine();
System.out.println("line"+line);
//if((line= br.readLine()) != null) {
int column = 0;
StringTokenizer st = new StringTokenizer(line);
while(st.hasMoreTokens()){
card[row][column] = Integer.parseInt(st.nextToken());
column++;
count++;
}
column = 0;
// }
}
int i = 0, b=0;
String line ;
while((line =br.readLine() )!= null && i < 75) {
StringTokenizer st1 = new StringTokenizer(line);
while(st1.hasMoreTokens()){
ball[i++]= Integer.parseInt(st1.nextToken());
}
}
}
public static void printing() {
System.out.println("Card values\n");
for(int i=0;i<SIZE;i++) {
for(int j=0;j<SIZE;j++){
System.out.print(card[i][j]+"\t");
}
System.out.println();
}
System.out.println("Ball values\n");
for(int j=0;j<75;j++) {
System.out.print(ball[j]+"\t");
//System.out.println();
}
}
public static void main (String[] args) throws java.lang.Exception
{
card = new int[SIZE][SIZE];
ball = new int[75];
for(int t=0 ; t<2 ; t++) {
read();
printing();
}
}
}
Also the test case input is as follows
10 17 44 59 62
2 28 31 58 68
5 16 37 53 71
6 26 35 51 66
9 21 34 60 70
45 37 19 47 16 39 66 14 28 15
72 17 62 12 55 11 73 75 9 18
56 4 29 32 61 63 51 38 33 2
8 36 6 24 23 22 21 5 60 35
41 74 34 7 67 25 50 10 43 53
3 46 68 40 48 69 54 30 20 70
31 59 57 49 1 42 58 27 52 13
64 44 71 26 65
1 7 4 9 2
2 28 31 58 68
5 16 37 53 71
6 26 35 51 66
9 21 34 60 70
45 37 19 47 16 39 66 14 28 15
72 17 62 12 55 11 73 75 9 18
56 4 29 32 61 63 51 38 33 2
8 36 6 24 23 22 21 5 60 35
41 74 34 7 67 25 50 10 43 53
3 46 68 40 48 69 54 30 20 70
31 59 57 49 1 42 58 27 52 13
64 44 71 26 65
I am able to read the first test case but while reading the 2nd test case i am getting null error.
On your second pass:
for(int t=0 ; t<2 ; t++) {
read();
You create a second BufferedReader from System.in.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
It looks like you're expecting this to re-read the input. But all the input has already been read.
You might be able to reset this small input stream with a combination of InputStream.readLimit(), InputStream.mark() and InputStream.reset(). Or you could put the data in a file, and create a new FileInputStream for each read.

String to Int 2D array

How do you fix the problem of
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 19 at ProjectEuler11.main(ProjectEuler11.java:21).
Because what I am trying to do is to put all numbers to int 2D array from this String
class ProjectEuler11 {
public static void main(String []args) {
String digitNumber = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 2124 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48";
String []numberVector = digitNumber.split(" ");
int [][]digitNumberVector = new int[19][19];
int x = 0;
int y = 0;
int answerNumber = 0;
for(int parameter2 = 0; parameter2 < 20; parameter2++) {
for(int parameter3 = 0; parameter3 < 20; parameter3++){
for(String parameter : numberVector) {
x = parameter2;
y = parameter3;
digitNumberVector[x][y] = Integer.parseInt(parameter);
}
}
}
}
}// Java Document
new int[19][19]
In such array, elements are indexed 0...18. But in your for loops:
for(int parameter2 = 0; parameter2 < 20; parameter2++)
parameter2 and parameter3 take values 0...19, and this 19 is already out of bounds.
uchitha Kiripitige is correct, the array is indexed wrong. Below is a correct version of your code that dynamically calculates the indices. Also, I made a few performance optimizations to your code.
public class ProjectEuler11 {
public static void main(String []args){
String digitNumber = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 2124 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 76";
String []numberVector = digitNumber.split(" ");
final int DIM = (int)Math.sqrt(numberVector.length + 1);
int [][]digitNumberVector = new int[DIM][DIM];
int vectorIdx = 0;
for(int parameter2 = 0; parameter2 < DIM; parameter2++){
for(int parameter3 = 0; parameter3 < DIM; parameter3++){
digitNumberVector[parameter2][parameter3] = Integer.parseInt(numberVector[vectorIdx]);
vectorIdx++;
}
}
}
}// Java Document
As a side note, your digitNumber string only contained 399 values instead of 400 so I had to add an extra integer so the equal-sized for loops would work properly.

Project Euler #11 java. Not getting the right answer

I have some problems with Problem 11 on Project Euler.
I manage to get an answer, but it's not the right one. I'm getting 51267216.
This is found by the greatestVert method. I think the problem is located in the greatestDiaglonal method, but I'm not quite sure. Can somebody check if my algorithm is correct?
The task is to find out what the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) is in the 20x20 grid.
package euler;
public class Problem11 {
int num1, num2, num3, num4;
int highestNum1, highestNum2, highestNum3, highestNum4;
private int sum = 0;
String[] l = {
"08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08",
"49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00",
"81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65",
"52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91",
"22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80",
"24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50",
"32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70",
"67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21",
"24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72",
"21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95",
"78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92",
"16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57",
"86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58",
"19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40",
"04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66",
"88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69",
"04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36",
"20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16",
"20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54",
"01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48"
};
int greatestSide() {
for(int i = 0;i<=19;i++) {
for(int n = 0;n<=l[i].length()-12;n+=3) {
num1 = Integer.parseInt(l[i].substring(0+n,3+n).trim());
num2 = Integer.parseInt(l[i].substring(3+n,6+n).trim());
num3 = Integer.parseInt(l[i].substring(6+n,9+n).trim());
num4 = Integer.parseInt(l[i].substring(9+n,12+n).trim());
if(num1*num2*num3*num4 > sum) {
sum = num1*num2*num3*num4;
highestNum1 = num1;
highestNum2 = num2;
highestNum3 = num3;
highestNum4 = num4;
}
}
}
return sum;
}
int greatestVert() {
for(int i = 0; i<=16; i++) {
for(int n = 0; n<=l[i].length()-3; n+=3) {
num1 = Integer.parseInt(l[i].substring(0+n,3+n).trim());
num2 = Integer.parseInt(l[i+1].substring(0+n,3+n).trim());
num3 = Integer.parseInt(l[i+2].substring(0+n,3+n).trim());
num4 = Integer.parseInt(l[i+3].substring(0+n,3+n).trim());
if(num1*num2*num3*num4 > sum) {
sum = num1*num2*num3*num4;
highestNum1 = num1;
highestNum2 = num2;
highestNum3 = num3;
highestNum4 = num4;
}
}
}
return sum;
}
int greatestDiagonal() {
for(int i = 19; i>=3; i--) {
for(int n = 0; n<=l[i].length()-12; n+=3) {
num4 = Integer.parseInt(l[i].substring(9+n,12+n).trim());
num3 = Integer.parseInt(l[i-1].substring(6+n,9+n).trim());
num2 = Integer.parseInt(l[i-2].substring(3+n,6+n).trim());
num1 = Integer.parseInt(l[i-3].substring(0+n,3+n).trim());
if(num1*num2*num3*num4 > sum) {
sum = num1*num2*num3*num4;
highestNum1 = num1;
highestNum2 = num2;
highestNum3 = num3;
highestNum4 = num4;
}
}
}
for(int i = 0; i>=16; i++) {
for(int n = 0; n<=l[i].length()-12; n+=3) {
num4 = Integer.parseInt(l[i].substring(9+n,12+n).trim());
num3 = Integer.parseInt(l[i+1].substring(6+n,9+n).trim());
num2 = Integer.parseInt(l[i+2].substring(3+n,6+n).trim());
num1 = Integer.parseInt(l[i+3].substring(0+n,3+n).trim());
if(num1*num2*num3*num4 > sum) {
sum = num1*num2*num3*num4;
highestNum1 = num1;
highestNum2 = num2;
highestNum3 = num3;
highestNum4 = num4;
}
}
}
return sum;
}
public static void main(String[] args) {
Problem11 prog = new Problem11();
prog.greatestSide();
prog.greatestVert();
prog.greatestDiagonal();
System.out.println(prog.sum);
System.out.println(prog.highestNum1);
System.out.println(prog.highestNum2);
System.out.println(prog.highestNum3);
System.out.println(prog.highestNum4);
}
}
You are omitting to scan one direction of diagonals:
for(int i = 0; i>=16; i++) {
doesn't run at all. You meant i <= 16 there.
Here is another solution that firstly converts the String to a 2D array:
public class P11 {
public static void main(String[] args) throws Exception {
String input = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48";
int n = 20;
int a[][] = getMatrixOfIntegers(input, n);
System.out.println(maxProduct(a));
}
private static long maxProduct(int a[][]) {
int size = a.length - 4;
int l = a.length - 1;
long pMax = 1;
for ( int i = 0 ; i < size ; i++ ) {
for ( int j = 0 ; j < size ; j++ ) {
int prodDiag1 = a[i][j]*a[i+1][j+1]*a[i+2][j+2]*a[i+3][j+3];
if ( pMax < prodDiag1 ) {
pMax = prodDiag1;
}
int prodCol = a[i][j]*a[i+1][j]*a[i+2][j]*a[i+3][j];
if ( pMax < prodCol ) {
pMax = prodCol;
}
int prodRow = a[i][j]*a[i][j+1]*a[i][j+2]*a[i][j+3];
if ( pMax < prodRow ) {
pMax = prodRow;
}
int prodDiag2 = a[i][l-j]*a[i+1][l-j-1]*a[i+2][l-j-2]*a[i+3][l-j-3];
if ( pMax < prodDiag2 ) {
pMax = prodDiag2;
}
}
}
return pMax;
}
private static int[][] getMatrixOfIntegers(String input, int n) {
String m[] = input.split(" ");
int a[][] = new int[n][n];
for ( int i = 0 ; i < n*n ; i++ ) {
a[i/n][i%n] = Integer.parseInt(m[i]);
}
return a;
}
}
Time for maxProduct: 0.2 ms

Categories