0

In this code, I create a scanner to read a file full of ASCII values, which are ints. I put them in a 2d array of chars using nested loops after I typecast them to type 'char', and then proceed to print the resulting arrays in a separate method. the result is supposed to be an image of Homer Simpson from the Simpsons cartoon made out of characters, but an InputMismatchException prevents the image from coming to life, and I really struggle to see where I am trying to accept an integer that doesn't match what type it is. I assume it is in the nested loops, but after tweaking all that I can, I still cannot find the underlying problem. I would appreciate any help.

import java.util.*;

public class HomerSimpson
{     
   public static void main(String[] args)
   {
      display();  
   }

   public static void display()
   {
      Scanner fileReader = new Scanner("homer.txt");
      char[][] homer = new char[fileReader.nextInt()][fileReader.nextInt()];

      for (int i = 0; i < homer.length; i++)
      {
         for (int j = 0; j < homer[0].length; j++)
         {
            homer[i][j] = (char)fileReader.nextInt();
         }
      }
      print2DArray(homer);
   }

   public static void print2DArray(char[][] homer)
   {
      for (int h = 0; h < homer.length; h++)
      {
         System.out.println(Arrays.toString(homer[h]));
      }
   }  
}     
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
redbull_nowings
  • 111
  • 1
  • 6
  • Your Scanner's input is not what you expect it to be. You will need to do some debugging to find out what it really is. – Hovercraft Full Of Eels Jan 11 '17 at 03:14
  • @HovercraftFullOfEels How so? If my file is simply a set of integers separated by whitespace, and I am reading the next token with a call of nextInt, and type casting it as a char before putting it in the array of type char, arent they the same type? I very much am a novice to coding, so I would appreciate if you advanced coders would help educate me in working towards solving my problems. Thank you. – redbull_nowings Jan 11 '17 at 03:27

0 Answers0