我正在努力满足这个社区的要求。我已经对这个问题大打了十个小时左右。我很少寻求帮助,所以请原谅我,如果我不完全顺从。
我被分配制作一个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()中,我收到了&#34;没有找到行&#34;第126行的异常。它打印的文件非常少,第一条垂直线上只有几个字符。
答案 0 :(得分:0)
您的getImage
方法存在多个问题。首先,您应该从包含width / height的文件中跳过第一行。其次,您正在读取该行(每次循环迭代调用input.nextLine()
两次),因此您需要2*imageHeight
行。难怪你无法正确阅读它们。接下来,由于某种原因,将string
与先前的字符串(string = string + input.nextList()
)连接起来。这完全没必要。接下来,我认为从n-th
中的行中提取char q = charArray[n]
字符并打印它是毫无意义的。 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;
}
}
}