Java 有没有办法修复我的邻接列表图的主类?

Java 有没有办法修复我的邻接列表图的主类?,java,Java,所以基本上我想从一个文件中获取数字 把它变成这个前男友 我们假设创建一个图类并存储图的邻接列表表示。我们还假设使用数组列表来实现。因此,我得到了一些帮助来创建graph类,我正在创建它,以便通过graph类处理用户文件,但由于某种原因,出现了错误,输出不正确。有人能帮忙吗 图形类 import java.util.ArrayList; public class Graph { ArrayList<Integer> [] nodes; int n_nodes; public G

所以基本上我想从一个文件中获取数字

把它变成这个前男友

我们假设创建一个图类并存储图的邻接列表表示。我们还假设使用数组列表来实现。因此,我得到了一些帮助来创建graph类,我正在创建它,以便通过graph类处理用户文件,但由于某种原因,出现了错误,输出不正确。有人能帮忙吗

图形类

import java.util.ArrayList;

public class Graph {
ArrayList<Integer> [] nodes;
int n_nodes;

public Graph(int numberNodes){
    this.nodes = new ArrayList[numberNodes+1];
    this.n_nodes = numberNodes;
    for(int i = 0; i < n_nodes + 1; i++){
        nodes[i] = new ArrayList<>();
    }
}


public void addNeighbor(int node, int neighbor){
    nodes[node].add(neighbor);
}

public String toString(){
    StringBuilder myGraph = new StringBuilder();
    for(int i = 1; i < nodes.length; i++){
        myGraph.append(i);
        ArrayList<Integer> neighbors = nodes[i];
        int totalNeighbors = neighbors.size();
        for(int j = 0; j < totalNeighbors; j++){
            int myneighbor = neighbors.get(j);
            myGraph.append(" -> " + myneighbor);
        }
        myGraph.append('\n');
    }
    return myGraph.toString();
   }}
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Program1 {
public static void main(String[] args) throws IOException {

    File file = null;
    JFileChooser chooser = new JFileChooser();
    Scanner ans = new Scanner(System.in);

        int result = chooser.showOpenDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
        } else {
            JOptionPane.showMessageDialog(null, "No File Selected");
            System.exit(1);
        }

        Scanner input = new Scanner(file);

        int y = input.nextInt();
    int x = 0;
    Graph graph = new Graph(y);
    while (input.hasNextLine()) {
        for (int i = 0; i < y; i++) {
            x = input.nextInt();
            graph.addNeighbor(i, x);
        }
        System.out.println(graph.toString());
    }
    }}
import java.util.ArrayList;
公共类图{
ArrayList[]节点;
int n_节点;
公共图(整数节点){
this.nodes=new ArrayList[numberNodes+1];
此.n_节点=数节点;
对于(int i=0;i”+myneighbor);
}
myGraph.append('\n');
}
返回myGraph.toString();
}}
主类

import java.util.ArrayList;

public class Graph {
ArrayList<Integer> [] nodes;
int n_nodes;

public Graph(int numberNodes){
    this.nodes = new ArrayList[numberNodes+1];
    this.n_nodes = numberNodes;
    for(int i = 0; i < n_nodes + 1; i++){
        nodes[i] = new ArrayList<>();
    }
}


public void addNeighbor(int node, int neighbor){
    nodes[node].add(neighbor);
}

public String toString(){
    StringBuilder myGraph = new StringBuilder();
    for(int i = 1; i < nodes.length; i++){
        myGraph.append(i);
        ArrayList<Integer> neighbors = nodes[i];
        int totalNeighbors = neighbors.size();
        for(int j = 0; j < totalNeighbors; j++){
            int myneighbor = neighbors.get(j);
            myGraph.append(" -> " + myneighbor);
        }
        myGraph.append('\n');
    }
    return myGraph.toString();
   }}
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Program1 {
public static void main(String[] args) throws IOException {

    File file = null;
    JFileChooser chooser = new JFileChooser();
    Scanner ans = new Scanner(System.in);

        int result = chooser.showOpenDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
        } else {
            JOptionPane.showMessageDialog(null, "No File Selected");
            System.exit(1);
        }

        Scanner input = new Scanner(file);

        int y = input.nextInt();
    int x = 0;
    Graph graph = new Graph(y);
    while (input.hasNextLine()) {
        for (int i = 0; i < y; i++) {
            x = input.nextInt();
            graph.addNeighbor(i, x);
        }
        System.out.println(graph.toString());
    }
    }}
导入java.io.File;
导入java.io.IOException;
导入javax.swing.JFileChooser;
导入javax.swing.JOptionPane;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
公共课程1{
公共静态void main(字符串[]args)引发IOException{
File=null;
JFileChooser chooser=新的JFileChooser();
扫描仪ans=新扫描仪(System.in);
int result=chooser.showOpenDialog(null);
if(result==JFileChooser.APPROVE\u选项){
file=chooser.getSelectedFile();
}否则{
showMessageDialog(null,“未选择文件”);
系统出口(1);
}
扫描仪输入=新扫描仪(文件);
int y=input.nextInt();
int x=0;
图形=新图形(y);
while(input.hasNextLine()){
for(int i=0;i
另外,我是整个stackoverflow网站的新手,如果我的措辞不清楚或者我的代码格式不够好,我很抱歉

编辑

这就是它显示的错误

作为
3
作为读取者

int y = input.nextInt();
如果在另一行,则您还需要阅读
CR-LF

我个人会使用以下范例

str = input.nextLine () 
// convert to int
while (input.hasNextLine) {
    str = input.nextLine () 
    arr[] = str.split (" ");
    // loop through length of arr - in your code `y` is not updated
        add arr[x] to graph
所以基本上你想要

    Scanner input = new Scanner(file);

    String line = input.nextLine();
    int y = 0;
    if (line != null) {
        y = Integer.parseInt(line.trim());  
    }

    Graph graph = new Graph(y);
    while (input.hasNextLine() && y > 0) {
        if (input.hasNextLine()) {
            line = input.nextLine();
        }
        String nums[] = line.split(" ");
        for (int i = 0; i < nums.length; i++) {

            int x = Integer.parseInt(nums[i]);  
            graph.addNeighbor(i, x);
        }
        System.out.println(graph.toString());
    }
扫描仪输入=新扫描仪(文件);
String line=input.nextLine();
int y=0;
如果(行!=null){
y=Integer.parseInt(line.trim());
}
图形=新图形(y);
while(input.hasNextLine()&&y>0){
if(input.hasNextLine()){
line=input.nextLine();
}
字符串nums[]=line.split(“”);
对于(int i=0;i
还可以尝试使用
input.hasNextInt()
我已经尝试过了,但都是相同的错误。很抱歉,我感到困惑,不太明白您的意图。您希望我在哪里实现该代码?