Java:从文本文件读取&&;检查图表是否完整
我有一个学校作业,大部分时间我都完成了,但我不知道如何从文本文件中读取,我在代码方面遇到了一些问题,给我的任务是: 写一个程序来读取一个名为“input.txt”的输入文件 由多个图形组成。每个图形以包含 顶点数n,其中1Java:从文本文件读取&&;检查图表是否完整,java,algorithm,Java,Algorithm,我有一个学校作业,大部分时间我都完成了,但我不知道如何从文本文件中读取,我在代码方面遇到了一些问题,给我的任务是: 写一个程序来读取一个名为“input.txt”的输入文件 由多个图形组成。每个图形以包含 顶点数n,其中1
package phase1project;
import java.util.Scanner;
public class Phase1Project {
public static void main(String[] args) {
int max = 2000; //declare the max element for the array
int[][] adj= new int[max][max]; //declare the array
int n; //nodes
int l; // to determine the max number of edges
int loopChecker = 0;//To check whether there's a loop or not
int isolatedCounter = 0;
int completeCounter = 0;
int subCounter = 0;
int origin; //to determine where the edge starts
int destination; //to detemine where the edge stops
int i,j; //for the loop
Scanner input = new Scanner(System.in); //to scan inputs
System.out.println("Enter the number of vertices: ");
n=input.nextInt();
while(n<1 || n>200){ //nodes/vertices should be between 1 and 200
System.out.println("\nWrong input! Insert a numbe between 1 and 200\n");
n=input.nextInt();
}
System.out.println("Enter the number of edges: ");
l=input.nextInt();
System.out.println("Enter the begining of the edge then press enter then enter the end side of the edge to input it correctly.\n");
for(i=0;i<=(l-1);i++){ //a loop that populates the adjacency matrix
System.out.println("Enter edge "+i+": >> (0 0) to quit: ");
origin = input.nextInt();
destination = input.nextInt();
adj[origin][destination]=1;
}
System.out.println("\n\n\nThis is how your graph looks like:\n");
for(i=0;i<=(n-1);i++){
for(j=0;j<=(n-1);j++)
System.out.printf("%4d",adj[i][j]);
System.out.printf("\n");
}
//////////////////////////////////////////////////////////////
//Loop Checker starts from here
for(i=0;i<=(n-1);i++){
if(adj[i][i]==1){//If there's a 1 in the same place
loopChecker=1;
break;
}
}
if(loopChecker==1){
System.out.println("Self Loop");
}
else{
System.out.println("No Self Loop");
}
//Loop Checker ends here
///////////////////////////////////////////////////////////////
//checks whether the graph is complete
for(i=0;i<=(n-1);i++){
subCounter=0;//reset
for(j=0;j<=(n-1);j++){
if(adj[i][j]==1){
subCounter+=1;//buffer variable
}
if(subCounter==(n-1)){
completeCounter+=1;//this counter incriments if the whole line has 1s that equal n-1
}
}
}
if(completeCounter == n){
System.out.println("Complete");
}
else
System.out.println("Not Complete");
//end of complete or not check
/////////////////////////////////////////////////////////////////////
subCounter =0;//reset
//checks whether the graph has an isolated vertex or not
for(i=0;i<=(n-1);i++){
for(j=0;j<=(n-1);j++){
if(adj[i][j]==0){
subCounter+=1;//buffer variable
}
if(subCounter==n){
isolatedCounter=1;
break;
}
}
subCounter=0;//reset
}
if(isolatedCounter==1){
System.out.println("There's an isolated vertex");
}
else
System.out.println("There's no isolated vertex");
//end of isolated check
///////////////////////////////////////////////////////////////////
}
包一期工程;
导入java.util.Scanner;
公营一期工程{
公共静态void main(字符串[]args){
int max=2000;//声明数组的max元素
int[][]adj=newint[max][max];//声明数组
int n;//节点
int l;//确定最大边数
int loopChecker=0;//检查是否存在循环
int isolatedCounter=0;
int completeCounter=0;
整数次计数=0;
int origin;//确定边的起始位置
int destination;//确定边停止的位置
int i,j;//用于循环
扫描仪输入=新扫描仪(System.in);//扫描输入
System.out.println(“输入顶点数:”);
n=input.nextInt();
而(n200){//节点/顶点应介于1和200之间
System.out.println(“\n错误输入!插入一个介于1和200之间的数字\n”);
n=input.nextInt();
}
System.out.println(“输入边数:”);
l=输入。nextInt();
System.out.println(“输入边的开始,然后按Enter键,然后输入边的结束侧以正确输入。\n”);
对于(i=0;i)您没有“违反规则”,但您发布了一个糟糕的问题。太长了。请阅读。创建一个MCVE。很抱歉,太长了,但正如我所说,我不熟悉从文本文件读取输入,所以我包含了整个程序,以便于人们帮助我。