I would like to get string
that starts with another string
(that I should fix it) I used the following codes but I get also string
that begins with other string
that I wouldn't like to get!
if (critere.equals("Date")) {
String date = jTextField5.getText();
try {
FileInputStream file = new FileInputStream(new File(jTextField1.getText()));
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
for (Row row : sheet) {
for (Cell cell : row) {
/* SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date d = dateFormat.parse(cell.getRichStringCellValue().getString());
SimpleDateFormat printFormat1 = new SimpleDateFormat("MM/dd/yyyy");
Date dd = printFormat1.parse(printFormat1.format(d));*/
if (cell.getRichStringCellValue().getString().trim().startsWith(date)) {// I have problem here
CellReference ref = new CellReference("A1");
CellReference ref1 = new CellReference("B1");
CellReference ref2 = new CellReference("C1");
CellReference ref3 = new CellReference("D1");
CellReference ref4 = new CellReference("E1");
CellReference ref5 = new CellReference("G1");
CellReference ref6 = new CellReference("I1");
CellReference ref7 = new CellReference("J1");
Cell c = row.getCell(ref.getCol());
Cell c1 = row.getCell(ref1.getCol());
Cell c2 = row.getCell(ref2.getCol());
Cell c3 = row.getCell(ref3.getCol());
Cell c4 = row.getCell(ref4.getCol());
Cell c5 = row.getCell(ref5.getCol());
Cell c6 = row.getCell(ref6.getCol());
Cell c7 = row.getCell(ref7.getCol());
if (c.getStringCellValue().equals("Warning")) {
((DefaultTableModel) jTable1.getModel()).addRow(new Object[]{c.getStringCellValue(), c1.getStringCellValue(), c2.getStringCellValue(),
c3.getStringCellValue(), c4.getStringCellValue(), c5.getStringCellValue(), c6.getStringCellValue(), c7.getStringCellValue()});
}
if (c.getStringCellValue().equals("Minor")) {
((DefaultTableModel) jTable2.getModel()).addRow(new Object[]{c.getStringCellValue(), c1.getStringCellValue(), c2.getStringCellValue(),
c3.getStringCellValue(), c4.getStringCellValue(), c5.getStringCellValue(), c6.getStringCellValue(), c7.getStringCellValue()});
}
if (c.getStringCellValue().equals("Major")) {
((DefaultTableModel) jTable3.getModel()).addRow(new Object[]{c.getStringCellValue(), c1.getStringCellValue(), c2.getStringCellValue(),
c3.getStringCellValue(), c4.getStringCellValue(), c5.getStringCellValue(), c6.getStringCellValue(), c7.getStringCellValue()});
}
if (c.getStringCellValue().equals("Critical")) {
((DefaultTableModel) jTable4.getModel()).addRow(new Object[]{c.getStringCellValue(), c1.getStringCellValue(), c2.getStringCellValue(),
c3.getStringCellValue(), c4.getStringCellValue(), c5.getStringCellValue(), c6.getStringCellValue(), c7.getStringCellValue()});
}
}
}
}
I would like to enter this loop when the date starts with a date that I should enter in my application as input but when I run my app it enter to this loop even the date didn't start with the value that I did enter
See this example: http://www.tutorialspoint.com/java/java_string_startswith.htm
Maybe your data variable is not a String?
public class Test{
public static void main(String args[]){
String str = new String("Welcome to Tutorialspoint.com");
System.out.print("Return Value :" );
System.out.println(str.startsWith("Welcome") );
System.out.print("Return Value :" );
System.out.println(str.startsWith("Tutorials") );
System.out.print("Return Value :" );
System.out.println(str.startsWith("Tutorials", 11) );
}
}
This produces the following result:
Return Value :true
Return Value :false
Return Value :true
I presume the date
is a String
variable so you need to check if it is a subString of the whole and if the index of its first occurrence is 0
If date
is not a String
first transform it into one
if(cell.getRichStringCellValue().getString().trim().indexOf(date.toString()) == 0);
我解决了我在另一个行中有另一个以字符串date开头的列的问题,因此这就是为什么当我运行代码时,它也要以另一个列中的date开头的字符串
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.