Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8149237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:48:37+00:00 2026-06-06T14:48:37+00:00

I am trying to make an application that should help making Dungeon and Dragons

  • 0

I am trying to make an application that should help making Dungeon and Dragons items easier for the user!

I have 3 lists of different weapons, with different stats etc. 2 of the lists are added to the program just fine, but 1 of them throws the NoSuchElementException. I really don’t understand why, as I can’t find anything remotely different from that list, compared to the other lists I’ve got.

A line from the list looks like this:

 Light Melee Weapon,1d4,1d6,x2,10 ft.,Slashing,Axe (Throwing),8 gp, 2lb.

http://pastebin.com/Fb2db0f1 link to the entire list causing problems if you want to have a look.

http://pastebin.com/9Hg0Rw2a also link to a list that works just fine!

I hope the method is not too long for you to look through. I’ve really done all I could think of to try and fix this. And I am positive that it’s the list causing a problem, because as soon as I removing it from the destination it reads from, the program runs just fine!

It runs through the for loop just fine on the lists that work, but that first list just won’t work ._.

Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
at MainGUI.init(MainGUI.java:60)
at MainGUI.main(MainGUI.java:32)

public HashMap<String,Weapon> init() {
    String path = "base";
    File folder = new File(path);
    File[] listOfFiles = folder.listFiles();
    if(listOfFiles.length == 0) {
        JOptionPane.showMessageDialog(MainGUI.this, "The folder \"base\" was empty"+
                " so no weapons or armor was attempted loaded.", "Warning",
                JOptionPane.WARNING_MESSAGE);
        return new HashMap<>();
    }
    HashMap<String, Weapon> weapons = new HashMap<>();
    BufferedReader r = null;
    for(File f : listOfFiles) {
        if(f.getName().endsWith(".txt")) {
            String line;
            try {
                r = new BufferedReader(new FileReader(f));
                while((line = r.readLine()) != null) {
                    Weapon w;
                    StringTokenizer st = new StringTokenizer(line,",");
                    while(st.hasMoreTokens()) {

                        w = new Weapon(WeaponCategory.fromString(st.nextToken()),
                                st.nextToken(),st.nextToken(),st.nextToken(),
                                st.nextToken(),st.nextToken(),st.nextToken(),
                                st.nextToken(),st.nextToken());
                        weapons.put(w.getName(), w);
                    }
                }
            } catch(FileNotFoundException fnfe) {
                JOptionPane.showMessageDialog(MainGUI.this, "The File "+
                    f.getName() + " was not found.", "Error",
                    JOptionPane.ERROR_MESSAGE);
            } catch(IOException ioe) {
                JOptionPane.showMessageDialog(MainGUI.this, "There was a problem reading "+
                        f.getName() + ".", "Error",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    try {
        r.close();
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(MainGUI.this, "An error occured while"
                + "closing the File Reader Stream:\n"+ex.getMessage(),
                "Error", JOptionPane.ERROR_MESSAGE);
    }
    return weapons;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:48:38+00:00Added an answer on June 6, 2026 at 2:48 pm

    Code like this

                    while(st.hasMoreTokens()) {
    
                        w = new Weapon(WeaponCategory.fromString(st.nextToken()),
                                st.nextToken(),st.nextToken(),st.nextToken(),
                                st.nextToken(),st.nextToken(),st.nextToken(),
                                st.nextToken(),st.nextToken());
                        weapons.put(w.getName(), w);
                    }
    

    is unsafe as you assume that each input line has 9 items in it. You should check for the number of items and handle invalid lines gracefully (by e.g. writing out the contents of the line to make it easier to identify the problem).

    One simple way to achieve this would be to use String.split() to tokenize the line. It returns a String[] and you can easily check its size before trying to access its elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to make an application that allows the user to create
I have been trying to test my application to make sure that all the
I'm trying to make an application that needs authentication :- when user type username
I am trying to make a simple Windows Form application that will show different
I am trying to make an application that will fill in the info i
I am trying to make a application that uses the standard Split View Application
I am trying to make an application that opens a certain file type (similar
I'm trying to make an application that - in some stage - stores all
I am trying to make a simple application that turns a character to look
I'm trying to make a little console application that is able to deal with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.