Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从文本文件获取的字符数组:索引超出范围_Java_Multidimensional Array_Indexing_Char - Fatal编程技术网

Java 从文本文件获取的字符数组:索引超出范围

Java 从文本文件获取的字符数组:索引超出范围,java,multidimensional-array,indexing,char,Java,Multidimensional Array,Indexing,Char,我正在编写一个程序,输入一个文本文件,其中包含一个50×50的块,由三个不同的字符组成:#、*和/,并根据每个符号给输出PNG文件的每个像素上色,这样每个字符都有一种颜色 但是,我看到第43行的错误是“字符串索引超出范围:2500”。 我知道这个正在读取的文本文件有50行,每行50个字符,这意味着总共有2500个项目,那么问题是什么呢?非常感谢您的帮助 这是我的密码: import java.util.Scanner; import javax.imageio.ImageIO; import j

我正在编写一个程序,输入一个文本文件,其中包含一个50×50的块,由三个不同的字符组成:#、*和/,并根据每个符号给输出PNG文件的每个像素上色,这样每个字符都有一种颜色

但是,我看到第43行的错误是“字符串索引超出范围:2500”。

我知道这个正在读取的文本文件有50行,每行50个字符,这意味着总共有2500个项目,那么问题是什么呢?非常感谢您的帮助

这是我的密码:

import java.util.Scanner;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter; 

public class Lab_Week8_ImgFrmTxtFINAL {

    public static void main(String[] args) throws Exception  {  

    int image_width = 50;
    int image_height = 50;
    String output_file_path = ("image.png");
    String input_file_path = "superimage.txt";
    int r = 0;
    int g = 0;
    int b = 0;
    int a = 255;
    Scanner reader = new Scanner (new FileInputStream(input_file_path));
    int i = 0;
    int x = 0;
    int y = 0; 


    if(image_width <= 0 || image_height <= 0) {
        System.err.println("Width and Height have to be strictly positive!");
    }

    if(x < 0 || y < 0) {
        System.err.println("Coordinates (x, y) cannot be negative numbers!");
    }

    if(r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
        System.err.println("Colour values (r, g, b) have to be between 0 and 255!");
        }   

    BufferedImage image = new BufferedImage (image_width, image_height, BufferedImage.TYPE_INT_ARGB);

    while (reader.hasNextLine()){
        char c = reader.next().charAt(i);
        for (i = 0; i < image_width * image_height; i++)
        {

        int p = (a << 24) | (r << 16) | (g << 8) | b;

        for (x = 0; x < image_width; x++)
        {
            for (y = 0; y < image_height; y++){

                if (c == ('#'))
                {p = (a << 24) | (r << 16) | (g << 8) | b;
                image.setRGB(x, y, p);
                }

                else if (c == ('/'))
                {p = (a << 0) | (r << 16) | (g << 8) | b;
                    image.setRGB(x , y , p);
                }

                else if (c == ('*'))
                {p = (a << 8) | (r << 16) | (g << 24) | b;
                    image.setRGB(x , y , p);
                }
            }   
        x += 1;
    }
    y += 1;
    x = 0;
    }

    }
    File f = new File (output_file_path);
    ImageIO.write(image, "png", f);

    reader.close();
}
}
import java.util.Scanner;
导入javax.imageio.imageio;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.io.PrintWriter;
公开课实验室8周IMGFRMTXT期末考试{
公共静态void main(字符串[]args)引发异常{
int图像_宽度=50;
int图像_高度=50;
字符串输出文件路径=(“image.png”);
字符串输入文件\u path=“superimage.txt”;
int r=0;
int g=0;
int b=0;
INTA=255;
扫描仪阅读器=新扫描仪(新文件输入流(输入文件路径));
int i=0;
int x=0;
int y=0;
如果(图像宽度255 | | b<0 | | b>255){
System.err.println(“颜色值(r、g、b)必须介于0和255之间!”);
}   
BuffereImage=新的BuffereImage(图像宽度、图像高度、BuffereImage.TYPE\u INT\u ARGB);
while(reader.hasNextLine()){
char c=reader.next().charAt(i);
对于(i=0;i<图像宽度*图像高度;i++)
{
int p=(aReplace

while (reader.hasNextLine())


将char c设置为索引i的那一行位于增加i的for循环之前。您的意思是这样做的吗?当for循环结束时,i为2500,这大于2499@Tyler谢谢Tyler,我又忘记了0索引!我现在将它改为image_width*image_height-1,并将c的设置移到fi范围内rst for loop,然而,我现在在第45行得到“NoTouchElement异常”我现在在第45行42到45行得到“NoTouchElement异常”:while((Iwhile (i < image_width * image_height && reader.hasNextLine())
while (reader.hasNextLine()) {          
    for (i = 0; i < image_width * image_height ; i++) {             
        int p = (a << 24) | (r << 16) | (g << 8) | b;

        for (x = 0; x < image_width && reader.hasNext(); x++) {
            char c = reader.next().charAt(i);