Results 1 to 6 of 6
  1. #1
    carden2 is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default using random strings from arrays insult generator

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    import java.util.Random;
    
    public class InsultGenerator
    
    {
    
    //randomly picks an adjective and a noun from a list of 10 random nouns and adjectives
    //it then creates a random insult using one adjective and one noun
    
    	public static void main (String [] args)throws IOException
    
    	{
    		String[]adjectives = new String [10];
    		String[]nouns = new String [10];
    		int size = readFileIntoArray (adjectives);
    		int size2 = readFileIntoArray2 (nouns);
    		String adjective = getAdjective(adjectives, size);
    		String noun = getNoun(nouns, size2);
    		printResults(adjectives, nouns, adjective, noun );
    	}
    
    	public static int readFileIntoArray (String[] adjectives)throws IOException
    
    	{
    		Scanner fileScan= new Scanner("adjectives.txt");
    		int count = 0;	
    		while (fileScan.hasNext()) 
    		{
    			adjectives[count]=fileScan.nextLine();
    			count++;
    		}
    		return count;
    		}
    
    	public static int readFileIntoArray2(String[] nouns)throws IOException
    
    	{
    		Scanner fileScan= new Scanner("nouns.txt");
    		int count = 0;	
    		while (fileScan.hasNext()) 
    		{
    			nouns[count]=fileScan.nextLine();
    			count++;
    		}
    		return count;
    		}
    
    	public static String getAdjective(String [] adjectives, int size)
    
    	{
    		Random random = new Random();
    		String adjective = "";
    		int count=0;
    		while (count < size)
    		{
    			adjective = adjectives[random.nextInt(10)]; 
    			count ++;
    		}
    		return adjective;
    	}
    
    	public static String getNoun(String[] nouns, int size2)
    	{
    		Random random = new Random();
    		String noun = "";
    		int count=0;
    		while (count < size2)
    		{
    			noun = nouns[random.nextInt(10)]; 
    			count ++;
    		}
    
    		return noun;
    
    	}
    
    	public static void printResults(String[] adjectives, String[] nouns, String adjective, String noun)
    
    
    
    	{
    
    	System.out.println("You " + adjective + " " + noun);
            
    	}
    
    }
    there's my code. I am having a problem with my print output. I need my code to pick a random word from a text file of 10 adjectives and a random word from a text file of 10 nouns. I then need it to print out 20 insults by randomly picking 1 adjective and 1 noun. I am concentrating on getting it to print one statement first then I will try to make it print 20 different ones. It does compile in jgrasp. but right now when i run it just says you null null. any help would be appreciated.
    Last edited by carden2; 04-04-2010 at 07:26 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Just as you should break your project into small bits and solve each one in isolation, you should do the same with debugging. For instance, why not check the results of each step in your program and fix it before moving on to the next (actually, your development should be like this. I thus propose that you simplify your main method to something like this which will give you a clue as to what needs to be fixed:

    Java Code:
      public static void main(String[] args) throws IOException {
        String[] adjectives = new String[10];
        String[] nouns = new String[10];
        int size = readFileIntoArray(adjectives);
        int size2 = readFileIntoArray2(nouns);
        
        System.out.println(java.util.Arrays.toString(adjectives));
        System.out.println(java.util.Arrays.toString(nouns));
      }
    Much luck!

    edit: also, why do you have two methods that essentially do the same thing: readFileIntoArray and readFileIntoArray2. Wouldn't one method be fine here?

  3. #3
    carden2 is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    so my text files aren't getting read into the program correctly because by doing that it shows all null for all 10 i put in my lists.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by carden2 View Post
    so my text files aren't getting read into the program correctly because by doing that it shows all null for all 10 i put in my lists.
    I bet your files aren't there where you (not your program) expects them to be. Try absolute paths for starters.

    kind regards,

    Jos

  5. #5
    carden2 is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    I bet your files aren't there where you (not your program) expects them to be. Try absolute paths for starters.

    kind regards,

    Jos
    we haven't learned about absolute paths yet we were shown to put the plain text files we made into run arguments. so in my run arguments i have adjectives.txt and nouns.txt files that i created a list of 10. i was trying to get those files into arrays so i could use them to pick a random string from each list.

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    import java.util.Random;
    
    public class InsultGenerator
    
    {
    
    //randomly picks an adjective and a noun from a list of 10 random nouns and adjectives
    //it then creates a random insult using one adjective and one noun
    
    	public static void main (String [] args)throws IOException
    
    	{
    		String[]adjectives = new String [10];
    		String[]nouns = new String [10];
    		int size = readFileIntoArray (adjectives);
    		int size2 = readFileIntoArray2 (nouns);
    		String adjective = getAdjective(adjectives, size);
    		String noun = getNoun(nouns, size2);
    		printResults(adjectives, nouns, adjective, noun );
    	}
    You're only going to print out one sentence unless you put a loop around those last three lines.
    Java Code:
    	public static int readFileIntoArray (String[] adjectives)throws IOException
    
    	{
    		Scanner fileScan= new Scanner("adjectives.txt");
    		int count = 0;	
    		while (fileScan.hasNext()) 
    		{
    			adjectives[count]=fileScan.nextLine();
    			count++;
    		}
    		return count;
    		}
    
    	public static int readFileIntoArray2(String[] nouns)throws IOException
    
    	{
    		Scanner fileScan= new Scanner("nouns.txt");
    		int count = 0;	
    		while (fileScan.hasNext()) 
    		{
    			nouns[count]=fileScan.nextLine();
    			count++;
    		}
    		return count;
    		}
    Review how to use a Scanner to read a file. (Hint: you don't just give it a file name in a String).

    What you want here is something more like this:
    Java Code:
    	public static int fillArrayFromFile(String[] word, String fileName) throws IOException {
                    ...
            }
    ...so that you don't have two copies of essentially the same method to maintain. Also note that your method will fail if there are more than ten words in the file. It would be better if you could use an ArrayList<String> so that you don't have to do that awkward stuff with the size variables, but I imagine your instructor has handcuffed you on that.
    Java Code:
    	public static String getAdjective(String [] adjectives, int size)
    
    	{
    		Random random = new Random();
    		String adjective = "";
    		int count=0;
    		while (count < size)
    		{
    			adjective = adjectives[random.nextInt(10)]; 
    			count ++;
    		}
    		return adjective;
    	}
    
    	public static String getNoun(String[] nouns, int size2)
    	{
    		Random random = new Random();
    		String noun = "";
    		int count=0;
    		while (count < size2)
    		{
    			noun = nouns[random.nextInt(10)]; 
    			count ++;
    		}
    
    		return noun;
    
    	}
    Similarly, use something like this:
    Java Code:
            public static String getRandomWord(String[] words, int size) {
                    ...
            }
    ...and pass it the array you want to select from. It'd be nice if you could just use the array's length property, and you can, if you make sure that there are exactly ten words in each file. Anyway, you're not properly using the size that you've passed, except for a useless loop. You want the size of the array for the nextInt() call. There is no need to loop in this method.
    Java Code:
    	public static void printResults(String[] adjectives, String[] nouns, String adjective, String noun)
    	{
    	System.out.println("You " + adjective + " " + noun);
            
    	}
    
    }
    Why are we asking for two arrays in this method? You're not using them. You have already used the arrays in your earlier method call, and you already have the words you need at this point.

    The main issue is setting up your Scanner properly. Review your docs and your text.

    -Gary-

Similar Threads

  1. Random number generator
    By Michailangelo in forum Advanced Java
    Replies: 4
    Last Post: 04-02-2010, 06:47 PM
  2. Mersenne twister random generator
    By mental in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:21 AM
  3. Help with class project, random number generator.
    By Christopher The Great in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 02:12 AM
  4. Random Shape generator
    By scheng12 in forum New To Java
    Replies: 1
    Last Post: 03-09-2009, 02:06 AM
  5. Random Generator
    By padutch2 in forum New To Java
    Replies: 1
    Last Post: 12-03-2007, 06:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •