Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting into Java: French Verb Conjugation Program
#19
When dealing with user input, a do-while is best.

Code:
do{
            Scanner input = new Scanner(System.in);
            String in = input.nextLine();
            
            if (in.equals("exit()")){
                isRunning = False;
            }
            else
            {

            String infinitive = in.split(" ")[0];
            if (infinitive.length() < 2){ // If infinitive is too short, demand another one
                System.out.println("The infinitive is too short. Please enter another:\n"); //<-- RIGHT HERE
                continue;
            }    
            
            String tense = "present"; // If no tense is entered, default to present
            if (in.split(" ").length >= 2){
                tense = in.split(" ")[1];  //MIGHT WANT TO VERIFY THAT THE TENSE IS ONE THAT YOU ACCEPT. IS THE TENSE TO BE WRITTEN IN FRENCH OR ENGLISH?
            }
            
            Verb MyVerb = new Verb(infinitive);

            for (int i = 0; i < 6; i++) {
                System.out.println(MyVerb.getPronoun(i) + " " + MyVerb.conjugate(i, tense));
            }
            System.out.println(""); //spacer after each verb to make the output more readable
            System.out.print("Ready for another verb. Remember the format is <verb> <tense>\n");
            }

        } while(isRunning);

For the INI file, put the try/catch in the constructor so that it's only loaded once for the entire conjugation process. As it stands, you're loading it 6 times for every verb.

Something else that I"m noticing is that you're calling the conjugate method 6 times to get all 6 conjugations. Ideally, from outside the object, other objects don't need to know that there are six types of conjugations. What if you want to add or subtract conjugations later? Just call conjugate once and let the loop in conjugate take care of it. Make it return an array, then tell your main method to loop through the array and print it out.

As an additional level of complexity, try adding the following features:

- Conjugation of reflexive verbs (se marier)
- Ability to conjugate in multiple tenses at the same time (se marier present pastperfect)
Reply


Messages In This Thread
Getting into Java: French Verb Conjugation Program - by Fiel - 2010-10-20, 06:53 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)