繁体   English   中英

txt文件到java中的二维数组

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM