Java 从文本文件中读取内容并将其存储到数组中
我有一个文本文档,其中包含一个带有这些值的5x5表Java 从文本文件中读取内容并将其存储到数组中,java,arrays,loops,text,Java,Arrays,Loops,Text,我有一个文本文档,其中包含一个带有这些值的5x5表 5 5 39 95 99 56 41 88 8 1 48 75 3 58 13 54 80 92 72 74 25 86 30 38 3 21 2 我必须将它们添加到一个数组中,并显示最低值(即1)并告诉最低值的位置(第1行第2列) publicstaticvoidmain(字符串[]args) { 扫描仪输入; File fileIn=新文件(“src/array2d/array2dtes
5 5
39 95 99 56 41
88 8 1 48 75
3 58 13 54 80
92 72 74 25 86
30 38 3 21 2
我必须将它们添加到一个数组中,并显示最低值(即1)并告诉最低值的位置(第1行第2列)
publicstaticvoidmain(字符串[]args)
{
扫描仪输入;
File fileIn=新文件(“src/array2d/array2dtest1.txt”);
System.out.println(fileIn.getAbsolutePath());
int[][]数组=新的int[5][5];
对于(int row=0;row,此代码实际上将您的输入存储到一个数组中
public static void main(String[] args) {
Scanner input;
File fileIn = new File("src/array2d/array2dtest1.txt");
System.out.println(fileIn.getAbsolutePath());
int[][] array = new int[5][5];
try
{
input = new Scanner(fileIn);
String values = input.nextLine();
String[] value = values.split("\\s+");
int index = 0;
for(int row = 0;row < 5;row++)
{ index = row;
for(int col = 0 ; col < 5; col++){
array[row][col] = Integer.parseInt(value[index*5 + col]);
}
}
}
catch (FileNotFoundException e)
{
System.out.println(fileIn.getName() + " is not found.");
return;
}
input.close();
}
publicstaticvoidmain(字符串[]args){
扫描仪输入;
File fileIn=新文件(“src/array2d/array2dtest1.txt”);
System.out.println(fileIn.getAbsolutePath());
int[][]数组=新的int[5][5];
尝试
{
输入=新扫描仪(文件输入);
字符串值=input.nextLine();
字符串[]值=值。拆分(\\s+);
int指数=0;
对于(int行=0;行<5;行++)
{索引=行;
for(int col=0;col<5;col++){
数组[row][col]=Integer.parseInt(值[index*5+col]);
}
}
}
catch(filenotfounde异常)
{
System.out.println(未找到fileIn.getName()+);
回来
}
input.close();
}
使用@vikasn91的答案,我对其进行了一些编辑,以正确地将值分配给数组,找到最小的数字及其在数组中的位置:
try {
input = new Scanner(fileIn);
int lowestCol = 0;
int lowestRow = 0;
int lowest = 0;
for (int row = 0; row < 5; row++) {
String values = input.nextLine();
String[] value = values.split("\\s+");
for (int col = 0; col < 5; col++) {
array[row][col] = Integer.parseInt(value[col]);
if (row == 0 && col == 0) {
lowest = array[row][col];
} else if (array[row][col] < lowest) {
lowestCol = col;
lowestRow = row;
lowest = array[lowestRow][lowestCol];
}
}
}
System.out.println("Lowest number: " + lowest);
System.out.println("Found in row: " + lowestRow + ", col: " + lowestCol);
} catch (FileNotFoundException e) {
System.out.println(fileIn.getName() + " is not found.");
return;
}
input.close();
试试看{
输入=新扫描仪(文件输入);
int lowestCol=0;
int-lowstrow=0;
int最低=0;
对于(int行=0;行<5;行++){
字符串值=input.nextLine();
字符串[]值=值。拆分(\\s+);
for(int col=0;col<5;col++){
数组[row][col]=Integer.parseInt(值[col]);
if(行==0&&col==0){
最低=数组[行][列];
}else if(数组[行][列]<最低){
低柱=柱;
Lowstrow=行;
最低=数组[lowstrow][lowstcol];
}
}
}
System.out.println(“最低编号:+最低编号”);
System.out.println(“在行:+lowestRow+”,列:+lowestCol中找到);
}catch(filenotfounde异常){
System.out.println(未找到fileIn.getName()+);
回来
}
input.close();
公共静态void main(字符串[]args)
{
扫描仪输入;
File fileIn=新文件(“array2dtest1.txt”);
System.out.println(fileIn.getAbsolutePath());
尝试
{
输入=新扫描仪(文件输入);
int row=input.nextInt();
int column=input.nextInt();
int min=整数最大值;
int-val;
int minR=0,minC=0;
对于(int i=0;i,如果使用扫描仪,则不需要直接拆分或解析整数。默认分隔符为空格
Scanner s = new Scanner(new FileReader("src/array2d/array2dtest1.txt"));
int numRows = s.nextInt();
int numCols = s.nextInt();
int[][] array = new int[numRows][numCols];
int least = Integer.MAX_VALUE;
int leastRow = -1;
int leastCol = -1;
for(int r = 0; r < numRows; r++) {
for(int c = 0; c < numCols; c++) {
if((array[r][c] = s.nextInt()) < least) {
leastRow = r;
leastCol = c;
}
}
}
Scanner s=新的扫描仪(新的文件阅读器(“src/array2d/array2dtest1.txt”);
int numRows=s.nextInt();
int numCols=s.nextInt();
int[][]数组=新的int[numRows][numCols];
int最小值=整数最大值;
int leastRow=-1;
int-leastCol=-1;
对于(int r=0;r
public static void main(String[] args)
{
Scanner input;
File fileIn = new File("array2dtest1.txt");
System.out.println(fileIn.getAbsolutePath());
try
{
input = new Scanner(fileIn);
int row = input.nextInt();
int column = input.nextInt();
int min = Integer.MAX_VALUE;
int val;
int minR=0,minC=0;
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
val = input.nextInt();
if(val<min){
min = val;
minR = i;
minC = j;
}
}
}
System.out.println("Min Value is " + min + "\nat position (" + minR + "," + minC + ")" );
}
catch (FileNotFoundException e)
{
System.out.println(fileIn.getName() + " is not found.");
return;
}
input.close();
}
Scanner s = new Scanner(new FileReader("src/array2d/array2dtest1.txt"));
int numRows = s.nextInt();
int numCols = s.nextInt();
int[][] array = new int[numRows][numCols];
int least = Integer.MAX_VALUE;
int leastRow = -1;
int leastCol = -1;
for(int r = 0; r < numRows; r++) {
for(int c = 0; c < numCols; c++) {
if((array[r][c] = s.nextInt()) < least) {
leastRow = r;
leastCol = c;
}
}
}