GitBucket
4.20.0
Toggle navigation
Sign in
Files
Branches
1
Tags
Issues
4
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
Jonathan
/
KA-Visual
Browse code
Added comments, fixed formatting and added counting when UnknownHostException is thrown.
master
1 parent
0d8746e
commit
b86fc8e0e80139bfc95043126087b5b8d5d7e364
Jonathan Ström
authored
on 25 Feb 2017
Patch
Showing
4 changed files
src/getUrlResults.java
src/getUrlResultsTest.java
src/mainClass.java
src/showGrid.java
Ignore Space
Show notes
View
src/getUrlResults.java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; public class getUrlResults { private static int UnknownHostExceptionCount = 0; public static List<String> getSearchResults(String searchText) { return getWordsList(downloadUrl(searchText), searchText); } public static List<String> getWordsList(List<String> linesList, String searchText) { List<String> resultList = new ArrayList<String>(); String lookForText = "<a class='detailslink' href='Details.aspx?word="; for (String line : linesList) { if (line.contains(lookForText)) { String word = line.substring(lookForText.length() + 2, lookForText.length() + 2 + searchText.length()); if (word.equals("%c3%")) continue; System.out.println(word); resultList.add(word); } } return resultList; } public static List<String> downloadUrl(String searchWord) { List<String> linesList = new ArrayList<String>(); URL url; InputStream is = null; BufferedReader br; String line; try { url = new URL("http://www.kryssakuten.se/Search.aspx?stype=ord-som-matchar-monster&sword=" + searchWord); is = url.openStream(); //Throws an IOException br = new BufferedReader(new InputStreamReader(is)); while ((line = br.readLine()) != null) { linesList.add(line); } } catch (MalformedURLException mue) { mue.printStackTrace(); } catch (UnknownHostException uhe) { System.err.println("Error: [" + ++UnknownHostExceptionCount + "] No internet connection."); } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if (is != null) is.close(); } catch (IOException ioe) { } } return linesList; } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; public class getUrlResults { public static List<String> getSearchResults(String searchText) { return getWordsList(downloadUrl(searchText), searchText); } public static List<String> getWordsList(List<String> linesList, String searchText) { List<String> resultList = new ArrayList<String>(); String lookForText = "<a class='detailslink' href='Details.aspx?word="; for (String line : linesList) { if (line.contains(lookForText)) { String word = line.substring(lookForText.length() + 2, lookForText.length() + 2 + searchText.length()); if (word.equals("%c3%")) continue; System.out.println(word); resultList.add(word); } } return resultList; } public static List<String> downloadUrl(String searchWord) { List<String> lines = new ArrayList<String>(); URL url; InputStream is = null; BufferedReader br; String line; try { url = new URL("http://www.kryssakuten.se/Search.aspx?stype=ord-som-matchar-monster&sword=" + searchWord); is = url.openStream(); // throws an IOException br = new BufferedReader(new InputStreamReader(is)); while ((line = br.readLine()) != null) { lines.add(line); } } catch (MalformedURLException mue) { mue.printStackTrace(); } catch (UnknownHostException uhe) { System.err.println("Error: No internet connection."); } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if (is != null) is.close(); } catch (IOException ioe) { // nothing to see here } } return lines; } }
Ignore Space
Show notes
View
src/getUrlResultsTest.java
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class getUrlResultsTest { public static void runAll() { printInfo("runOfflineTest", !getUrlResultsTest.runOfflineTest()); printInfo("runOnlineTest", !getUrlResultsTest.runOnlineTest()); } public static boolean runOnlineTest() { String correct = "EXEMPELSEXEMPLAREXEMPLENEXEMPLET"; String result = ""; for (String line : getUrlResults.getSearchResults("EXEMP???")) { result += line; } return (correct.equals(result)); } public static boolean runOfflineTest() { String correct = "EXEMPELSEXEMPLAREXEMPLENEXEMPLET"; String result = ""; for (String line : getUrlResults.getWordsList(getUrlFileFromFile(), "EXEMP???")) { result += line; } return (correct.equals(result)); } private static List<String> getUrlFileFromFile() { List<String> linesList = new ArrayList<String>(); try (BufferedReader br = new BufferedReader(new FileReader( "getUrlResultsTestContent"))) { // Read all lines from the file. String line = br.readLine(); while (line != null) { linesList.add(line); line = br.readLine(); } } catch (IOException ioe) { } return linesList; } private static void printInfo(String testMethod) { System.out.println("GetUrlResultsTest->" + testMethod + "(): [OK] Test completed successfully!"); } private static void printError(String testMethod) { System.err.println("GetUrlResultsTest->" + testMethod + "(): [ERROR] Test did not complete successfully!"); } private static void printInfo(String testMethod, boolean error) { if (error) printError(testMethod); else printInfo(testMethod); } }
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class getUrlResultsTest { public static void runAll() { printInfo("runOfflineTest", !getUrlResultsTest.runOfflineTest()); printInfo("runOnlineTest", !getUrlResultsTest.runOnlineTest()); } public static boolean runOnlineTest() { String correct = "EXEMPELSEXEMPLAREXEMPLENEXEMPLET"; String result = ""; for (String line : getUrlResults.getSearchResults("EXEMP???")) { result += line; } return (correct.equals(result)); } public static boolean runOfflineTest() { String correct = "EXEMPELSEXEMPLAREXEMPLENEXEMPLET"; String result = ""; for (String line : getUrlResults.getWordsList(getUrlListFromFile(), "EXEMP???")) { result += line; } return (correct.equals(result)); } private static List<String> getUrlListFromFile() { List<String> linesList = new ArrayList<String>(); try(BufferedReader br = new BufferedReader(new FileReader("getUrlResultsTestContent"))) { String line = br.readLine(); while (line != null) { linesList.add(line); line = br.readLine(); } } catch (IOException ioe) {} return linesList; } private static void printInfo(String testMethod) { System.out.println("GetUrlResultsTest->" + testMethod + "(): [OK] Test completed successfully!"); } private static void printError(String testMethod) { System.err.println("GetUrlResultsTest->" + testMethod + "(): [ERROR] Test did not complete successfully!"); } private static void printInfo(String testMethod, boolean error) { if (error) printError(testMethod); else printInfo(testMethod); } }
Ignore Space
Show notes
View
src/mainClass.java
//import javax.swing.JFrame; public class mainClass { public static void main(String[] args) { /* * JFrame window = new JFrame(); window.setSize(640, 480); * window.setTitle("KA-visual"); * window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); * window.setVisible(true); */ // getUrlResultsTest.runAll(); showGrid sg = new showGrid(); sg.run(); } }
//import javax.swing.JFrame; public class mainClass { public static void main(String[] args) { /*JFrame window = new JFrame(); window.setSize(640, 480); window.setTitle("KA-visual"); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true);*/ //getUrlResultsTest.runAll(); showGrid sg = new showGrid(); sg.run(); } }
Ignore Space
Show notes
View
src/showGrid.java
import java.util.ArrayList; import java.util.List; public class showGrid { private List<String[]> gridList = new ArrayList<String[]>(); public void run() { // Whatever you want to search for here. Any x or y length. gridList.add(new String[] { "H", " ", " ", "P" }); gridList.add(new String[] { "?", "G", "N", "?" }); gridList.add(new String[] { "L", "A", "?", "S" }); gridList.add(new String[] { "S", "?", "A", "S" }); gridList.add(new String[] { " ", "L", " ", "I" }); gridList.add(new String[] { " ", "E", "T", "O" }); gridList.add(new String[] { " ", "R", "I", "N" }); gridList.add(new String[] { " ", "I", "K", " " }); // First loop through and check for all words it can find. // Second loop check again to see if the previous check change anything for it. // Third loop: Same as second loop. for (int i = 0; i < 3; ++i) { if (!needChecking(gridList)) break; System.out.println("Starting round " + (i + 1)); checkGrid(gridList); } } /** * Loops through gridList and search for words where question marks is found. */ private void checkGrid(List<String[]> gridList) { for (int i = 0; i < gridList.size(); ++i) { for (int z = 0; z < gridList.get(i).length; ++z) { // Look for question marks. if (gridList.get(i)[z].equals("?")) { String x = searchX(i, z); String y = searchY(i, z); System.out.println(x); System.out.println(y); List<String> wordsX = getUrlResults.getSearchResults(x); List<String> wordsY = getUrlResults.getSearchResults(y); int indexX = x.indexOf("?"); int indexY = y.indexOf("?"); if (wordsX.size() == 1) { gridList.get(i)[z] = wordsX.get(0).charAt(indexX) + ""; continue; } if (wordsY.size() == 1) { gridList.get(i)[z] = wordsY.get(0).charAt(indexY) + ""; continue; } List<String> charX = new ArrayList<String>(); List<String> charY = new ArrayList<String>(); for (String word : wordsX) { charX.add(word.charAt(indexX) + ""); } for (String word : wordsY) { charY.add(word.charAt(indexY) + ""); } List<String> common = new ArrayList<String>(); for (int x3 = 0; x3 < charX.size(); ++x3) { for (int y3 = 0; y3 < charY.size(); ++y3) { if (charX.get(x3).equals(charY.get(y3))) { boolean exists = false; for (int c = 0; c < common.size(); ++c) { if (common.get(c).equals(charX.get(x3))) { exists = true; break; } } if (!exists) { common.add(charX.get(x3)); } } } } if (common.size() == 1) { gridList.get(i)[z] = common.get(0); } } } } // Print current grid-result. for (int i = 0; i < gridList.size(); ++i) { for (int z = 0; z < gridList.get(i).length; ++z) { System.out.print(gridList.get(i)[z]); } System.out.println(); } } /** * Search for a word in the X-axis. * @param i Y location to start looking at. * @param z X location to start looking at. * @return A word if one is found. */ private String searchX(int i, int z) { for (int x = z; x >= 0; --x) { if (x <= 0 || gridList.get(i)[x - 1].equals(" ")) { String word = ""; for (int x2 = x; x2 < gridList.get(i).length; ++x2) { if (gridList.get(i)[x2].equals(" ")) { break; } word += gridList.get(i)[x2]; } return word; } } return ""; } /** * Search for a word in the Y-axis. * @param i Y location to start looking at. * @param z X location to start looking at. * @return A word if one is found. */ private String searchY(int i, int z) { for (int y = i; y >= 0; --y) { if (y <= 0 || gridList.get(y - 1)[z].equals(" ")) { String word = ""; for (int y2 = y; y2 < gridList.size(); ++y2) { if (gridList.get(y2)[z].equals(" ")) { break; } word += gridList.get(y2)[z]; } return word; } } return ""; } /** * * @return A value whether a question mark exists or not in the grid. */ private boolean needChecking(List<String[]> gridList) { for (int i = 0; i < gridList.size(); ++i) { for (int z = 0; z < gridList.get(i).length; ++z) { if (gridList.get(i)[z].equals("?")) return true; } } return false; } }
import java.util.ArrayList; import java.util.List; public class showGrid { private List<String[]> gridList = new ArrayList<String[]>(); public void run() { //Whatever you want to search for here. Any x or y length. gridList.add(new String[] {"H", " ", " ", "P"}); gridList.add(new String[] {"?", "G", "N", "?"}); gridList.add(new String[] {"L", "A", "?", "S"}); gridList.add(new String[] {"S", "?", "A", "S"}); gridList.add(new String[] {" ", "L", " ", "I"}); gridList.add(new String[] {" ", "E", "T", "O"}); gridList.add(new String[] {" ", "R", "I", "N"}); gridList.add(new String[] {" ", "I", "K", " "}); //First loop through and check for all words it can find. //Second loop check again to see if the previous check change anything for it. //Third loop: Same as second loop. for (int i = 0; i < 3; ++i) { if (!needChecking()) break; System.out.println("Starting round " + (i + 1)); checkGrid(); } } private void checkGrid() { for (int i = 0; i < gridList.size(); ++i) { for (int z = 0; z < gridList.get(i).length; ++z) { //Look for question marks. if (gridList.get(i)[z].equals("?")) { String x = searchX(i, z); String y = searchY(i, z); System.out.println(x); System.out.println(y); List<String> wordsX = getUrlResults.getSearchResults(x); List<String> wordsY = getUrlResults.getSearchResults(y); int indexX = x.indexOf("?"); int indexY = y.indexOf("?"); if (wordsX.size() == 1) { gridList.get(i)[z] = wordsX.get(0).charAt(indexX) + ""; continue; } if (wordsY.size() == 1) { gridList.get(i)[z] = wordsY.get(0).charAt(indexY) + ""; continue; } List<String> charX = new ArrayList<String>(); List<String> charY = new ArrayList<String>(); for (String word : wordsX) { charX.add(word.charAt(indexX) + ""); } for (String word : wordsY) { charY.add(word.charAt(indexY) + ""); } List<String> common = new ArrayList<String>(); for (int x3 = 0; x3 < charX.size(); ++x3) { for (int y3 = 0; y3 < charY.size(); ++y3) { if (charX.get(x3).equals(charY.get(y3))) { boolean exists = false; for (int c = 0; c < common.size(); ++c) { if (common.get(c).equals(charX.get(x3))) { exists = true; break; } } if (!exists) { common.add(charX.get(x3)); } } } } if (common.size() == 1) { gridList.get(i)[z] = common.get(0); } } } } //Print current grid-result. for (int i = 0; i < gridList.size(); ++i) { for (int z = 0; z < gridList.get(i).length; ++z) { System.out.print(gridList.get(i)[z]); } System.out.println(); } } private String searchX(int i, int z) { for (int x = z; x >= 0; --x) { if (x <= 0 || gridList.get(i)[x - 1].equals(" ")) { String word = ""; for (int x2 = x; x2 < gridList.get(i).length; ++x2) { if (gridList.get(i)[x2].equals(" ")) { break; } word += gridList.get(i)[x2]; } return word; } } return ""; } private String searchY(int i, int z) { for (int y = i; y >= 0; --y) { if (y <= 0 || gridList.get(y - 1)[z].equals(" ")) { String word = ""; for (int y2 = y; y2 < gridList.size(); ++y2) { if (gridList.get(y2)[z].equals(" ")) { break; } word += gridList.get(y2)[z]; } return word; } } return ""; } private boolean needChecking() { for (int i = 0; i < gridList.size(); ++i) { for (int z = 0; z < gridList.get(i).length; ++z) { if (gridList.get(i)[z].equals("?")) return true; } } return false; } }
Show line notes below