Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Wierd Compiler Error  (Read 772 times)
0 Members and 1 Guest are viewing this topic.
Offline chaucer345
« Posted 2013-12-28 19:30:23 »

Hey guys, I've got this little chunk of code I'm working on, and as far as I can tell it should work... but Eclipse reports a major compiler error from it for some reason.

1  
2  
3  
4  
5  
6  
7  
private int combatOptionsCount = 0;
   String[] combatOptions =  new String[5];
   combatOptions[combatOptionsCount] = "Tackle it!";
   combatOptionsCount++;
   combatOptions[combatOptionsCount] = "Throw something!";
   combatOptionsCount++;
   //Keep to this format and it should all work out.


It says: "Syntax error on token ";", { expected after this token"
for the third line down, but I have no idea why it would need that, it's not a function, or the start of a block, just a declaration. Does anyone have an idea what's going on here?
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 835
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2013-12-28 19:53:16 »

The syntax error is clearly not in the code you provided. Please post the full class.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline chaucer345
« Reply #2 - Posted 2013-12-28 20:07:14 »

The syntax error is clearly not in the code you provided. Please post the full class.

Unfortunately the full class would be a little tricky to post as it's over 5000 lines long (a lot of string literals). Still this might help, here, starting with line 3, is the entire region Eclipse underlined in red (ending at "public int getLargestChoice() {"):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
   
   private int combatOptionsCount = 0;
   String[] combatOptions =  new String[5];
   combatOptions[combatOptionsCount] = "Tackle it!";
   combatOptionsCount++;
   combatOptions[combatOptionsCount] = "Throw something!";
   combatOptionsCount++;
   //Keep to this format and it should all work out.
   
   
   // Calls the method based on scene, also sets input to the passed parameter
   public String returnNewText(int currentInput) {
      input = currentInput;
      String newText = "";
      if (scene == 1) {
         newText = this.scene1();
      }
      if (scene == 2) {
         newText = this.scene2();
      }
      if (scene == 3) {
         newText = this.scene3();
      }
      if (scene == 4) {
         newText = this.scene4();
      }
      if (scene == 5) {
         newText = this.scene5();
      }
      if (scene == 6) {
         newText = this.scene6();
      }
      if (scene == 7) {
         newText = this.scene7();
      }
      if (scene == 8) {
         newText = this.scene8();
      }
      if (scene == 9) {
         newText = this.scene9();
      }
      if (scene == 10) {
         newText = this.scene10();
      }
      return newText;
   }

   // returns the largest choice based on how the int variable, largestChoice,
   // is modified
   public int getLargestChoice() {
      return largestChoice;
   }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 835
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2013-12-28 20:11:04 »

you can't have that code outside of a method block / static block / instance block (of whatever the name is).

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
class X
{
   private int combatOptionsCount = 0;
   String[] combatOptions =  new String[5];

+ { // it's like a static block, without the static part >_>
     combatOptions[combatOptionsCount] = "Tackle it!";
     combatOptionsCount++;
     combatOptions[combatOptionsCount] = "Throw something!";
     combatOptionsCount++;
+ }


}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline StrideColossus
« Reply #4 - Posted 2013-12-28 20:15:50 »

Unfortunately the full class would be a little tricky to post as it's over 5000 lines long (a lot of string literals).

Urgh!  Break up your code into logical chunks, otherwise you'll only be facing more and more nasties like this.

Quote
Still this might help, here, starting with line 3, is the entire region eclipse underlined in red:

1  
2  
3  
4  
5  
6  
private int combatOptionsCount = 0;
   String[] combatOptions =  new String[5];
   combatOptions[combatOptionsCount] = "Tackle it!";
   combatOptionsCount++;
   combatOptions[combatOptionsCount] = "Throw something!";
   combatOptionsCount++;


That code doesn't make any sense:
- the first line is (presumably) a class member since it's declared as private.
- the second line may or may not be a class member or a local variable, hard to tell (as previous posters says) without seeing the structure of the class.
- the following lines appear to be statements that ought to be inside a method, so it's no wonder the compiler doesn't like it.

