Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Bluej Java - Quiz Help!!!
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Harblkipz




PostPosted: Tue Apr 08, 2008 8:05 am   Post subject: Bluej Java - Quiz Help!!!

Hey, I'm making a quiz in Bluej and I need help because I can't figure out how to change a input string into a int value. I have this half in terminal and half in dialogue boxes, if you can help make it all in dialogue boxes that will be helpful too. I have to put in 25 questions. I thank you in advance for your help. By the way, I used Hacker Dan's TUT for Swing for this. Not my oringinal work completely.

Here's my code.
code:

import javax.swing.*;    //import line to load the libray/package that
import java.io.*;                         //has the swing GUI components.

public class SwifhgfjfjdngGUI
{
       
       
    public static void main(String[] args)
    {

           int x1,score;

   x1=0;
   score = 0;
         
        //This is the code to disply the msg box.
        //JOptionPane.showMessageDialog(null, "This quiz is about Java, I hope you're prepared.", "JavaQuiz!", JOptionPane.WARNING_MESSAGE);
        //The 1st argument for the dialog is for a parent component, when no parent component is used you put it as null
        //The last argument is for the icon to be displayed with the dialog.
        //Thess are the comanan icons used in Java:
        //WARNING_MESSAGE
        //ERROR_MESSAGE
        //INFORMATION_MESSAGE
        //PLAIN_MESSAGE
        //QUESTION_MESSAGE
       
        /*Note: JOptionPane.showMessageDialog can be shorted to*/ JOptionPane.showMessageDialog(null,"This quiz is about Java, I hope you're prepared.");
        //if no title or icon setings are whonted or needed.
       
        System.out.println("A. What is the library you import into Java,uasually, at the begining of the Program?");
        System.out.println("1) java.oi.#");
        System.out.println("2) java.io.&");
        System.out.println("3) java.io.*");
        System.out.println("4) java.io.#");
   
        String uInput; //a varibale to store the users input
       
        //the JOptionPane.showInputDialog fuction returns a string of the users input
        uInput = JOptionPane.showInputDialog(null, "Type your answer here.(1, 2, 3, or 4)", "Answer Box", JOptionPane.ERROR_MESSAGE);
        //As with the msg Dialog the 1st argument is for a parent component and the last for
        //an icon to go with the dialog. (see posable icons under msg dialog)
        //The JOptionPane.showInputDialog fuction can be shortend to JOptionPane.showInputDialog("msg to show user")
        //if no other arrments are needed.
        //If the user hits Cancel insted of OK the return value will be null
       
        //Note: the string responce can be conveted to an other tpye if need
        //i may add how to do this in an update of this tutorale latter.
       
        //uInput=Integer.parseInt(x1);

       
        if (uInput == 1)
        {
            score = score+0;
           
        }
        else if (uInput == 2)
        {
            score = score+0;
        }
        else if (uInput == 3)
        {
            score = score+1;
        }
         else if (uInput == 4)
        {
            score = score+0;
        }
       
        System.out.println(" ");
        System.out.println("Next Question");
        System.out.println(" ");
       
    }
}


It has some bad code sorry about that. Thanks!
Sponsor
Sponsor
Sponsor
sponsor
Barbarrosa




PostPosted: Wed Apr 09, 2008 4:29 am   Post subject: Re: Bluej Java - Quiz Help!!!

with java 1.6:
code:

uInput=Integer.parseInt(x1); //this is wrong

x1=Integer.parseInt(uInput); //this is right

//hereafter, use x1 for comparison. You might also consider telling the user if they've used an invalid value(using a try/catch statement & JOptionPanel)
Harblkipz




PostPosted: Wed Apr 09, 2008 7:44 am   Post subject: Re: Bluej Java - Quiz Help!!!

Thanks a lot! That literally solved my biggest problem with this code.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: