0
このwhileループは永遠にループします。私は解決策を探し、入力を消費するものを追加しようとしましたが、これは役に立たなかった。 printf "readDONEZO"は表示されません。ここで.hasNextLine()永遠にループする
は私のコード
public void read(Scanner stdin) {
int sRow = 0;
while (stdin.hasNextLine()) {
String theLine = stdin.nextLine();
String[] split = theLine.split(",");
int size = split.length; //how many values in array = 3
for(int i = 0; i < size ; i++){
String value = split[i];
int sColumn = i;
setCellValue(sRow, sColumn, value);
System.out.printf("%s", getCellValue(sRow,sColumn));
if (i+1 != size) {
System.out.printf(",");
}
}
sRow += 1;
System.out.printf("\n");
}
System.out.printf("readDONEZO\n");
}
メイン
import java.io.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int rows = 100;
int columns = 26;
StringSpreadsheet a = new StringSpreadsheet(rows, columns);
Scanner stdin = new Scanner(System.in);
a.read(stdin);
System.out.printf("out of read\n");
a.write();
}
}
クラス
import java.util.Scanner;
public class StringSpreadsheet {
private int rows;
private int columns;
private String[][] cells;
private int allRows;
private int allColumns;
public StringSpreadsheet(int rows, int columns) {
allColumns = 0;
allRows = 0;
this.rows = rows;
this.columns = columns;
cells = new String[this.rows][this.columns];
}
public void read(Scanner stdin) {
while (stdin.hasNextLine()) {
String theLine = stdin.nextLine();
String[] split = theLine.split(",");
allColumns = split.length; //how many values in array = 3
for(int i = 0; i < allColumns ; i++){
String value = split[i];
int sColumn = i;
setCellValue(allRows, sColumn, value);
System.out.printf("%s", getCellValue(allRows,sColumn));
if (i+1 != allColumns) {
System.out.printf(",");
}
}
allRows += 1;
System.out.printf("\n");
}
System.out.printf("readDONEZO\n");
}
public void write() {
for (int i = 0 ; i < allRows ; i++){
for(int j = 0 ; j < allColumns ; j++){
String value = getCellValue(i, j);
if()
System.out.printf("%s,", value);
}
}
}
public String getCellValue(int gRow, int gColumn) {
return cells[gRow][gColumn];
}
public void setCellValue(int sRow, int sColumn, String value) {
cells[sRow][sColumn] = value;
}
}
ちょうど記録のために:変数stdinを呼び出すのはちょっと混乱します。あるいは、別のソースから来たスキャナにこのメソッドを使用したくないと思いますか? – GhostCat
あなたが読んでいるソースは何ですか?それはファイルか、おそらく 'System.in'ですか? – Pshemo
私はあなたのコードを試して、それはうまく動作します(私は2つのラインは、細胞に関連するコメント)。メソッドをどのように呼び出すか見ることは可能ですか? – acornagl