But I'll re-iterate the point about the class, 5000 lines would have you sacked as a professional Wink, break up the code as best as possible and therefore you only have to deal with smaller chunks that are easier to maintain, clearer to understand and simpler to fix when broke.

- stride
Offline chaucer345
« Reply #5 - Posted 2013-12-28 20:30:14 »

Unfortunately the full class would be a little tricky to post as it's over 5000 lines long (a lot of string literals).

Urgh!  Break up your code into logical chunks, otherwise you'll only be facing more and more nasties like this.

Quote
Still this might help, here, starting with line 3, is the entire region eclipse underlined in red:

1  
2  
3  
4  
5  
6  
private int combatOptionsCount = 0;
   String[] combatOptions =  new String[5];
   combatOptions[combatOptionsCount] = "Tackle it!";
   combatOptionsCount++;
   combatOptions[combatOptionsCount] = "Throw something!";
   combatOptionsCount++;


That code doesn't make any sense:
- the first line is (presumably) a class member since it's declared as private.
- the second line may or may not be a class member or a local variable, hard to tell (as previous posters says) without seeing the structure of the class.
- the following lines appear to be statements that ought to be inside a method, so it's no wonder the compiler doesn't like it.

But I'll re-iterate the point about the class, 5000 lines would have you sacked as a professional Wink, break up the code as best as possible and therefore you only have to deal with smaller chunks that are easier to maintain, clearer to understand and simpler to fix when broke.

- stride


It's code for a text adventure. The problem is ascii art and String literals. When you have code like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
      // image for the Clinic
      if (room == 1) {
         newText = newText + "CLINIC\n"
               + "00000000_____000000000000000000000000000000000\n"
               + "0       DOOR            U()U       |         0\n"
               + "0                      WASH BASIN  |         0\n"
               + "0                                  |CABINET  0\n";
         if (ura == 1) {
            newText = newText
                  + "0                     (URA)        |         0\n";
         } else if (ura != 1) {
            newText = newText
                  + "0                                  |         0\n";
         }
         newText = newText
               + "0                                  ----------0\n"
               + "0                                            0\n"
               + "0                                            0\n";
         if (blueBlood == 1) {
            newText = newText
                  + "0                (BLUEBLOOD)                 0\n";
         } else {
            newText = newText
                  + "0                                            0\n";
         }
         newText = newText
               + "0                                            0\n";
         if (hardKnock == 1) {
            newText = newText
                  + "0-------        --------      (HARD KNOCK)   0\n";
         } else {
            newText = newText
                  + "0-------        --------                     0\n";
         }
         newText = newText
               + "0      |        |      |                     |\n";
         if (wind == 1) {
            newText = newText
                  + "0 YOUR | (WIND) |      |                 DOOR|\n";
         } else {
            newText = newText
                  + "0 YOUR |        |      |                 DOOR|\n";
         }
         newText = newText
               + "0 COT  |        | COT  |                     |\n"
               + "0      |        |      |                     0\n"
               + "0      |        |      |                     0\n"
               + "0000000000000000000000000000000000000000000000\n\n";
         newText = newText
               + "You find yourself in a tidy, if somewhat bare clinic.\n"
               + "It has a large medical cabinet and a wash basin with some \n"
               + "sort of drain.\n";
         if (wind == 1) {
            newText = newText
                  + "A certain yellow earth pony mare in an orange vest\n"
                  + "resides here.\n\n";
         }


or this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
case 2:
      newText = newText
            + "PEGASUS\n"
            + "..................~()~()......\n"
            + "..................(~000000............\n"
            + "..............(~0/    _    ............\n"
            + ".............(~0/     Q     __.....\n"
            + "............(~0/             _D........\n"
            + "...........(~0/             ___/.........\n"
            + "..........(~0/          __/...................\n"
            + "____________/           /.........................\n"
            + "|||||||||>>             |..........................\n"
            + "|||||||||>>             |...........................\n"
            + "|||||||||>              |..........................\n"
            + "|||||||||               |..........................\n"
            + "|||||||||               |.........................\n"
            + "|||||||||               .......................\n"
            + "|||||||||                .......................\n"
            + "|||||||||     |_____      .......................\n"
            + "--------|     |......     ......................\n"
            + "........|     |.......      .........................\n\n";
      break;


controlling your line count becomes a bit difficult. I swear I'm doing my best to break it up into methods at least, but there's only so much I can do.

The array was not declared private simply because I removed the modifier to check if that was what was causing the bug. It was a blind grab I know, but I was comparing it to some copy pasted code.

But there's something key I'm missing here, I feel like all I did there was declare a variable and modify it. What's special about arrays that stops me from doing to them the same things that I do to ints and booleans?
Offline StrideColossus
« Reply #6 - Posted 2013-12-28 20:50:41 »

Quote
...controlling your line count becomes a bit difficult. I swear I'm doing my best to break it up into methods at least, but there's only so much I can do.

Fair enough, but strongly suggest you investigate using files to store the character data and load them into classes representing the rooms, descriptions, etc. in your game.  It will certainly be a whole load easier to edit text files than Java code.

Quote
The array was not declared private simply because I removed the modifier to check if that was what was causing the bug. It was a blind grab I know, but I was comparing it to some copy pasted code.

But there's something key I'm missing here, I feel like all I did there was declare a variable and modify it. What's special about arrays that stops me from doing to them the same things that I do to ints and booleans?

See the previous couple of posts, that code needs to be inside a method.
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #7 - Posted 2013-12-28 20:58:21 »

You can't place statements inside the main class body which is what you are trying to do.

1  
2  
3  
4  
5  
// these are statements, not declarations
   combatOptions[combatOptionsCount] = "Tackle it!";
   combatOptionsCount++;
   combatOptions[combatOptionsCount] = "Throw something!";
   combatOptionsCount++;

As Riven said, use an initializer block. You can read about them here.

Why are all OpenGL tutorials written in Brainf**k?
Offline chaucer345
« Reply #8 - Posted 2013-12-28 23:04:14 »

Wow, good stuff to know. Thanks for the help!
Offline Longarmx
« Reply #9 - Posted 2013-12-28 23:06:14 »

You can also condense that statement down. (if you feel you need to)

1  
2  
combatOptions[combatOptionsCount++] = "Tackle it!";
combatOptions[combatOptionsCount++] = "Throw something!";

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Kernel


Medals: 113
Projects: 3


You think about my Avatar right now!


« Reply #10 - Posted 2013-12-29 14:13:18 »

or this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
case 2:
      newText = newText
            + "PEGASUS\n"
            + "..................~()~()......\n"
            + "..................(~000000............\n"
            + "..............(~0/    _    ............\n"
            + ".............(~0/     Q     __.....\n"
            + "............(~0/             _D........\n"
            + "...........(~0/             ___/.........\n"
            + "..........(~0/          __/...................\n"
            + "____________/           /.........................\n"
            + "|||||||||>>             |..........................\n"
            + "|||||||||>>             |...........................\n"
            + "|||||||||>              |..........................\n"
            + "|||||||||               |..........................\n"
            + "|||||||||               |.........................\n"
            + "|||||||||               .......................\n"
            + "|||||||||                .......................\n"
            + "|||||||||     |_____      .......................\n"
            + "--------|     |......     ......................\n"
            + "........|     |.......      .........................\n\n";
      break;


controlling your line count becomes a bit difficult. I swear I'm doing my best to break it up into methods at least, but there's only so much I can do.

W00t. Oh god.

You said it's ascii art, right?
It's like if game programmers would start to define each color of each texture's pixel in code. That'd be crazy.
So what do they do? They create image files. Mostly .png or a similar image saving format.

Now you have strings, not images. How'd you do that? Well, there are .txt files Wink

So let's imagine you have a method called
readTextFromFile(File)
, then you could simply do this:

1  
2  
3  
4  
5  
// somewhere in initializer:
pegasus = readTextFromFile(new File("asciiart/pegasus.txt"));
// You can now simply use that pegasus here:
newText = newText + pegasus;
// That's it.


Problem. We haven't got / implemented the method
readTextFromFile
yet. I'll give one implementation I wrote (a long time ago):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public static String readTextFromFile(File file) throws IOException {
    // opens the file and creates a readable stream of data:
    return readText(new FileInputStream(file));
}

