[英]Txt file to 2D array in java
我正在努力滿足這個社區的要求。 我已經為這個問題動了十個小時左右。 我很少尋求幫助,因此請諒解我是否不完全合規。
我被分配去做一個Java代碼,它將讀取一個ASCII圖像的文本文件。 文本文件的第一行是圖像的尺寸,忽略第一行。 我在填充字符數組時有些惡魔。 當我使用偽文本時,程序會輸出正確大小的數組,但無法獲取它來存儲文件中的實際字符。 我絕對可以確定我遇到了一些1D1OT錯誤,其中可能有很多垃圾代碼。 我試圖清理它,但是我主要關注寫文件的失敗。
另外:是的,這絕對是一項家庭作業。 全面披露。 否則,我不會為該任務使用數組。 但是,分配任務的時間不得超過一周。 我真的不想成為想要您為他們工作的人之一。
import java.util.*;
import java.io.*;
public class Shell{
private static String fileName = null;
private static int imageHeight =0;
private static int imageWidth=0;
private static char[][] buffer = null;
private static Scanner input;
private static Scanner in;
private static Scanner in2;
private static Scanner inFile;
/*
FUNCTION NAME: Main ;
INPUT: none.
OUTPUT: a message to the user of this program, all of the
prompts and a final display according to user specifications.
PRECONDITIONS: None.
POSTCONDITIONS: Variables and calls made according to users input
output set to start on a new line.
CALLERS: None.
CALLEES: askPermission, getParameters(), getImage(), and doTileJob().
*/
public static void main(String args[]) throws FileNotFoundException
{
//in = new Scanner(System.in);
//System.out.println("What file name would you like to print?");
//String fileName = in.nextLine();
//input = new Scanner(new File(fileName));
boolean a = askPermission();
if (a == true) {
while (a == true) {
System.out.println("What is the name of the file you want printed?");
in = new Scanner(System.in);
fileName = in.nextLine();
new Scanner(new File(fileName));
System.out.println(fileName);
getParameters();
buffer = new char[imageHeight][imageWidth];
getImage();
printImage();
a = askPermission();}
}
//else if (a == false) {
System.out.println("Goodbye!");
//}
}
/*
FUNCTION NAME: askPermission ;
INPUT: none.
OUTPUT: a message to the user of this program.
PRECONDITIONS: output set to start on a new line.
POSTCONDITIONS: variable response has user's answer stored in it.
CALLERS: the main program
CALLES: None.
*/
public static boolean askPermission()
{
System.out.println("Would you like to print an image in a file?");
System.out.println("If yes, type 'y'. If no, type 'n'");
in2 = new Scanner(System.in);
String Ans = in2.nextLine();
if (Ans.equals("y")){
return true;}
else {
return false;
}
}
/*
FUNCTION NAME getParameters ;
INPUT: the file name, number of tiles across and down.
OUTPUT: message "Getting Image".
PRECONDITIONS: the variable response has 'y' in it.
POSTCONDITIONS: variables set with the values entered by user.
CALLERS: the main program
CALLEES: none
*/
static void getParameters() throws FileNotFoundException
{
inFile = new Scanner(new File(fileName));
imageHeight = (inFile.nextInt());
imageWidth = (inFile.nextInt());
}
/*
FUNCTION NAME: getImage ;
INPUT:the file name and the height and width of the pattern to be made.
OUTPUT: the message "Getting Image".
PRECONDITIONS: array for image declared, the variables fileName,
imageHeight and imageWidth set with proper values.
POSTCONDITIONS: the image is stored in the array.
CALLERS: the main program
CALLEES: none
*/
public static void getImage() throws FileNotFoundException
{
String string = "";
input = new Scanner(new File(fileName));
{
for (int n = 0; n<(imageHeight);)
{
string = input.nextLine();
char[] charArray = new char[n];
string = string + input.nextLine();
charArray = string.toCharArray();
System.out.println(charArray[n]);
char q = charArray[n];
buffer[n][0] = (q);
for(int p = 1; p<(imageWidth); p++)
{
char[] charArrayW = string.toCharArray();
char a = charArrayW[p];
buffer[n][p] = (a);
}
n=n+1;
}
}
}
/*
FUNCTION NAME: printImage
INPUT:the buffer with the image and the height and width of the
pattern to be made
OUTPUT: the patterns structured according to users input.
PRECONDITIONS: All of the variables are set and pattern is stored in 'buffer'.
POSTCONDITIONS: Output displayed according to users input.
CALLERS: the main program
CALLEES: none
*/
// This function uses for loops to display the images. The inner most for loop prints one line of the picture.
public static void printImage()
{
for ( int i=0; i<imageHeight; i++) {
System.out.print (buffer[i][0]);
//System.out.println();
for(int j=0; j<imageWidth; j++)
System.out.print (buffer[i][j]);
System.out.println();}
}
}
現在看來,我是如此接近。 我得到的問題是,在getImage()中,我在第126行接收到“找不到行”異常。它只打印很少量的文件,在第一個垂直行僅打印幾個字符。
您的getImage
方法中有幾個問題。 首先,您應該從包含寬度/高度的文件中跳過第一行。 其次,您正在讀取該行(每個循環迭代調用兩次input.nextLine()
),因此實際上需要2*imageHeight
行。 難怪您無法正確閱讀它們。 接下來,出於某種原因將string
與上一個字符串( string = string + input.nextList()
)連接起來。 完全沒有必要。 接下來,我看到從char q = charArray[n]
的行中提取第n-th
字符並打印出來毫無意義。 n
變量是行號,而charArray
索引對應於列! 同樣,無需在每個嵌套循環迭代中調用toCharArray()
。 最后,您需要使用try-with-resources
語句關閉文件。 這是固定代碼:
public static void getImage() throws FileNotFoundException {
String string = "";
try(Scanner input = new Scanner(new File(fileName)))
{
input.nextLine(); // skip width/height
for (int n = 0; n < (imageHeight);) {
string = input.nextLine();
char[] charArray = string.toCharArray();
for (int p = 0; p < (charArray.length); p++) {
char a = charArray[p];
buffer[n][p] = (a);
}
n = n + 1;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.