Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translation showing strange characters

Tags:

java

netbeans

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 netbeans

From jar file :

enter image description here

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\">&diams;</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));
        }

    }
}


`
like image 712
Amr Emaish Avatar asked Oct 30 '22 23:10

Amr Emaish


1 Answers

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.

like image 76
Amr Emaish Avatar answered Nov 15 '22 05:11

Amr Emaish