1

私は indexoutofboundsexception を持っています。私はインターネットを精査し、いくつかの異なる解決策を見てきましたが、このコードではうまくいきませんでした。

ここに私のエラーメッセージがあります:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at jxl.write.biff.WritableWorkbookImpl.getSheet(WritableWorkbookImpl.java:408)
at Segmentation.main(Segmentation.java:81)

81行目:

WritableSheet sheet = copy.getSheet(0); 

いくつかの助けをいただければ幸いです。ありがとう!

import java.io.File; 
import java.util.Date; 
import jxl.*; 
import java.io.IOException;
import jxl.read.biff.*;
import jxl.write.*;
import jxl.Workbook;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class Segmentation{



    private static  final String googleTranslate = "http://http://translate.google.com/#zh-CN|zh-TW|";

    public static String translate(String string) {

        String translation = googleTranslate + string;
        URL translationURL;

        try { translationURL = new URL(translation); }
        catch(MalformedURLException e) { return e.getMessage(); }

        BufferedReader httpin;
        String fullPage = "";
        System.out.println(translation);
        try {
            httpin = new BufferedReader(
                    new InputStreamReader(translationURL.openStream()));
            String line;
            while((line=httpin.readLine()) != null) { fullPage += line + '\n'; }
            httpin.close();
        } catch(IOException e) { return e.getMessage(); }

        int begin = fullPage.indexOf("<span class=\"\">");
        int end = fullPage.indexOf("</span>");

        return fullPage.substring(begin + 15, end);

    }


public static void main(String[] args) throws IOException, WriteException, BiffException
    {

        /*try
        {
            Workbook workbook = Workbook.getWorkbook(new File("DATA_Chinese_All.xls"));
            Sheet sheet = workbook.getSheet(0);



        }
        catch (IOException e)
        {   
            e.printStackTrace();
        }
        catch (BiffException e)
        {
            e.printStackTrace();}*/



            Workbook workbook = Workbook.getWorkbook(new File("DATA_Chinese_All.xls"));
            WorkbookSettings s = new WorkbookSettings();  
            s.setUseTemporaryFileDuringWrite(true);  
            WritableWorkbook copy = Workbook.createWorkbook(new File("someFile.xls"), s);
            //WritableWorkbook copy = Workbook.createWorkbook(new File("output.xls"), workbook);


            WritableSheet sheet = copy.getSheet(0); 




            int max = 302;
            int count = 0;
            for(int i = 1; i < max; i++)
            {   
                WritableCell cell  = sheet.getWritableCell(1, i);

                if (cell.getType() == CellType.LABEL);

                {

                    Label l = (Label) cell;
                    //Result = "http://translate.google.com/#zh-CN|zh-TW|"+(l.getContents();)
                    l.setString(translate(l.getContents()));

                    //l.setString();
                }   
                count++;

            copy.write();
            copy.close();






    } 




}
}
4

1 に答える 1

0

getSheetの代わりにcreateSheetを使用してみてください

http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/write/WritableWorkbook.html#createSheet%28java.lang.String,%20int%29

于 2012-07-07T21:27:00.210 に答える