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 6779681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:24:53+00:00 2026-05-26T16:24:53+00:00

So here is my code: public static void getArmor(String treasure) throws FileNotFoundException{ Random rand=new

  • 0

So here is my code:

public static void getArmor(String treasure)
    throws FileNotFoundException{
    Random rand=new Random();
    Scanner file=new Scanner(new File ("armor.txt"));
    while(!file.next().equals(treasure)){
        file.next(); //stack trace error here
        }
    int min=file.nextInt();
    int max=file.nextInt();
    int defense=min + (int)(Math.random() * ((max - min) + 1));
    treasure=treasure.replace("_", " ");
    System.out.println(treasure);
    System.out.println("Defense: "+defense);
    System.out.println("=====");
    System.out.println();
    }

public static void getTreasureClass(Monster monGet)
throws FileNotFoundException{
    Random rand = new Random();
    String tc=monGet.getTreasureClass();
    while (tc.startsWith("tc:")){
        Scanner scan=new Scanner(new File ("TreasureClassEx.txt"));
        String eachLine=scan.nextLine();
        while(!tc.equals(scan.next())){
        eachLine=scan.nextLine();
        }
        for (int i=0;i<=rand.nextInt(3);i++){
            tc=scan.next();
        }
    getArmor(tc); //stack trace error here
    }
 }

For some reason I get a No Such Element Exception

    at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at LootGenerator.getArmor(LootGenerator.java:43)
at LootGenerator.getTreasureClass(LootGenerator.java:68)
at LootGenerator.getMonster(LootGenerator.java:127)
at LootGenerator.theGame(LootGenerator.java:19)
at LootGenerator.main(LootGenerator.java:11)

I’m not sure why though. Basically my program is searching through two text files – armor.txt and TreasureClassEx.txt. getTreasureClass receives a treasure class from a monster and searches through the txt until it reaches a base armor item (a string that does not start with tc:.) It then searches getArmor for an armor that matches the name of the base armor it got in treasure class. Any advice would be appreciated! Thanks!

The link to the txt files is here: http://www.cis.upenn.edu/~cis110/hw/hw06/large_data.zip

  • 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-05-26T16:24:53+00:00Added an answer on May 26, 2026 at 4:24 pm

    It looks like you are calling next even if the scanner no longer has a next element to provide… throwing the exception.

    while(!file.next().equals(treasure)){
            file.next();
            }
    

    Should be something like

    boolean foundTreasure = false;
    
    while(file.hasNext()){
         if(file.next().equals(treasure)){
              foundTreasure = true;
              break; // found treasure, if you need to use it, assign to variable beforehand
         }
    }
        // out here, either we never found treasure at all, or the last element we looked as was treasure... act accordingly
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the code: public static void mergeAllFilesJavolution()throws FileNotFoundException, IOException { String fileDir = C:\\TestData\\w12;
here's my code public String path; public String fileName; public static void readData() throws
Here's my JFrame code: public static void main(String[] args) { JFrame jf = new
Here is my code : public static void main(String[] args) { // System.setProperty( //
Here's the code snippet: public static void main (String[]arg) { char ca = 'a'
Here is a sample code: public class TestIO{ public static void main(String[] str){ TestIO
Here is my code: public class Main { public static void main(String[] args) {
This is driving me insane! Here's the code: public static void main(String[] strings) {
Here is my code: public class Main { public static void main(String[] args) {
Okay, so here is my code: public static void playAgain(Scanner in){ System.out.print(Play again?: );

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.