gpt4 book ai didi

java - jexcel API 中的 IndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-01 05:15:04 24 4
gpt4 key购买 nike

我遇到了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();






}




}
}

最佳答案

关于java - jexcel API 中的 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11378688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com