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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:12:06+00:00 2026-06-17T04:12:06+00:00

Here’s the code: import java.io.File; import java.io.IOException; import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; import

  • 0

Here’s the code:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import static java.lang.System.*;

public class MadLib
{
private ArrayList<String> verbs = new ArrayList<String>();
private ArrayList<String> nouns = new ArrayList<String>();
private ArrayList<String> adjectives = new ArrayList<String>();

public MadLib()
{
    loadNouns();
    loadVerbs();
    loadAdjectives();
    out.println(nouns);
}

public MadLib(String fileName)
{
    //load stuff
    loadNouns();
    loadVerbs();
    loadAdjectives();


    try{
        Scanner file = new Scanner(new File(fileName));
    }
    catch(Exception e)
    {
        out.println("Houston we have a problem!");
    }
}

public void loadNouns()
{
    nouns = new ArrayList<String>();
    try{
        //nouns = new ArrayList<String>();
        String nou = "";
        Scanner chopper = new Scanner(new File ("nouns.dat"));

        //chopper.nextLine();
        while(chopper.hasNext()){
            nou = chopper.next();
            out.println(nou);
            nouns.add(nou);
            //chopper.nextLine();
        }
        //chopper.close();
        out.println(nouns.size());
    }
    catch(Exception e)
    {
        out.println("Will");
    }       
}

public void loadVerbs()
{
    verbs = new ArrayList<String>();
    try{
        Scanner chopper = new Scanner(new File("verbs.dat"));
        while(chopper.hasNext()){
            verbs.add(chopper.next());
            chopper.nextLine();
        }
        chopper.close();
    }
    catch(Exception e)
    {
        out.println("run");
    }
}

public void loadAdjectives()
{
    adjectives = new ArrayList<String>();
    try{
        Scanner chopper = new Scanner(new File("adjectives.dat"));
        while(chopper.hasNext()){
            adjectives.add(chopper.next());
            chopper.nextLine();
        }
        chopper.close();
    }
    catch(Exception e)
    {
    }
}

public String getRandomVerb()
{

    String verb = "";
    int num = 0;
    num = (int)(Math.random()*(verbs.size()-1));
    verb = verbs.get(num);
    return verb;
}

public String getRandomNoun()
{

    String noun = "";
    int num = 0;
    if(nouns == null){
        loadNouns();
    }
    double rand = (Math.random());
    num = (int)(rand * (nouns.size()-1));
    out.println(num);
    noun = nouns.get((int) num);
    out.print(noun);
    return noun;
}

public String getRandomAdjective()
{

    String adj = "";
    int num = 0;
    num = (int)(Math.random()*(adjectives.size()-1));
    adj = adjectives.get(num);
    return adj;
}       

public String toString()
{
    String output = "The " + getRandomNoun() + getRandomVerb() + " after the " + getRandomAdjective() + getRandomAdjective() + getRandomNoun() + " while the " + getRandomNoun() + getRandomVerb() + " the " + getRandomNoun();
   return output;
}
}

and here’s one of the .dat files (the other 2 are the exact same aside from the specific words they contain):

dog
pig
chicken
building
car
person
place
thing
truck
city
state
school
student
bird
turkey
lion
tiger
alligator
elephant

My issue is that I can’t get any of my arrayLists to read in their appropriate .dat files and from my POV, my code seems like it should do that

UPDATE

current output aside from the absence of “Will” (I removed that line):

run
[ ] 
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 
at java.util.ArrayList.rangeCheck(Unknown Source) 
at java.util.ArrayList.get(Unknown Source)
at MadLib.getRandomNoun(MadLib.java:130)
at MadLib.toString(MadLib.java:147) 
at java.lang.String.valueOf(Unknown Source) 
at java.io.PrintStream.println(Unknown Source)
at Lab16d.main(Lab16d.java:18) 
  • 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-17T04:12:07+00:00Added an answer on June 17, 2026 at 4:12 am
    import java.io.File;
    import java.io.IOException;
    import java.util.Random;
    import java.util.Scanner;
    import java.util.ArrayList;
    import java.util.Collections;
    
    import static java.lang.System.*;
    
    public class MadLib {
        private ArrayList<String> verbs = new ArrayList<String>();
        private ArrayList<String> nouns = new ArrayList<String>();
        private ArrayList<String> adjectives = new ArrayList<String>();
    
        public static void main(String args[]) {
            MadLib a = new MadLib();
            System.out.println(a.toString());
        }
    
        public MadLib() {
            loadAllWords();
            System.out.println(nouns);
        }
    
        public MadLib(String fileName) {
            loadAllWords();
    
            try {
                Scanner file = new Scanner(new File(fileName));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void loadAllWords() {
            loadNouns();
            loadVerbs();
            loadAdjectives();
        }
    
        public void loadNouns() {
            nouns = loadFile("nouns.dat");
        }
    
        public void loadVerbs() {
            verbs = loadFile("verbs.dat");
        }
    
        public void loadAdjectives() {
            adjectives = loadFile("adjectives.dat");
        }
    
        public ArrayList<String> loadFile(String filename) {
            ArrayList<String> words = new ArrayList<String>();
            try {
                Scanner chopper = new Scanner(new File(filename));
                while (chopper.hasNext()) {
                    words.add(chopper.next());
                    // just calling nextLine will cause an exception at the end of the file unless you have an blank line there on purpose, so this makes sure it does
                    if (chopper.hasNext()) {
                        chopper.nextLine();
                    }
                }
                chopper.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return words;
        }
    
        public String getRandomWord(ArrayList<String> words) {
            Random rand = new Random();
            int num = rand.nextInt(words.size());
            String word = nouns.get(num);
            System.out.println(word);
            return word;
        }
    
        public String getRandomVerb() {
            if (verbs == null) {
                loadNouns();
            }
            return getRandomWord(verbs);
        }
    
        public String getRandomNoun() {
            if (nouns == null) {
                loadNouns();
            }
            return getRandomWord(nouns);
        }
    
        public String getRandomAdjective() {
            if (adjectives == null) {
                loadNouns();
            }
            return getRandomWord(adjectives);
        }
    
        public String toString() {
            return "The " + getRandomNoun() + " " + getRandomVerb() + " after the " + getRandomAdjective()  + " " +  getRandomAdjective()  + " " +  getRandomNoun() + " while the " + getRandomNoun()  + " " +  getRandomVerb() + " the " + getRandomNoun();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code I have. This is my PrintToFile class import java.util.*; import
Here's the code i keep seeing-- public Task PossiblyAsyncOperation(bool condition) { //this condition means
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>
Here is the code in a function I'm trying to revise. This example works
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
Here's my code: NSDate *currDate = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter
Here is my code and I expect it prints out a number, instead, it
Here is the code I'm using http://jsbin.com/evike5/edit When the jQuery UI dialog is fired

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.