user3211033
user3211033

Reputation:

Why am I getting a "java.util.NoSuchElementException"?

Here the error i am getting...

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Maze.Map.readFile(Map.java:60)
at Maze.Map.<init>(Map.java:23)
at Maze.Board.<init>(Board.java:16)
at Maze.Maze.<init>(Maze.java:15)
at Maze.Maze.main(Maze.java:9)

Below is my code!

package Maze;

import javax.swing.*;

public class Maze 
{

    public static void main(String[] args)

    {
        new Maze();
    }

    public Maze()

    {

        JFrame f = new JFrame();

        f.setTitle("Maze Game");

        f.add(new Board());
        f.setSize(464, 485);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

package Maze;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Board extends JPanel implements ActionListener
{

    private Timer timer;

    private Map m;

    public Board()
    {
        m = new Map();

        timer = new Timer(25, this);
        timer.start();
    }

    public void actionPerformed(ActionEvent e)
    {
        repaint();
    }

    public void paint(Graphics g)
    {
        super.paint(g);

        for(int y = 0; y < 14; y++)
        {
            for(int x = 0; x < 14; x++)
            {
                if(m.getMap(x, y).equals("g"))
                {
                    g.drawImage(m.getFloor(), x * 32, y * 32, null);
                }
                if(m.getMap(x, y).equals("w"))
                {
                    g.drawImage(m.getWall(), x * 32, y * 32, null);
                }
            }
        }

    }

}

package Maze;

import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.ImageIcon;

public class Map 
{
    private Scanner m;
    private String Map[] = new String[14];
    private Image floor,
                  wall;    

    public Map()
    {
        ImageIcon img = new ImageIcon("C://Test//MazeGame//floor.jpg");
        floor = img.getImage();
        img = new ImageIcon("C://Test//MazeGame//wall.jpg");
        wall = img.getImage();

        openFile();
        readFile();
        closeFile();
    }

    public Image getFloor()
    {
        return floor;
    }

    public Image getWall()
    {
        return wall;
    }

    public String getMap(int x, int y)
    {
        String index = Map[y].substring(x, x + 1);
        return index;
    }

    public void openFile()
    {
        try
        {
            m = new Scanner(new File("C://Test//MazeGame//Map.txt"));
        }catch(Exception e)
            {
                System.out.print("Error Loading Map!");
            }
    }

    public void readFile()
    {
        while(m.hasNext())
        {
            for(int i = 0; i < 14; i++)
            {
                Map[i] = m.next();
            }
        }
    }



    public void closeFile()
    {
        m.close();
    }

}

Upvotes: 0

Views: 437

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

I don't know if this is your error (you haven't told us which line is line 60 of your Map class, the line that is causing your exception), but this is dangerous code:

while(m.hasNext())
{
    for(int i = 0; i < 14; i++)
    {
        Map[i] = m.next();
    }
}

You're calling hasNext() once per loop iteration, but calling next() 14 times! There should be a strict 1 to 1 correlation in that each next() should match with a preceding hasNext().


Also, there's no reason for the nested for loop since the while loop will handle all you need. You probably would get by with something like:

int i = 0;
while(m.hasNext()) {
   Map[i] = m.next();
   i++;
}

But it would be safer to use an ArrayList rather than an array.


As an aside, please learn and follow Java coding conventions. Methods and fields/variables/parameters should all start with a lower-case letter, so Map[i] is not allowed, but rather should be map[i]. Doing this will help us to better understand and follow your code and thus help you.

Upvotes: 5

Related Questions