2 Replies - 379 Views - Last Post: 05 August 2019 - 05:28 AM Rate Topic: -----

#1 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 22-October 18

nedd help wit this trivia test

Posted 02 August 2019 - 10:34 AM

hey guys i need helo with that trivia test(haryy potther trivia test) i ended code an when i run program and type harry.txt the qustion doesent shown...does anyone know why?? thanks for asnwers guys

package ground.pkg35triviatestgiver;

import java.io.FileNotFoundException;
import java.util.Scanner;


public class Ground35TriviatestGiver {

   
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input=new Scanner(System.in);
        System.out.print("enter file name: ");
        String filename=input.nextLine();
        TriviaTest test = new TriviaTest(filename);
        test.giveTEST();
        
        
       
    }
    
}





package ground.pkg35triviatestgiver;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TriviaTest {
    
    private String []correct;
    private String []response;
    private int numquestion;
    
    private Scanner input;
    private Scanner fileInput;
    
    public TriviaTest()
    {
        System.out.print("no fileName");
        System.exit(0);
    }
    
    public TriviaTest(String filename) throws FileNotFoundException
    {
        input=new Scanner(System.in);
        File questionFile=new File(filename);
        if(!questionFile.exists())
        {
            System.out.println("EROR "+filename+" not found");
            System.exit(0);
            
        }
            fileInput=new Scanner(questionFile);
            numquestion=input.nextInt();
            correct=new String[numquestion];
            for(int i=0;i<numquestion;i++)
                correct[i]=fileInput.next();
            response=new String[numquestion];
            fileInput.nextLine();
            
         
    }
    
    public void giveTEST()
    {
        System.out.println("answer the following question with t for true  or f for false");
        for(int i=0;i<numquestion;i++)
        {
           System.out.println((i+1)+"."+fileInput.nextLine());
           System.out.print("Answer: ");
           response[i]=(input.next().toUpperCase());
           System.out.println();
           
           
        }
        System.out.println("you scored "+TestScore()+" % on the test");
        
        showcorrect();
        
            fileInput.close();
        
    }
    private int TestScore()
    {
        int correctt=0;
        for(int i=0;i<numquestion;i++)
            if(response[i].equals(correct[i]))
                correctt++;
        
         return Math.round((100*correctt)/numquestion);
        
        
    }
    private void showcorrect()
    {
        for(int i=0;i<numquestion;i++)
            System.out.println((i+1)+"\t"+correct[i]+"\t\t"+response[i]);
    }
    
}





harry.txt
harry.txt

This post has been edited by kretze: 02 August 2019 - 10:40 AM


Is This A Good Question/Topic? 0
  • +

Replies To: nedd help wit this trivia test

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3639
  • View blog
  • Posts: 16,717
  • Joined: 20-September 08

Re: nedd help wit this trivia test

Posted 02 August 2019 - 11:05 AM

You need to place debugging statements in your code to find out what's going on. You seem to have expected user inputs without prompts too - that's not going to help

This post has been edited by g00se: 02 August 2019 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

#3 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 22-October 18

Re: nedd help wit this trivia test

Posted 05 August 2019 - 05:28 AM

so what do i have to do that this would work???
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1