r/javahelp•
H1dden

Having difficulties with simple syntax errors, perhaps someone can assist me

Hey guys, I'm making this Counter Strike guide for my grade 12 computer science class final assignment and I can't seem to figure out what is wrong with the code! I'd really appreciate any help or feedback you guys can give, even if you can't fix the code it's fine!

code: http://pastebin.com/S3TtbrA9

Posted Jun 15, 2014, 4:36 PM UTC

29
Lerke•

Going over it really quickly, there's a few very obvious errors.

charName = input.nextLine();
if (charName = "Terrorist") {

charName is of type String. String comparisons should be done with the .equals() method. Aside from that, you're assigning charName a value in your if-statement, which is not what you're looking for. I think you're looking for this:

if (charName.equals("Terrorist")) {

2.

  switch(charChoice) {
    case 1: choosePhoenix();
    case 2: chooseElite();
    case 3: chooseArtic();
    case 4: chooseWarfare();
  }

While your switch and cases are fine, you need to use the break; statement after each of your cases (except 4). Because now, if case 1 is picked, your program will execute choosePhoenix(), and then, when it's done, execute chooseElite, chooseArtic and chooseWelfare as well. So change this into:

  switch(charChoice) {
    case 1: choosePhoenix();
                break;
    case 2: chooseElite();
                break;
    case 3: chooseArtic();
                break;
    case 4: chooseWarfare();
                break;
  }

This goes for the Counter-Terrorist section as well.

On line 278, you forget to close a String.

  System.out.println("Press 1 for M249);

should be

  System.out.println("Press 1 for M249");

At several points, you seem to be using a very odd " character. Namely this one: ”. As far as I know, this is not valid and should be replaced with ".

3
desrtfxFun Brewer•

From a quick look over the code, I say that u/Lerke got most of the errors.

Are you using an IDE? If not, then you should switch to an IDE because 99% of your code errors would have been caught by the IDE.

All these errors you are getting are precisely described when you try to compile. Learn to read and understand the error messages. This is absolutely essential.

Look at your lines 10 to 13:

System.out.println("Are you a Terrorist or Counter-Terrorist");
charName = input.nextLine();

if (charName = "Terrorist") {

Lines 10 and 11 are fine, but the big mistake is in Line 13:

if (charName = "Terrorist") {

Here, you are assigning the String "Terrorist" to the variable charName. What you want to achieve is a comparison. The comparison operator in general is == (two equals signs). Unfortunately, this doesn't apply to Strings or other Objects. In Java Strings are Objects and therefore are compared with .equals or .equalsIgnoreCase.

So, Line 13 should be:

if ("Terrorist".equalsIgnoreCase(charName)) {

Note that I've put the String literal (the constant String) before the variable. This way, if the user does not enter anything, a NullPointerException is avoided. Had the comparison been the other way round - like:

if (charName.equalsIgnoreCase("Terrorist")) {

and the user had not entered anything, a NullPointerException would be thrown by the runtime.

Switch - Case constructs need break - as u/Lerke pointed out. Also, it is bad practice not to have a default case that gets called when none of the individual cases fit. Let's say, the user enters 5 for the first case - what will happen in your program?

In Lines 118, 120, 122, 190, and 278 you did not terminate the String Literal - the closing double-quotes are missing - the pastebin already highlights this fact.

Really: Use an IDE (any of Eclipse, Netbeans, or IntelliJ Community Edition will do) - and learn to interpret the error messages - it's essential.

3
toastykittenzI brew kittens•

Here is your code with all the syntax errors fixed. I think it should be working as you want it:

http://pastebin.com/0TDXVAB1

Please note:

  1. Your ChooseRifles() method was missing. I created it but it is missing a body.

    if (charName = "Terrorist") {

should be

    if (charName.equals("Terrorist")) {

3. Switch statements should always have the form of:

switch(condition) {
case 1: method1();
break;
case 2: method2();
break;
case 3: method3();
break;

}

You seemed to be either missing the break statements, or the semicolon after the case x: methodx() line.

4.) When you print strings, make sure you remember to use closing quotations. For example,

System.out.println("This is a string);

should be

System.out.println("This is a string");

5.) You created methods inside your main method. main() is a method itself. You cannot create new methods inside methods.

Which brings me to the next note,

6.) Keep track of your curly braces. When you use a switch statement, be sure to remember the closing curly brace.

Use indention to help you.

7.) Either pass your Scanner as a parameter, create a new one in the methods it is used, or make it a global variable. Some methods used a Scanner but couldn't use it because it was declared in your main method. I made the Scanner global for simplicity.

8.) Weird quotation character: ” For some of your code, you were using the quotation character: ” which is not the same as ". This cause a syntax error. You may have copied and pasted your string names from somewhere, and this quotation character was added with it. Avoid copying and pasting. It can cause weird syntax errors like this one, because although the characters look the same, they have different ASCII values.

That's all I can remember.

Good luck.

Also, for Java development, you may want to look into the IDE Eclipse. It is a helpful text editor that can help you spot majority of the syntax errors you made.

A lot of people will tell you it is bad for beginners to use an IDE, but I disagree, and think it will help you learn a lot, because you will spot your mistakes as you make them.

Lastly, test and run your code as you are programming, do not wait for the syntax errors to build up. Program little by little, and make sure it works along the way so you know which method/lines you messed up on.

Good luck!

2
toastykittenzI brew kittens•

What exactly are the errors you are getting? I guess I can try to run the code myself. One sec.

1
H1ddenOP•

I'm getting syntax errors on misplaced tokens and constructs.

0
aqua_regis•

Learn to read and understand these errors.

Don't rely on others to fix them for you. You'll never learn anything if you don't work these things out by yourself.

3
H1ddenOP•

I'm aware of that but I'd like to have a copy of the code that works for reference.

0
aqua_regis•

Don't go that road. It will help you much less than you'd expect.

Only if you find, understand, and fix the errors yourself, you will remember not to make them again (at least this will apply after about the elevtenty-fifth time ;)

1
toastykittenzI brew kittens•

I have found a bunch of syntax errors. I will fix them and point out the main mistakes that you have made.

2