Results 1 to 10 of 10

Thread: test program

  1. #1
    azaaza0909 is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Question test program

    I'm trying to make a program that will ask random questions from a list, it all worked until I added the code to answer and check the answer. It suddenly asked two questions before it waited for an answer. Because I couldn't find the solution to the problem I mad the code look better(declare all the variables in the same place, add some empty lines,..) and now it just doesn't ask the question! It's probably something really simple but because I started learning java a few hours ago I can't find the solution, please help me.
    better looking version:
    Java Code:
    import java.io.File;
    import java.util.*;
    
    public class test {
    	public static void main (String args[]){
    
    		Scanner x = null;
    		Scanner sam = new Scanner(System.in);
    		Random rand = new Random();
    		int vn=0;
    		int v[]=new int[vn];
    		int f[]=new int[vn];
    		String antw[]=new String[vn];
    		int tot =0;
    		
    		try{
    			x = new Scanner(new File("vragen.txt"));
    		}
    		catch(Exception e){
    			System.out.println("Couldn't find file");
    		}
    		int va=x.nextInt();
    		String vragen[]=new String[(va*3)-1];
    		for(int a=0; a<vragen.length; a++){
    			vragen[a]=x.next();
    		}
    		
    		x.close();
    		
    		
    		System.out.println("Aantal vragen?");
    		vn = sam.nextInt();
    		
    		
    		for(int a=0; a<v.length; a++){
    			if(a==0){
    				v[a] = rand.nextInt((va/vn)-1)+1;
    			}else {
    				v[a] = v[a-1]+rand.nextInt((va/vn)-1)+1;
    			}
    		}
    		
    		for(int a=0; a<v.length; a++){
    			System.out.println(vragen[v[a]+(2*(v[a]-1))]);
    			antw[v[a]] = sam.nextLine();
    			if(antw[v[a]].equals(antw[v[a]+(2*(v[a]-1))+1])){
    				f[a+1]=1;
    				tot = tot++;
    			}else{
    				f[a+1]=0;
    			}
    		}
    		System.out.println(tot + "/" + vn);
    		
    			
    	}
    }
    previous version:
    Java Code:
    import java.io.File;
    import java.util.*;
    
    public class test {
    	public static void main (String args[]){
    
    		Scanner x = null;
    		
    		try{
    			x = new Scanner(new File("vragen.txt"));
    		}
    		catch(Exception e){
    			System.out.println("Couldn't find file");
    		}
    		int va=x.nextInt();
    		String vragen[]=new String[(va*3)-1];
    		for(int a=0; a<vragen.length; a++){
    			vragen[a]=x.next();
    		}
    		
    		x.close();
    		
    		
    		Scanner sam = new Scanner(System.in);
    		Random rand = new Random();
    		
    		int vn;
    		
    		System.out.println("Aantal vragen?");
    		vn = sam.nextInt();
    		
    		int v[]=new int[vn];
    		
    		for(int a=0; a<v.length; a++){
    			if(a==0){
    				v[a] = rand.nextInt((va/vn)-1)+1;
    			}else {
    				v[a] = v[a-1]+rand.nextInt((va/vn)-1)+1;
    			}
    		}
    		int f[]=new int[vn];
    		String antw[]=new String[vn];
    		int tot =0;
    		
    		for(int a=0; a<v.length; a++){
    				System.out.println(vragen[v[a]+(2*(v[a]-1))]);
    				antw[v[a]] = sam.nextLine();
    				if(antw[v[a]].equals(antw[v[a]+(2*(v[a]-1))+1])){
    					f[a+1]=1;
    					tot = tot++;
    				}else{
    					f[a+1]=0;
    				}
    			}
    			System.out.println(tot + "/" + vn);
    		
    			
    	}
    }
    and this is the text file I use to store the questions:
    Java Code:
    20
    1 test1? test1
    2 test2? test2
    3 test3? test3
    4 test4? test4
    5 test5? test5
    6 test6? test6
    7 test7? test7
    8 test8? test8
    9 test9? test9
    10 test10? test10
    11 test11? test11
    12 test12? test12
    13 test13? test13
    14 test14? test14
    15 test15? test15
    16 test16? test16
    17 test17? test17
    18 test18? test18
    19 test19? test19
    20 test20? test20
    there aren't any real questions in it yet.

  2. #2
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: test program

    First thing is that your array that holds the questions and answers is missing the answer for the 20th question.

    Next you declare the arrays for both v and f to be of size vn, which vn is 0. So the program never goes into the loops. That is why your questions are not asked/printed.
    you should declare v and f after
    Java Code:
    vn = sam.nextInt();
    To ensure that the array sizes are correct.

  3. #3
    azaaza0909 is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default Re: test program

    ok thanks but it now outputs this
    Java Code:
    Aantal vragen?
    5
    test2?
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    	at test.main(test.java:46)
    or this when the first question is test1?
    Java Code:
    Aantal vragen?
    5
    test1?
    test2?
    And why isn't my 20th answer included?

  4. #4
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: test program

    The error is coming from
    Java Code:
    antw[v[a]] = sam.nextLine();
    It looks like you get a random number and placing it in v[] and here you are accessing the spot in the antw array which doesn't exist. I commented those spots in the code
    Java Code:
    import java.io.File;
    import java.util.*;
     
    public class test {
        public static void main (String args[]){
     
            Scanner x = null;
            Scanner sam = new Scanner(System.in);
            Random rand = new Random();
            int vn=0;
            int v[]=new int[vn];
            int f[]=new int[vn];
            String antw[]=new String[vn];
            int tot =0;
             
            try{
                x = new Scanner(new File("vragen.txt"));
            }
            catch(Exception e){
                System.out.println("Couldn't find file");
            }
            int va=x.nextInt();
            String vragen[]=new String[(va*3)-1];
            for(int a=0; a<vragen.length; a++){
                vragen[a]=x.next();
            }
             
            x.close();
             
             
            System.out.println("Aantal vragen?");
            vn = sam.nextInt();
             
             
            for(int a=0; a<v.length; a++){
                if(a==0){
                    v[a] = rand.nextInt((va/vn)-1)+1;//<--PLACES RANDOM NUMBER INSIDE v[]
                }else {
                    v[a] = v[a-1]+rand.nextInt((va/vn)-1)+1;//<--PLACES RANDOM NUMBER INSIDE v[]
                }
            }
             
            for(int a=0; a<v.length; a++){
                System.out.println(vragen[v[a]+(2*(v[a]-1))]);
                antw[v[a]] = sam.nextLine();//<--TRIES TO PLACE INSIDE A SPOT IN ARRAY THAT DOES NOT EXIST
                if(antw[v[a]].equals(antw[v[a]+(2*(v[a]-1))+1])){
                    f[a+1]=1;
                    tot = tot++;
                }else{
                    f[a+1]=0;
                }
            }
            System.out.println(tot + "/" + vn);
             
                 
        }
    }

  5. #5
    azaaza0909 is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default Re: test program

    ok so I replaced antw[v[a]] with antw[a] is that correct?
    But I still get two questions at once when the first one is "test1?" else I get
    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11//<= this number changes
    	at test.main(test.java:47)
    how do I fix that?

  6. #6
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: test program

    First we need to move this below the declaration of vn also
    Java Code:
    vn = sam.nextInt();
    String antw[]=new String[vn];
    Next you are using nextLine() which gets the actually pulls the "enter" input from the last time you called nextInt(). You will actually want to change that to

    Java Code:
     antw[a] = sam.next();

  7. #7
    azaaza0909 is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default Re: test program

    Ok thanks that worked, just one problem to go. When I run it I get this

    Java Code:
    Aantal vragen?//how many questions I want
    5//my answer
    test2?//the first question
    test2//my answer
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5//And then I get this
    	at test.main(test.java:47)
    the code I have now:
    Java Code:
    import java.io.File;
    import java.util.*;
     
    public class test {
        public static void main (String args[]){
     
            Scanner x = null;
            Scanner sam = new Scanner(System.in);
            Random rand = new Random();
            int vn;
            int tot =0;
             
            try{
                x = new Scanner(new File("vragen.txt"));
            }
            catch(Exception e){
                System.out.println("Couldn't find file");
            }
            int va=x.nextInt();
            String vragen[]=new String[(va*3)-1];
            for(int a=0; a<vragen.length; a++){
                vragen[a]=x.next();
            }
             
            x.close();
             
             
            System.out.println("Aantal vragen?");
            vn = sam.nextInt();
            
            int v[]=new int[vn];
            int f[]=new int[vn];
            String antw[]=new String[vn];
             
             
            for(int a=0; a<v.length; a++){
                if(a==0){
                    v[a] = rand.nextInt((va/vn))+1;
                }else {
                    v[a] = v[a-1]+rand.nextInt((va/vn))+1;
                }
            }
             
            for(int a=0; a<v.length; a++){
                System.out.println(vragen[v[a]+(2*(v[a]-1))]);
                antw[a] = sam.next();
                if(antw[a].equals(antw[v[a]+(2*(v[a]-1))+1])){
                    f[a+1]=1;
                    tot = tot++;
                }else{
                    f[a+1]=0;
                }
            }
            System.out.println(tot + "/" + vn);
             
                 
        }
    }

  8. #8
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: test program

    try going from
    Java Code:
    antw[v[a]+(2*(v[a]-1))+1]
    //TO
    vragen[v[a]+(2*(v[a]-1))+1]
    on line 47 and also f[a+1] on lines 48 and 51 will be out of index
    They should be just f[a]


    and finally tot = tot++; will not work its either has to be
    Java Code:
    tot++; or tot +=1;
    Last edited by jhuber151; 07-17-2012 at 10:42 PM.

  9. #9
    azaaza0909 is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default Re: test program

    Wow thanks for helping me it works perfectly now!

  10. #10
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

Similar Threads

  1. GUI Array help...test score program
    By VettesRus in forum New To Java
    Replies: 4
    Last Post: 07-18-2012, 01:35 AM
  2. Print Test page directly from Java program
    By irsbenz in forum New To Java
    Replies: 3
    Last Post: 10-21-2011, 06:14 AM
  3. Replies: 10
    Last Post: 05-01-2010, 09:51 PM
  4. Replies: 0
    Last Post: 04-05-2008, 12:38 AM

Tags for this Thread

Posting Permissions

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