有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将此坐标图拆分为除第一个数字外的其他数字,并将(x,y)存储为二维数组

as the title,i want to split the coordinate chart as following

拆分后,应该是88 72 47 40 13 67.... 并将x和y值存储到数组中 如何修改。拆分(“”),如果使用

.split(""tab"")

它完全拆分字符串,而不忽略第一个数字 ,以及如何将这些数字存储到二维数组中

package coordinates;
import java.util.*;
import java.io.*; //scanner and reader
public class Path {
public static void readfromtxt() throws Exception{
    FileReader File = new FileReader("coordinations.txt");
    BufferedReader Reader= new BufferedReader(File);
    String text="";
    String textsub="";//to isolate first line
    String line=Reader.readLine();
        textsub=text+line+"\n";
        while(line!=null){//if there are lines
            text=text+line+"\n";//pass data of line into text
            line=Reader.readLine();//load data of line
            String[]textArrays=line.split(" ");//split by tab 1.1 2.88 3.72
            for(String textArray:textArrays){
                int coordinates=Integer.parseInt(textArray);// to int 
                System.out.println(coordinates);}}

共 (2) 个答案

  1. # 1 楼答案

    将最后一位从

    for(String textArray:textArrays){
        int coordinates=Integer.parseInt(textArray);// to int 
            System.out.println(coordinates);}}
    

    int x = Integer.parseInt(textArrays[0]);
    int y = Integer.parseInt(textArrays[1]);
    System.out.println("X Y Coordinates: (%i, %i)", x, y);
    
  2. # 2 楼答案

    以下是一个可运行的工作解决方案(注释解释了某些部分):

    package coordinates;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Path {
        public static void main(String[] args) throws IOException {
            readFromTxt();
        }
    
        public static void readFromTxt() throws IOException {
            FileReader file = new FileReader("coordinations.txt");
            BufferedReader reader = new BufferedReader(file);
            String line = reader.readLine(); // Skip row 0
            List<String> lines = new ArrayList<String>();
    
            while ((line = reader.readLine()) != null) { // While there are lines
                lines.add(line);
            }
    
            int[][] array = new int[lines.size()][2]; // Two dimensional array
    
            for (int i = 0; i < lines.size(); i++) { // For each line
                String[] l = lines.get(i).split("\t"); // l has length 3
                array[i][0] = Integer.parseInt(l[1]); // 88 (index 1 means second
                                                        // column)
                array[i][1] = Integer.parseInt(l[2]); // 72 (index 2 means third
                                                        // column)
                System.out.println((i + 1) + " (x, y): (" + array[i][0] + ", "
                        + array[i][1] + ")");
            }
    
            reader.close();
        }
    }
    

    这将删除顶行,忽略第一列,并提供二维数组(array)。 注意:使用throws Exception通常是不好的,因为这可能会阻止您捕获并注意到代码中的特定问题。这个程序唯一需要抛出的异常是IOException

    我还更改了一些方法和变量名,以适应Java的编码标准