Java Program Help

Discussion in 'General Off-Topic Chat' started by nintendofreak, Jan 10, 2007.

Jan 10, 2007

Java Program Help by nintendofreak at 8:26 PM (1,475 Views / 0 Likes) 15 replies

  1. nintendofreak
    OP

    Member nintendofreak Around. Shoot me a PM.

    Joined:
    Mar 27, 2006
    Messages:
    1,333
    Location:
    Cal Poly Pomona / (323)
    Country:
    United States
    Ive got this program that i need to do for my java class:

    CODE/**
    Âfile: DriverExam.java
    ÂAuthor: (for now) Pedro Chavez
    ÂDate last modified: 1/10/07
    ÂThis class compares the input of a char array
    Âto that of the "correct" array, and returns
    Âcertain characteristics


    ============================================================
    TO DO: Make comments compatable with javadoc!!


    */
    import java.util.*;

    public class DriverExam
    Â{
    Â ÂScanner key = new Scanner(System.in);
    Â ÂString usersAns[] = new String[20];

    Â ÂString Correct[] = {"b","d","a","a","c","a","b",
    Â Â Â Â Â Â Â "a","c","d","b","c","d","a",
    Â Â Â Â Â Â Â "d","c","c","b","d","a"};
    Â Â//this method ask the user to input his/her answers and
    ÂÂÂÂ//copies it to the
    ÂÂÂÂ//usersAns[] array so that we can work on it

    Â Â//make a array to hold the users answers...

    Â Âpublic void display()
    Â Â Â{
    Â Â for(int v=0;v
     
  2. Omero

    Former Staff Omero Former Admin

    Joined:
    Oct 23, 2002
    Messages:
    907
    Location:
    Milan, Italy
    Country:
    Italy
    Main method has to be static.

    public static main(String[] args) {}

    Enjoy.
     
  3. Psyfira

    Member Psyfira Credit: 0ml. Insert tea to continue

    Joined:
    Dec 31, 2003
    Messages:
    3,887
    Location:
    England
    Country:
    United Kingdom
    Can't test compiling it here cause the other classes it needs (Scanner) are missing, but on first glance (and you're going to kick yourself if I'm right...)

    public static void main.... [​IMG]

    [Edit] yeah, what he said.
    [Edit 2] Wait a sec, Scanner is in the standard API... why on earth am I still running Java 1.4? :wtf:
     
  4. cenotaph

    Member cenotaph GBAtemp Fan

    Joined:
    Oct 24, 2006
    Messages:
    341
    Country:
    United States
    It would be to your advantage if you put some code in your main method as your class will be pretty worthless otherwise, unless you call it as an instance from somewhere else or something (but I guess you might not've gotten to that part yet).
     
  5. nintendofreak
    OP

    Member nintendofreak Around. Shoot me a PM.

    Joined:
    Mar 27, 2006
    Messages:
    1,333
    Location:
    Cal Poly Pomona / (323)
    Country:
    United States
    yuuppp but im trying to get rid of the error first before i get it to do anything [​IMG] working on it now..

    EDIT: Havent got to the static part of the book yet, so can someone please explain to me when i should use it. [​IMG] I know you put it when a method is made to run off a instance of a class, but since its all in the same class.. why do we need it?

    EDIT EDIT: Or is is that you DONT put it when its made to run off the instance of a class...? [​IMG]
     
  6. cenotaph

    Member cenotaph GBAtemp Fan

    Joined:
    Oct 24, 2006
    Messages:
    341
    Country:
    United States
    A non-static method can only be called using an instance of the class it's a member of, whereas a static method can be called from anywhere its scope permits. Note that you can't reference non-static objects from a static context (i.e. you can't call a non-static method from a static method as it will typically depend on instance-specific variables).
     
  7. nintendofreak
    OP

    Member nintendofreak Around. Shoot me a PM.

    Joined:
    Mar 27, 2006
    Messages:
    1,333
    Location:
    Cal Poly Pomona / (323)
    Country:
    United States
    New error [​IMG]
    CODE/**
    Âfile: DriverExam.java
    ÂAuthor: (for now) Pedro Chavez
    ÂDate last modified: 1/10/07
    ÂThis class compares the input of a char array
    Âto that of the "correct" array, and returns
    Âcertain characteristics


    ============================================================
    TO DO: Make comments compatable with javadoc!!


    */
    import java.util.*;

    public class DriverExam
    Â{
    Â Âstatic Scanner key = new Scanner(System.in);
    Â Âstatic String usersAns[] = new String[20];

    Â Âstatic String Correct[] = {"b","d","a","a","c","a","b",
    Â Â Â Â Â Â Â "a","c","d","b","c","d","a",
    Â Â Â Â Â Â Â "d","c","c","b","d","a"};
    Â Â//this method ask the user to input his/her answers and
    ÂÂÂÂ//copies it to the
    ÂÂÂÂ//usersAns[] array so that we can work on it

    Â Â//make a array to hold the users answers...

    Â Âpublic void display()
    Â Â Â{
    Â Â for(int v=0;v
     
  8. cenotaph

    Member cenotaph GBAtemp Fan

    Joined:
    Oct 24, 2006
    Messages:
    341
    Country:
    United States
    checkAnswers() isn't properly closed, you need another curly brace (expressions not being properly terminated is usually the cause of 'illegal expression'-type errors). Also, you've referenced lots of non-static variables from static contexts, which'll land you a myriad of errors. It's also not possible to compare strings by using the = operator, you'll have to use the equals-method (yourstring.equals("somestring")) from java.lang.String (basically, your variables are just instances of various classes within the java.lang-package and as such you'll be able to use the instance methods present within those classes).

    If this was too abstract, let me know and I'll walk you through using something more concrete.
     
  9. Psyfira

    Member Psyfira Credit: 0ml. Insert tea to continue

    Joined:
    Dec 31, 2003
    Messages:
    3,887
    Location:
    England
    Country:
    United Kingdom
    What he said. That one's caught me out loads of times.

    (As a side note it's not possible to compare anything with the = operator cause that's assignment, == is comparison [​IMG] (/smuglittlegit) )
     
  10. Jeda

    Member Jeda GBAtemp Fan

    Joined:
    Nov 7, 2002
    Messages:
    329
    Location:
    Germany
    Country:
    Germany
    do you use an ide, like eclipse? Those things really help!
     
  11. nintendofreak
    OP

    Member nintendofreak Around. Shoot me a PM.

    Joined:
    Mar 27, 2006
    Messages:
    1,333
    Location:
    Cal Poly Pomona / (323)
    Country:
    United States
    woo hoo, im just about done [​IMG]
    Just need to add the java doc things... [​IMG]

    CODE/**
    Âfile: DriverExam.java
    ÂAuthor: (for now) Pedro Chavez
    ÂDate last modified: 1/10/07
    ÂThis class compares the input of a char array
    Âto that of the "correct" array, and returns
    Âcertain characteristics


    ============================================================
    TO DO: Make comments compatable with javadoc!!


    */
    import java.util.*;

    public class DriverExam
    Â{
    Â Âstatic Scanner key = new Scanner(System.in);
    Â Âstatic String usersAns[] = new String[20];

    Â Âstatic String Correct[] = {"b","d","a","a","c","a","b",
    Â Â Â Â Â Â Â "a","c","d","b","c","d","a",
    Â Â Â Â Â Â Â "d","c","c","b","d","a"};
    Â Â//this method ask the user to input his/her answers and
    ÂÂÂÂ//copies it to the
    ÂÂÂÂ//usersAns[] array so that we can work on it

    Â Â//make a array to hold the users answers...

    Â Âpublic void display()
    Â Â Â{
    Â Â for(int v=0;v
     
  12. Jeda

    Member Jeda GBAtemp Fan

    Joined:
    Nov 7, 2002
    Messages:
    329
    Location:
    Germany
    Country:
    Germany
    yeah try eclipse. I pressed ctrl+shift+f to get your code formated into a "readable" way. Some notes:

    Code:
     ÂÂÂÂÂif (input.equalsIgnoreCase("a") || input.equalsIgnoreCase("b")
    Â ÂÂÂÂÂ|| input.equalsIgnoreCase("c")) {
    You are missing the "d" case there!


    Oh and also on a logical thing:

    CODEfor(int sindex = 0; sindex
     
  13. nintendofreak
    OP

    Member nintendofreak Around. Shoot me a PM.

    Joined:
    Mar 27, 2006
    Messages:
    1,333
    Location:
    Cal Poly Pomona / (323)
    Country:
    United States

    ah yes the scope forgot about that [​IMG]

    im having problems though, If i get ALL the answers wrong, it doesnt display the wrong ones.. cant find out why [​IMG]
     
  14. Jeda

    Member Jeda GBAtemp Fan

    Joined:
    Nov 7, 2002
    Messages:
    329
    Location:
    Germany
    Country:
    Germany
    look out for the error messages you get. In this case you get something like this:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
    at DriveTest.displayWrongOnes(DriveTest.java:96)
    at DriveTest.main(DriveTest.java:124)

    Array out of bounds:20 means you are trying to write at position 20 of an array that doesn't have such a field. It also tells you what line to check.

    ps: my lines won't match yours.
     
  15. nintendofreak
    OP

    Member nintendofreak Around. Shoot me a PM.

    Joined:
    Mar 27, 2006
    Messages:
    1,333
    Location:
    Cal Poly Pomona / (323)
    Country:
    United States
    ah yes, similar error:

    Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
    Â Â Â Âat DriverExam.displayWrongOnes(DriverExam.java:107)
    Â Â Â Âat DriverExam.main(DriverExam.java:144)
     
  16. cenotaph

    Member cenotaph GBAtemp Fan

    Joined:
    Oct 24, 2006
    Messages:
    341
    Country:
    United States
    This won't work:
    Code:
     Â Âint dWOControl = 1;
    Â Â Âwhile(wrongOnes[dWOControl] != 0)
    Â Â Â Â{
    Â ÂSystem.out.print("The wrong ones are question: ");
    Â ÂSystem.out.println("#"+wrongOnes[dWOControl]);
    Â ÂdWOControl++;
    Â Â }
    You'll have to do something like this:

    CODEfor (int i=0;i
     

Share This Page