翻译显示奇怪的字符

时间:2016-07-19 00:59:16

标签: java netbeans

我通过从翻译网站获取HTML代码来翻译一个单词。

使用NetBeans运行代码时,转换是正确的,但是

运行jar文件,我看到未知语言......

任何帮助,请.....

来自netbeans:

From netbeans

来自jar文件:

enter image description here

代码:

`/*
 * 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));
        }

    }
}


`

1 个答案:

答案 0 :(得分:2)

通过改变来解决......

Scanner input = new Scanner(url.openStream());

为...

Scanner input = new Scanner(url.openStream(), "UTF-8");

...以便使用适当的编码。