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 so it's downloading and searching for the correct words.
master
1 parent
f4784b2
commit
553181b000c51f823061e1dcb4568ab8283125c3
Jonathan Ström
authored
on 21 Feb 2017
Patch
Showing
2 changed files
src/getUrlResults.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 { 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; } }
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.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)) { System.out.println(line.substring(lookForText.length() + 2, lookForText.length() + 2 + searchText.length())); resultList.add(line.substring(lookForText.length() + 2, lookForText.length() + 2 + searchText.length())); } } 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 (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/showGrid.java
0 → 100644
import java.util.ArrayList; import java.util.List; public class showGrid { private List<String[]> gridList = new ArrayList<String[]>(); private List<String[]> gridList2 = new ArrayList<String[]>(); private List<String[]> gridList3 = new ArrayList<String[]>(); public void run() { gridList3.add(new String[] { " ", " ", " ", " " }); gridList3.add(new String[] { " ", " ", " ", " " }); gridList3.add(new String[] { " ", " ", " ", " " }); gridList3.add(new String[] { " ", " ", " ", " " }); gridList3.add(new String[] { " ", " ", " ", " " }); gridList3.add(new String[] { " ", " ", " ", " " }); gridList3.add(new String[] { " ", " ", " ", " " }); gridList3.add(new String[] { " ", " ", " ", " " }); gridList2.add(new String[] { " ", " ", " ", " " }); gridList2.add(new String[] { " ", " ", " ", " " }); gridList2.add(new String[] { " ", " ", " ", " " }); gridList2.add(new String[] { " ", " ", " ", " " }); gridList2.add(new String[] { " ", " ", " ", " " }); gridList2.add(new String[] { " ", " ", " ", " " }); gridList2.add(new String[] { " ", " ", " ", " " }); gridList2.add(new String[] { " ", " ", " ", " " }); 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", " "}); for (int i = 0; i < gridList.size(); ++i) { for (int z = 0; z < gridList.get(i).length; ++z) { if (!gridList.get(i)[z].equals(" ")) { if (z + 1 < gridList.get(i).length && i + 1 < gridList.size()) { if (!gridList.get(i)[z + 1].equals(" ") && !gridList.get(i + 1)[z].equals(" ")) { gridList2.get(i)[z] = "B"; } else if (!gridList.get(i)[z + 1].equals(" ")) { gridList2.get(i)[z] = "R"; } else if (!gridList.get(i + 1)[z].equals(" ")) { gridList2.get(i)[z] = "D"; } } else { if (z + 1 == gridList.get(i).length && i + 1 < gridList.size()) { gridList2.get(i)[z] = "D"; } else if (z + 1 < gridList.get(i).length && i + 1 == gridList.size()) { gridList2.get(i)[z] = "R"; } else { gridList2.get(i)[z] = "E"; } } } System.out.print(gridList2.get(i)[z]); } System.out.println(); } if (needChecking()) checkGrid(); if (needChecking()) checkGrid(); if (needChecking()) checkGrid(); //List words found /*for (int i = 0; i < gridList.size(); ++i) { for (int z = 0; z < gridList.get(i).length; ++z) { if (gridList2.get(i)[z].equals("B")) { if (i <= 0 || gridList2.get(i - 1)[z].equals(" ")) { String word = ""; for (int y = i; y < gridList.size(); ++y) { if (y + 1 < gridList.size() && gridList.get(y)[z].equals(" ")) { break; } word += gridList.get(y)[z]; } System.out.println("Word3: " + word); } if (z <= 0 || gridList2.get(i)[z - 1].equals(" ")) { String word = ""; for (int x = z; x < gridList.get(i).length; ++x) { if (x + 1 < gridList.size() && gridList.get(i)[x].equals(" ")) { break; } word += gridList.get(i)[x]; } System.out.println("Word3: " + word); } } if (gridList2.get(i)[z].equals("D")) { if (i <= 0 || gridList2.get(i - 1)[z].equals(" ")) { String word = ""; for (int y = i; y < gridList.size(); ++y) { if (y + 1 < gridList.size() && gridList.get(y)[z].equals(" ")) { break; } word += gridList.get(y)[z]; } System.out.println("Word: " + word); } } else if(gridList2.get(i)[z].equals("R")) { if (z <= 0 || gridList2.get(i)[z - 1].equals(" ")) { String word = ""; for (int x = z; x < gridList.get(i).length; ++x) { if (x + 1 < gridList.size() && gridList.get(i)[x].equals(" ")) { break; } word += gridList.get(i)[x]; } System.out.println("Word2: " + word); } } } } */ } 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); } } } } 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 || gridList2.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 || gridList2.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