public static String readText(InputStream stream) throws IOException {
    // Creates a Reader that can read an inputStream line by line:
    BufferedReader read = new BufferedReader(new InputStreamReader(stream));
    // Using a Stringbuilder we concatenate the individual lines
    // We could also use a String, but concatenating strings (with '+') is slower than with StringBuilders
    StringBuilder str = new StringBuilder();
    String line;
    // When the BufferedReader can't find any more lines, he returns 'null' instead of a line
    // While that's not the case we append the read line to our StringBuilder together with a line-break "\n"
    while ((line = read.readLine()) != null) {
        str.append(line + "\n");
    }
    // And in the end the together catenated lines are returned (as String, not StringBuilder!)
    return str.toString();
}


That should be it Smiley

It's not perfect, but it's an improvement!

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 138
Projects: 4
Exp: 3 years



« Reply #11 - Posted 2013-12-29 17:53:57 »

Note, you wouldn't need the \n loading from a text file.
Offline matheus23

JGO Kernel


Medals: 113
Projects: 3


You think about my Avatar right now!


« Reply #12 - Posted 2013-12-29 18:46:05 »

Note, you wouldn't need the \n loading from a text file.

You do.

I just tested it. I'm pretty sure. If you can spot a mistake, please tell me:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
package org.matheusdev;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * Author: matheusdev
 * Date: 3/10/13
 * Time: 8:52 PM
 */

public class Main {

    public static void main(String... args) {
       try {
           System.out.println(readTextFromFile(new File("testfile.txt")));
       } catch (IOException e) {
          e.printStackTrace();
       }
    }

   public static String readTextFromFile(File file) throws IOException {
      // opens the file and creates a readable stream of data:
      return readText(new FileInputStream(file));
   }

   public static String readText(InputStream stream) throws IOException {
      // Creates a Reader that can read an inputStream line by line:
      BufferedReader read = new BufferedReader(new InputStreamReader(stream));
      // Using a Stringbuilder we concatenate the individual lines
      // We could also use a String, but concatenating strings (with '+') is slower than with StringBuilders
      StringBuilder str = new StringBuilder();
      String line;
      // When the BufferedReader can't find any more lines, he returns 'null' instead of a line
      // While that's not the case we append the read line to our StringBuilder together with a line-break "\n"
      while ((line = read.readLine()) != null) {
         str.append(line + "\n");
      }
      // And in the end the together catenated lines are returned (as String, not StringBuilder!)
      return str.toString();
   }
}


The file content:
Quote
This
is
a
file
for
testing
purposes.

The output:
Quote
This
is
a
file
for
testing
purposes.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 138
Projects: 4
Exp: 3 years



« Reply #13 - Posted 2013-12-29 19:03:02 »

Oh, I meant in the file, sorry.
Offline Danny02
« Reply #14 - Posted 2013-12-29 23:50:01 »

or do =>
String text = new String(Files.readAllBytes(Paths.get("pegasus.txt")), StandardCharsets.UTF_8);
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (27 views)
2014-11-22 12:13:56

xFryIx (69 views)
2014-11-13 12:34:49

digdugdiggy (48 views)
2014-11-12 21:11:50

digdugdiggy (42 views)
2014-11-12 21:10:15

digdugdiggy (36 views)
2014-11-12 21:09:33

kovacsa (60 views)
2014-11-07 19:57:14

TehJavaDev (64 views)
2014-11-03 22:04:50

BurntPizza (62 views)
2014-11-03 18:54:52

moogie (77 views)
2014-11-03 06:22:04

CopyableCougar4 (77 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!