ユーザーの入力内容に基づいてテキスト ASCII ファイルを出力する必要がある宿題があります。教授がそれをテストできるようにする必要があるため、直接パスを使用していないため、srcフォルダーにテキストファイルがあるため、ファイルの名前だけを使用できます。ただし、テストしようとすると、ファイルが見つからないというエラーが発生します。ファイル名を正しく入力していることはわかっていますが、これを引き起こしているコードに欠けているものはありますか? プログラムをテストして、想定どおりに動作することを確認しようとしていますが、ファイルを出力できないため、行き詰っています。コードは次のとおりです。
import java.io.*;
import java.util.*;
class TileMap
{
/*
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.
CALLES: askPermission, getParameters(), getImage(), and doTileJob().
*/
// Start of Main Function
public static void main(String args[])
{
int MAXSIDE = 100;
Scanner scan = new Scanner(System.in);
char [][] buffer = new char [MAXSIDE][MAXSIDE];
String fileName = "";
int tilesAcross = 0;
int tilesDown = 0;
int imageHeight = 0;
int imageWidth = 0;
char userInput = 0;
System.out.println("Would you like to tile an image in a file?");
TileMap.askPermission(userInput);
TileMap.getParameters(fileName, tilesAcross, tilesDown);
TileMap.getImage(buffer, fileName, imageHeight, imageWidth);
TileMap.doTileJob(buffer, fileName, tilesDown, tilesAcross, imageHeight, imageWidth);
}
/*
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.
*/
static boolean askPermission(char response)
{
char y = 0;
Scanner scan = new Scanner(System.in);
System.out.println("If yes, type a 'y', else type 'n':");
response = scan.next().charAt(0);
boolean yes = (response == y);
if(yes = true)
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(String fileName, int tilesDown, int tilesAcross)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the file name: ");
fileName = scan.nextLine();
File file = new File(fileName);
System.out.println("Please enter the number of tiles across you want: ");
tilesAcross = scan.nextInt();
System.out.println("Please enter the number of tiles down you want: ");
tilesDown = scan.nextInt();
System.out.println("Getting Image...");
}
/*
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
*/
static void getImage(char [][] buffer,
String fileName, int imageHeight, int imageWidth)
{
File file = new File(fileName);
try
{
Scanner fstream = new Scanner(file);
imageHeight = fstream.nextInt();
imageWidth = fstream.nextInt();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}
/*
FUNCTION NAME: doTileJob;
INPUT:the buffer with the image and the height and width of the
pattern to be made, and the user's input for tilesAcross, and tilesDown.
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.
static void doTileJob (char [] [] buffer , String fileName, int tilesDown, int tilesAcross, int imageHeight, int imageWidth)
{
buffer = new char[tilesDown][tilesAcross];
for(int i=0; i < imageHeight; i++)
{
for(int w = 0; w < imageWidth; w++)
{
System.out.println(fileName);
for(int t = 0; t < tilesAcross; t++)
{
for(int a = 0; a < tilesDown; a++)
{
System.out.println(fileName);
}
}
}
}
}
}