Results 1 to 5 of 5
  1. #1
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Error with excuse generator

    Now that I'm out of school, I've been doing a little coding here and there to keep it in my head, so I built an excuse generator. It builds two arrays from two text files; one for nouns, one for verbs. Then it follows the format "My NOUN VERB my NOUN", and all the verbs end in "ed", so it actually makes sense. The problem I'm having is with building the excuse. I just get "My null null my null".

    Java Code:
    import java.util.*;
    import java.io.*;
    public class main {
    	public static void main(String[] args) {
    		Scanner inputNoun = new Scanner(System.in);
    		Scanner inputVerb = new Scanner(System.in);
    		
    		String excuse = null;
    		String nounFile = "nouns";
    		String verbFile = "verbs";
    		
    		String[] noun = new String[2000];
    		String[] verbs = new String[150];
    		
    		Random rand = new Random();
    		int j = rand.nextInt(2000);
    		int m = rand.nextInt(2000);
    		int n = rand.nextInt(150);
    		
    		
    		try{
    			inputNoun = new Scanner(new File(nounFile));
    			inputVerb = new Scanner(new File(verbFile));
    		}catch(Exception e){
    			System.out.println("Couldn't find the file(s)");
    		}
    	
    		for(int i = 0; i < noun.length; i++){
    			while(inputNoun.hasNextLine()){
    				noun[i] = inputNoun.nextLine();
    			}
    		}
    		for(int x = 0; x < verbs.length; x++){
    			while(inputVerb.hasNextLine()){
    				verbs[x] = inputVerb.nextLine();
    			}
    		}		
    		excuse = "My " + noun[j] + " " + verbs[n] + " my " + noun[m];
    		System.out.println(excuse);
    	}
    }
    Now, I tried replacing the index of the arrays in the format line with flat numbers, and it still gives me the same error. I have no idea what could be wrong, other than nothing being saved to the array, but I tested that earlier and it works just fine. Can anyone help?

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Error with excuse generator

    First of all, LOL at the program. It's a fantastic idea. :P

    Your problem seems to be that you're setting i = 0, then going through all of the words, then i = 1, then going through all the of the words but none are left. Instead, do something like this and see if it works:
    Java Code:
    int i = 0;
    while(inputNoun.hasNextLine())
    {
    	noun[i++] = inputNoun.nextLine();
    }
    Also, I would recommend using an ArrayList instead of an array with a preset size.

    Java Code:
    List<String> noun = new ArrayList<String>();
    while(inputNoun.hasNextLine())
    {
    	noun.add(inputNoun.nextLine());
    }
    Last edited by AndrewM16921; 06-17-2013 at 03:32 AM.

  3. #3
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Error with excuse generator

    Thanks, it works like a charm, and dear god it's hilarious. I've never used lists before, actually. I also added a small GUI to make it more convenient, with an option to save or generate a new excuse (just type "save" to save, hit OK to generate a new one, and "end" to terminate. The best thing is, the noun list is 1435 words long, and my verbs list is 151 long, giving me a total of 310,942,975 possible combinations. God I love programming. And Google.

    Java Code:
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    public class main {
    	public static void main(String[] args) {
    		Scanner inputNoun = new Scanner(System.in);
    		Scanner inputVerb = new Scanner(System.in);
    		Scanner input = new Scanner(System.in);
    		PrintWriter outputStream = null;
    		
    		String outFile = "out";
    		String words = null;
    		String excuse = null;
    		String nounFile = "nouns";
    		String verbFile = "verbs";
    		
    		List<String> noun = new ArrayList<String>();
    		List<String> verbs = new ArrayList<String>();
    		
    		Random rand = new Random();
    	
    		try{
    			inputNoun = new Scanner(new File(nounFile));
    			inputVerb = new Scanner(new File(verbFile));
    		}catch(Exception e){
    			System.out.println("Couldn't find the file(s)");
    		}
    		
    		try{
    			outputStream = new PrintWriter(outFile);
    		}catch(Exception e){
    			System.out.println("Couldn't find " +outFile);
    		}
    			
    			while(inputNoun.hasNextLine()){
    				 noun.add(inputNoun.nextLine());
    			}
    		
    			while(inputVerb.hasNextLine()){
    				verbs.add(inputVerb.nextLine());
    		}		
    		int j, m, n;
    			
    		while(words != "end"){
    		j = rand.nextInt(noun.size());
    		m = rand.nextInt(noun.size());
    		n = rand.nextInt(verbs.size());
    		excuse = "My " + noun.get(j) + " " + verbs.get(n) + " my " + noun.get(m);
    		words = JOptionPane.showInputDialog(excuse);
    		if(words.equals("save")){
    			outputStream.println(excuse);
    			outputStream.close();
    		}else if(words.equals("end")){
    			System.exit(0);
    			}
    		}
    		
    	}
    }

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Error with excuse generator

    A list would definitely be better in this case. Hypothetically, if you wanted to add or remove more words down the line you'd only need to edit the text file, and not the program itself. If you want to learn more this summer (other than just keeping what you know in your head), I definitely suggest you google tutorials on java collections - particularly LinkedList, ArrayList, HashSet, HashMap (these are probably the most commonly used). You'll probably come to love them. :P

  5. #5
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Error with excuse generator

    I'll definitely have to check those out, thanks.

Similar Threads

  1. ID Card Generator Help
    By werewolves12 in forum New To Java
    Replies: 1
    Last Post: 01-20-2012, 06:39 AM
  2. Match Generator Assignment Help / Error Message
    By kittykat850 in forum New To Java
    Replies: 9
    Last Post: 04-20-2011, 05:34 PM
  3. Class name generator
    By aianta in forum New To Java
    Replies: 3
    Last Post: 04-09-2011, 06:25 PM
  4. Random Generator
    By Learning Java in forum New To Java
    Replies: 9
    Last Post: 05-31-2010, 08:15 PM
  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
  •