数据结构---公交线路提示系统05(内附读取表格+迪杰斯特拉算法Java代码)
今天做的最多的事情就是纠错了,
通过添加输出语句判断错误来源;
找到错误来源:
wb = new XSSFWorkbook(input);//语句创建错误
网上查询发现是jar包的问题;
下图为poi的jar包各个用途:(本人需要的是excel)
读取表格:
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelUtil { private static String substring; public static void main(String[] args) throws IOException{ try { //输入文件名 System.out.println("开始读入表格"); ExcelUtil.read("D://dns.xls"); } catch (IOException e) { e.printStackTrace(); System.out.println("读入失败"); } } public static void read(String filePath) throws IOException { //判断是xls还是xlsx String fileType = filePath.substring(filePath.lastIndexOf(".") + 1,filePath.length()); InputStream input = new FileInputStream(filePath); Workbook wb = null; if (fileType.equals("xls")) { System.out.println("您的excel格式为.xls"); wb = new HSSFWorkbook(input); System.out.println("成功读取表格"); } else if (fileType.equals("xlsx")) { wb= new XSSFWorkbook(input); } else { System.out.println("您输入的excel格式不正确"); } //得到一个工作表对象; System.out.println("得到一个工作表对象"); Sheet sheet = wb.getSheetAt(0); int rsRows = sheet.getLastRowNum();// 获取sheet表中的总行数 // 遍历行 System.out.println("遍历行"); for (int i=0;i<=rsRows;i++) { Row row = sheet.getRow(i); StringBuffer sb = new StringBuffer(); System.out.println("遍历单元格"); //遍历单元格 for(int j=0;j<row.getLastCellNum();j++){ String value = null; Cell cell = row.getCell(j); //判断单元格是否为空 System.out.println("判断单元格是否为空"); if(cell==null||cell.equals(null)||cell.getCellType()==HSSFCell.CELL_TYPE_BLANK){//空值 value="null"; }else { //判断数据类型 switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_FORMULA:value = "" + cell.getCellFormula();//公式型 break; case HSSFCell.CELL_TYPE_NUMERIC:value = "" + cell.getNumericCellValue();//数值型 break; case HSSFCell.CELL_TYPE_STRING:value = cell.getStringCellValue();//字符串型 break; } } sb.append(value + " "); substring = sb.substring(0, sb.length()-1); } //转换为数组 String[] strings = sb.toString().split(","); System.out.println(substring.toString()); System.out.println("操作完成!"); for(int a=0;a<strings.length;a++) { System.out.println(strings[a]); } } } }
后来又报错:HSSFCell.CELL_TYPE_STRING、BOOLEAN、NUMERIC无定义!
新版:
if(cell==null||cell.equals(null)||cell.getCellType()==CellType.BLANK){ value="null"; }else { //判断数据类型 switch (cell.getCellType()) { case FORMULA:value = "" + cell.getCellFormula(); break; case NUMERIC:value = "" + cell.getNumericCellValue(); break; case STRING:value = cell.getStringCellValue(); break; default: break; } }
运行截图:
迪杰斯特拉算法:
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Dijkstra{ public static void main(String[] args) throws IOException{ @SuppressWarnings("resource") Scanner sc=new Scanner(System.in); System.out.println("输入起点与终点:"); int a[]=new int[2]; for(int i=0;i<2;i++){ a[i]=sc.nextInt(); } int vs=a[0]; int vf=a[1]; Dijkstra.dijkstra(vs,vf); } public static List<String> readTxtFile(String filePath) { /** * 读取文档 * @param filePath * @return */ List<String> list = new ArrayList<String>(); try { String encoding = "UTF-8"; File file = new File(filePath); if (file.isFile() && file.exists()) { InputStreamReader read = new InputStreamReader( new FileInputStream(file), encoding); BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while ((lineTxt = bufferedReader.readLine()) != null) { if (!lineTxt.startsWith("#")) list.add(lineTxt); } read.close(); } else { System.out.println("找不到文件"); } } catch (Exception e) { System.out.println("出错了"); e.printStackTrace(); } return list; } public static String[][] createArray(String filePath){ /** * 读取文档生成二维数组 * @param filePath * @return */ List<String> list = readTxtFile(filePath); System.out.println("读取成功"); String[][] array = new String[list.size()][]; for(int i=0;i<list.size();i++){ array[i] = new String[list.size()]; String linetxt=list.get(i); String[] myArray = linetxt.replaceAll("\\s+", "@").split("@"); for(int j=0;j<myArray.length;j++){ array[i][j]=myArray[j]; } } return array; } public static int[][] str2int(String[][] str) { int a,b; a = str.length; b = str[0].length; int result[][] = new int[a][b]; for(int i = 0 ; i < a ; ++ i) for(int j = 0 ; j < b ; ++ j) { result[i][j] = Integer.parseInt(str[i][j]); } return result; } public static void printArray(String array[][]){//打印输出,观察二维数组是否正确;纠错用 for(int i=0;i<array.length;i++){ for(int j=0;j<array[i].length;j++){ if(j!=array[i].length-1){ System.out.print("array["+i+"]["+j+"]="+array[i][j]+","); } else{ System.out.print("array["+i+"]["+j+"]="+array[i][j]); } } System.out.println(); } } public static void dijkstra(int vs,int vf) { /** * Dijkstra最短路径。 * 即图中"节点vs"到其它各个节点的最短路径。 * @param vs 起始节点 * @param Graph 图 */ String[][] str= createArray("D:\\text.txt"); System.out.println("成功创建二维字符串数组"); printArray(str); //将读取的String型二维数组转化为int型 int[][]Graph =str2int(str); System.out.println("成功转化为整数组"); int NUM = Graph[0].length; int[] prenode = new int[NUM];// 前驱节点数组 int[] path = new int[NUM];// 最短距离数组 boolean[] flag = new boolean[NUM];// 该节点是否已经找到最短路径 int vnear = 0;//距离vs最近的节点 //初始化 for (int i = 0; i <path.length; i++) { prenode[i] = i; path[i] = Graph[vs][i];//顶点i的最短路径为顶点vs到i的权 flag[i] = false; } flag[vs] = true;//vs自身初始化 //遍历 Graph.length-1次,找出每个顶点的最短路径 for (int v = 1; v < Graph.length; v++) { // 每次循环求得当前距离vs最近的顶点vnear和最短距离min int min = 100000;//100000表示无穷 for (int j = 0; j < Graph.length; j++) { if (!flag[j] && path[j] < min) { min = path[j]; vnear = j; } } //标记顶点vnear为已经获取到最短路径 flag[vnear] = true; // 根据vnear更新vs到其他所有节点的前驱节点和最短路径 for (int k = 0; k < Graph.length; k++) { if (!flag[k] && (min + Graph[vnear][k]) < path[k]) { prenode[k] = vnear; path[k] = min + Graph[vnear][k]; } } } System.out.println(";总公里数=" + path[vf]); System.out.println("起点"+vs+"到终点"+vf+"的最短路径为:"); System.out.print("终点<-" + vf + "前驱" + prenode[vf]); //依次输出前驱 do{ vf=prenode[vf]; System.out.print("<-前驱" + prenode[vf]); }while(prenode[vf]==vs); } }
运行截图: