Java Program Help

nintendofreak

Around. Shoot me a PM.
Member
Joined
Mar 27, 2006
Messages
1,333
Reaction score
5
Trophies
1
Age
37
XP
467
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
 
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....
smile.gif


[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? O_o
 
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).
 
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).

yuuppp but im trying to get rid of the error first before i get it to do anything
biggrin.gif
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.
biggrin.gif
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...?
unsure.gif
 
EDIT: Havent got to the static part of the book yet, so can someone please explain to me when i should use it.
biggrin.gif
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...?
unsure.gif

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).
 
New error
frown.gif

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
 
Code:
C:\CS 141\Group Work\1\Driver Lisence exam\DriverExam.java:86: illegal start of expression
 Âpublic static void checkAnswers()
 Â^
C:\CS 141\Group Work\1\Driver Lisence exam\DriverExam.java:153: ';' expected
^
C:\CS 141\Group Work\1\Driver Lisence exam\DriverExam.java:156: '}' expected
^
3 errors

Thanks everyone whos helping me
smile.gif

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.
 
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
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
biggrin.gif
(/smuglittlegit) )
 
woo hoo, im just about done
biggrin.gif

Just need to add the java doc things...
dry.gif


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
 
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
 
A variable is only availble in the content it is created. So in this case, as
soon as the for block ends, sindex is deleted. So this comment is kind of out of place as sindex cannot be used anywhere else anyway. This also means you can use the same index for all your for blocks if you want so.


ah yes the scope forgot about that
biggrin.gif


im having problems though, If i get ALL the answers wrong, it doesnt display the wrong ones.. cant find out why
mad.gif
 
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.
 
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)
 
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
 

Site & Scene News

Popular threads in this forum