要使用多种方法,我必须多次使用相同的检查异常catch(请参阅Java:checked vs uncheckedexception解释)。结果是重复的代码行。我想要某种减少重复异常的方法,catch以减少代码行数。
什么是减少重复catch节的数量并减少代码行的好方法?有什么方法可以catch在另一个文件中写入该操作并明确使用它?
基本上,我想减少行数,并使代码更简洁,更易于阅读。
这是我的代码示例:
@RequestMapping(value="")
public @ResponseBody Response addMultiple(){
if() {
try {
data = new weight();
}
catch( AmazonServiceException se ){
response = x;
}
catch (AmazonClientException ce) {
response = y;
}
catch(JsonProcessingException je) {
response = z;
}
}
@RequestMapping(value="")
public @ResponseBody Response addMultiple2(){
if() {
try {
data = new height();
}
catch( AmazonServiceException se ){
response = x;
}
catch (AmazonClientException ce) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Java从给定.xlsx文件中读取数据。
我的读取文件的代码如下:
public static void main(String[] args) throws Exception {
FileInputStream file = new FileInputStream(new File("E:\\test1.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet test = workbook.getSheetAt(0);
student emp = new student();
Iterator<Row> itr = test.iterator();
itr.next();
while(itr.hasNext()){
Row row = itr.next();
emp.reedData(row);
System.out.println(id+","+name+","+options);
}
Run Code Online (Sandbox Code Playgroud)
方法如下:
void reedData(Row row){
id= row.getCell(0).toString();
name= row.getCell(1).toString();
options= row.getCell(2).toString();
}
Run Code Online (Sandbox Code Playgroud)
但是,我得到这样的输出:
1.0,X,play game
,,sing song
2.0,Y,play game
,,sing song
Run Code Online (Sandbox Code Playgroud)
我希望输出看起来像这样,而不是上面的:
1.0,X,{play game,sing song}
2.0,Y,{play game,sing song}
Run Code Online (Sandbox Code Playgroud)
这个问题是因为我正在合并.xlsx文件中的两个单元格。
有什么建议吗?先感谢您..