Java 如何将while循环中的数据存储在数组中?
我需要用while循环存储通过扫描仪从.txt文件收集的数据。txt中的行包含x、y、z坐标和c值。 我设法将.txt中的行拆分并将它们转换为数字,但我无法在循环之外访问它们,以便从那里使用它们。我想使用“x-”和“y-”坐标以及二维数组“z65”中使用的c值Java 如何将while循环中的数据存储在数组中?,java,arrays,multidimensional-array,while-loop,Java,Arrays,Multidimensional Array,While Loop,我需要用while循环存储通过扫描仪从.txt文件收集的数据。txt中的行包含x、y、z坐标和c值。 我设法将.txt中的行拆分并将它们转换为数字,但我无法在循环之外访问它们,以便从那里使用它们。我想使用“x-”和“y-”坐标以及二维数组“z65”中使用的c值 导入java.io.File; 导入java.util.array; 导入java.util.Scanner; 导入java.util.regex.Pattern; 导入gdi.ct.CtImage; 公共类CtViewer{ 公共静态v
导入java.io.File;
导入java.util.array;
导入java.util.Scanner;
导入java.util.regex.Pattern;
导入gdi.ct.CtImage;
公共类CtViewer{
公共静态void main(字符串[]args)
抛出java.io.FileNotFoundException
{
int x;
int-y;
intz;
INTC;
int-xValue;
int值;
int-zValue;
int值;
int[][]ct=新int[204][204][139];
对于(x=0;x<204;x++){
对于(y=0;y<204;y++){
对于(z=0;z<139;z++){
ct[x][y][z]=-1000;}}
System.out.println(ct[0][0][0]);
System.out.println(ct[0][0][1]);
文件cTxt=新文件(“CT Chest.txt”);
扫描仪readTxt=新扫描仪(cTxt);
而(readTxt.hasNextLine()){
字符串i=readTxt.nextLine();
字符串[]out=i.split(“”);
xValue=Integer.parseInt(out[0]);
yValue=Integer.parseInt(out[1]);
zValue=Integer.parseInt(out[2]);
cValue=Integer.parseInt(out[3]);
x=x值;
y=y值;
z=zValue;
c=c值;
System.out.println(x+“”+y+“”+z+“”+c);//工作到这里。
ct[x][y][z]=c;
}
System.out.println(x+“”+y);
int[]z65=新的int[x][y];
CtImage cI=新CtImage(z65);
}
}
您的循环变量写得太多了。就这样吧
x =xValue; //delete all this
y=yValue;
z=zValue;
c = cValue;/// to here
System.out.println(x+ "" +y+"" +z+ ""+c); //Works 'till here.
ct [xValue] [yValue][zValue] = cValue;
我就是这样做到的:
int[][][] ct = new int[204][204][139];
for (int x = 0; x < 204; x++) {
for (int y = 0; y < 204; y++) {
for (int z = 0; z < 139; z++) {
ct[x][y][z] = -1000;
}
}
}
File file = new File(ClassLoader.getSystemResource("CT-Chest.txt").getFile());
List<String> lines = Files.readAllLines(file.toPath());
lines.stream().filter(s->!s.trim().isEmpty()).forEach(s->{
String[] split = s.split(" ");
ct[Integer.parseInt(split[0])][Integer.parseInt(split[1])][Integer.parseInt(split[2])]=Integer.parseInt(split[3]);
});
System.out.println(ct[5][8][99]);
int[]ct=newint[204][204][139];
对于(int x=0;x<204;x++){
对于(int y=0;y<204;y++){
对于(intz=0;z<139;z++){
ct[x][y][z]=-1000;
}
}
}
File File=新文件(ClassLoader.getSystemResource(“CT Chest.txt”).getFile());
列表行=Files.readAllLines(file.toPath());
lines.stream().filter(s->!s.trim().isEmpty()).forEach(s->{
字符串[]split=s.split(“”);
ct[Integer.parseInt(split[0])][Integer.parseInt(split[1])][Integer.parseInt(split[2])]=Integer.parseInt(split[3]);
});
System.out.println(ct[5][8][99]),;
以及我创建的文件的内容:
5899262031382000
203 188135 程序的输出为2
您是否可能正在尝试使用另一种方法<代码>ct和从该文件读取的所有值将从此点开始可用。我从文件中得到的行有点不同,但这不重要 如果将来有人需要解决这个问题,下面是我的答案
import java.io.File;
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Pattern;
import gdi.ct.CtImage;
public class CtViewer {
public static void main(String[] args) throws java.io.FileNotFoundException {
int x;
int y;
int z;
int c;
int[][][] ct = new int[204][204][139];
for (x = 0; x < 204; x++) {
for (y = 0; y < 204; y++) {
for (z = 0; z < 139; z++) {
ct[x][y][z] = -1000;
}
}
}
File cTxt = new File("CT-Chest.txt");
Scanner readTxt = new Scanner(cTxt);
int[][] z65 = new int[204][204];
do {
String i = readTxt.nextLine();
String[] out = i.split(" ");
int xValue = Integer.parseInt(out[0]);
int yValue = Integer.parseInt(out[1]);
int zValue = Integer.parseInt(out[2]);
ct[xValue][yValue][zValue] = Integer.parseInt(out[3]);
if (zValue == 65) {
xValue = Integer.parseInt(out[0]);
yValue = Integer.parseInt(out[1]);
zValue = Integer.parseInt(out[2]);
z65[xValue][yValue] = Integer.parseInt(out[3]);
}
}
while (readTxt.hasNextLine());
CtImage cI = new CtImage(z65);
readTxt.close();
}
}
导入java.io.File;
导入java.util.array;
导入java.util.Scanner;
导入java.util.regex.Pattern;
导入gdi.ct.CtImage;
公共类CtViewer{
公共静态void main(字符串[]args)引发java.io.FileNotFoundException{
int x;
int-y;
intz;
INTC;
int[][]ct=新int[204][204][139];
对于(x=0;x<204;x++){
对于(y=0;y<204;y++){
对于(z=0;z<139;z++){
ct[x][y][z]=-1000;
}
}
}
文件cTxt=新文件(“CT Chest.txt”);
扫描仪readTxt=新扫描仪(cTxt);
int[]z65=新int[204][204];
做{
字符串i=readTxt.nextLine();
字符串[]out=i.split(“”);
int xValue=Integer.parseInt(out[0]);
int-yValue=Integer.parseInt(out[1]);
intzvalue=Integer.parseInt(out[2]);
ct[xValue][yValue][zValue]=Integer.parseInt(out[3]);
如果(zValue==65){
xValue=Integer.parseInt(out[0]);
yValue=Integer.parseInt(out[1]);
zValue=Integer.parseInt(out[2]);
z65[xValue][yValue]=Integer.parseInt(out[3]);
}
}
while(readTxt.hasNextLine());
CtImage cI=新CtImage(z65);
readTxt.close();
}
}
哪些值具有x
、y
和z
?它们应该在0之间正确。值如您所述。实际上它在循环之外,所以那些shoundexistit仍然不允许我从循环之外访问值。它告诉我,我底部z65数组的变量X和y没有初始化1我刚才注意到的问题<代码>int[]z65=新int[x][y];CtImage cI=新CtImage(z65)
Doessent使用ct阵列,我们只是在其中放置所有数据为什么?您提供的代码甚至没有使用数组。是否要创建一个维度X、Y的图像以及Z=1的C值?你什么都不做。您确实尝试创建一个大小为X,Y的int数组,但没有在其中放入任何数据。我稍微更改了代码,希望它能够正常工作:
import java.io.File;
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Pattern;
import gdi.ct.CtImage;
public class CtViewer {
public static void main(String[] args) throws java.io.FileNotFoundException {
int x;
int y;
int z;
int c;
int[][][] ct = new int[204][204][139];
for (x = 0; x < 204; x++) {
for (y = 0; y < 204; y++) {
for (z = 0; z < 139; z++) {
ct[x][y][z] = -1000;
}
}
}
File cTxt = new File("CT-Chest.txt");
Scanner readTxt = new Scanner(cTxt);
int[][] z65 = new int[204][204];
do {
String i = readTxt.nextLine();
String[] out = i.split(" ");
int xValue = Integer.parseInt(out[0]);
int yValue = Integer.parseInt(out[1]);
int zValue = Integer.parseInt(out[2]);
ct[xValue][yValue][zValue] = Integer.parseInt(out[3]);
if (zValue == 65) {
xValue = Integer.parseInt(out[0]);
yValue = Integer.parseInt(out[1]);
zValue = Integer.parseInt(out[2]);
z65[xValue][yValue] = Integer.parseInt(out[3]);
}
}
while (readTxt.hasNextLine());
CtImage cI = new CtImage(z65);
readTxt.close();
}
}