Results 1 to 8 of 8
  1. #1
    Andie is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default String Arrays - Fill in the blank program

    I'm having a bit of a trouble with my program. It does compile and run fine, but when typing in any answer when prompt to, it would say that it is correct no matter if the answer is wrong or right. If I change the number past 8 in s[8] = sc.nextLine();, it would say Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: (whatever number is used in the s[]... i.e 9) at FIB.main(FIB.java:23).

    What I need to do is to write a program where that would read a fill in the blank question. If the user does not provide a question, the program should display an error message and quit. Once a question is entered, the program should display the question with the answer displayed as a series of underscore characters. Prompting the user for the answer, and display whether the response provided matches the correct answer. The question should be entered as command line arguments, with the word to be guessed surrounded by square brackets. A sample question might be,

    The school’s name is now College of the [Ouachitas].

    Any suggestion or thoughts?



    import java.util.*;

    class FIB {
    public static void main(String[] args){
    Scanner sc = new Scanner(System.in); //For scanner

    int i = 0;
    int num=args.length;
    String s[]=new String[num];


    String orgstr = "The school's name is now College of the [Ouachitas].";

    String substr = orgstr.substring(41,50);

    System.out.println("Fill in the blank: ");
    System.out.println("\n");
    System.out.println("The school's name is now College of the ___________.");
    System.out.println("\n");
    s[8] = sc.nextLine();

    if(substr.equals("Ouachitas")) {

    System.out.println("Correct!");
    }

    else{

    System.out.println("Sorry! Wrong Answer!");

    }

    }
    }

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: String Arrays - Fill in the blank program

    Hi,

    In this section of your program you are declaring an array with the following code:

    Java Code:
    int num=args.length;
    String s[]=new String[num];
    The num variable is set to the number of parameters you pass when you are executing your program. Let say you executed your program like:

    Java Code:
    java FIB a b c
    This will make your s array length to three, s[3]. If you don't give any parameter then it will be, s[0]. This mean when you access it with index larger then the size of your array you'll have an java.lang.ArrayIndexOutOfBoundsException error in your program.

  3. #3
    Andie is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: String Arrays - Fill in the blank program

    I'm able to fix that; however, when I put the right or wrong answer to the question in there, it will say "Correct!". Not for sure if it is something to do with the if-else statement.

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: String Arrays - Fill in the blank program

    Can you re-post the latest code that you have fixed so far?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: String Arrays - Fill in the blank program

    when I put the right or wrong answer to the question in there, it will say "Correct!".
    Where did you ever test the value contained by s[8], which is assigned the user input?
    Where did you ever change the value of substr after accepting user input?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Andie is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: String Arrays - Fill in the blank program

    I don't know what I did wrong, but now it is saying that any answer is wrong now. I'm feeling like scrapping the whole thing and just go without sleep tonight in order to finish it by tomorrow morning.

    import java.util.*;

    class FIB {
    public static void main(String[] args){
    Scanner sc = new Scanner(System.in); //For scanner

    int i = 0;
    int num=args.length;
    String s[]=new String[num];

    String orgstr = "The school's name is now College of the __________";
    String substr = orgstr.substring(41,50);

    System.out.println("Fill in the blank: ");
    System.out.println("\n");
    System.out.println("The school's name is now College of the ___________.");
    System.out.println("\n");
    s[0] = sc.nextLine();

    if(substr.equals("Ouachitas")) {

    System.out.println("Correct!");
    }

    else{

    System.out.println("Sorry! Wrong Answer!");

    }
    }
    }

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: String Arrays - Fill in the blank program

    Quote Originally Posted by Andie View Post
    I'm feeling like scrapping the whole thing and just go without sleep tonight in order to finish it by tomorrow morning.
    Probably a wise decision.

    Before you post any more code here, go through the forum FAQs and learn how to post code so that it retains its formatting.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Andie is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: String Arrays - Fill in the blank program

    I've rewritten the code again and this is what I gotten so far. I'll add the if-else statement after awhile, but the problem I have now is that I need the whole question to show up that was typed in the command prompt. I can't seem to remember how to correct it. I know it has to do with the int i = 0 and I don't really need a for loop because I just need for it to ask 'Fill in the blank' only once. Any suggestions on how correct it? Thanks.

    import java.util.*;

    class FIB {
    public static void main(String args[]) {
    Scanner sc = new Scanner(System.in); // For scanner
    int i = 0;

    String s[] = new String[args.length];

    System.out.println("Fill in the blank: ");
    System.out.println("\n");
    System.out.print(args[i] + " ____________");
    System.out.println("\n");
    s[0] = sc.nextLine();


    }
    }

Similar Threads

  1. blank space in String won't die!
    By MetalR0 in forum New To Java
    Replies: 2
    Last Post: 07-29-2011, 04:56 AM
  2. Fill in the blank code for arrays
    By chuckypalumbo in forum New To Java
    Replies: 1
    Last Post: 05-02-2011, 01:17 AM
  3. Replies: 1
    Last Post: 04-26-2011, 02:25 AM
  4. Get item from string array and pack with blank space
    By firewalll in forum New To Java
    Replies: 2
    Last Post: 09-02-2009, 07:38 AM

Posting Permissions

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