Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Error in code

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Error in code

    The point of this program is to read characters from a txt file and store them into a 2D array. After this has been accomplished, the information is to be printed in the same manner it is read from in the txt file.

    Here is the code I have so far:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;

    public class Main
    {

    public static void main(String[] args) throws FileNotFoundException
    {
    File file = new File("sampleMaze.txt");
    Scanner s = new Scanner(file);
    Maze maze = new Maze(s);
    System.out.print(file);
    }

    }

    import java.io.File;
    import java.util.Scanner;
    import java.util.Arrays;


    public class Maze
    {
    public int width;
    public int height;
    public Square[][] sampleMaze;


    Maze(Scanner file)
    {
    this.width = Integer.parseInt(file.next());
    this.height = Integer.parseInt(file.next());
    this.sampleMaze = new Square[height][width];

    for(int i = 0; i < height; i++)
    {
    String s = file.next();

    for(int j = 0; j < width; j++)
    {
    sampleMaze[height][width] = Square.fromChar(s.charAt(j));
    }


    }
    System.out.print(sampleMaze[height][width]);
    }


    }



    public enum Square
    {
    WALLS("#"),
    OPEN_SPACES("."),
    START("o"),
    FINISH("*");
    String x;
    Square(String x)
    {
    this.x = x;
    }
    public String toString()
    {
    return x;
    }
    public static Square fromChar(char x)
    {
    if (x == '#')
    return WALLS;
    else if (x == '.')
    return OPEN_SPACES;
    else if (x == 'o')
    return START;
    else if (x == '*')
    return FINISH;
    else
    throw new IllegalArgumentException();
    }
    }

    And this is the error I am receiving when trying to accomplish the goal of the project:

    Exception in thread "main" java.lang.NumberFormatException: For input string: "############"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at Maze.<init>(Maze.java:15)
    at Main.main(Main.java:20)


    Anyone know what's going on here and how I can correct this??

    The information I am trying to store in a 2D array and then print is this:

    ############
    #.#........#
    #.#.######.#
    #.#....#...#
    #.###.*#.#.#
    #...####.#.#
    #.#.#..#.#.#
    #.#.#.##.#.#
    #o#......#.#


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Error in code

    Exception in thread "main" java.lang.NumberFormatException: For input string: "############"
    At line 15 the code calls the parseInt() method with a String that does not contain numeric digits: "############"

    Make sure the data in the file being read has the expected contents.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: May 8th, 2014, 06:25 AM
  2. error in this code
    By vibinjohn05@gmail.com in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 16th, 2013, 09:13 AM
  3. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  4. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  5. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM

Tags for this Thread