I translated a word by getting HTML Code from translation website.
the translation is right while running the code with NetBeans , but while
running with jar file , I see unknown language ...
any help, please.....
From netbeans :
From jar file :
the code :
`/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication5;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
String URLString = "http://www.systranet.com/dictionary/english-arabic/play";
ArrayList<String> wordList = new ArrayList<>();
String FlangMarker = "<span class=\"dl_target_bullet\">♦</span><span class=\"dl_target_word\">";
try {
java.net.URL url = new java.net.URL(URLString);
Scanner input = new Scanner(url.openStream());
while (input.hasNext()) {
String line = input.nextLine();
// System.out.println(line);
String word = "";
if (line.contains(FlangMarker)) {
for (int i = FlangMarker.length(); line.charAt(i) != '<'; i++) {
word += line.charAt(i);
}
wordList.add(word);
}
}
} catch (java.net.MalformedURLException ex) {
System.out.println("Invalid World");
} catch (java.io.IOException ex) {
System.out.println("I/O Errors: no such file");
}
for (int i = 0; i < wordList.size(); i++) {
JOptionPane.showMessageDialog(null, wordList.get(i));
}
}
}
`
Solved by changing...
Scanner input = new Scanner(url.openStream());
to...
Scanner input = new Scanner(url.openStream(), "UTF-8");
... in order to use the appropriate encoding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With