expected unqualified-id before 'public' - fail to see the problem

I spent around 4, maybe 5 hours trying to figure out this chunk of code from the java site yesterday. It takes a hexadecimal input and converts it into a digit. There were a lot of methods I hadn’t yet explored. But after finally wrapping my head around it (with some help from the stackoverflow community), I tried throwing it into my program and compiling. And then I ran into this error:

expected unqualified-id before ‘public’

I’ve made and called void functions before but I’ve never dealt with classes or static functions yet. Most tutorials I’ve been following just rely on coding purely within the setup or loop scopes because arduino is geared toward beginners. From the guy who answered my question on stack overflow mentioned, getDecimal(String hex) has to be declared at the beginning of the function so property String hex can be called later in the program for use.

I know it has to be something related to the syntax of the class/function in the program, since I copied the code from the java site and is 1:1 verbatum (of course, after breaking it down and analyzing it first).

Code:

public class HexToDecimalExample3{    
public static int getDecimal(String hex){   
    String digits = "0123456789ABCDEF";  
             hex = hex.toUpperCase();  
             int val = 0;  
             for (int i = 0; i < hex.length(); i++)  
             {  
                 char c = hex.charAt(i);  
                 int d = digits.indexOf(c);  
                 val = 16*val + d;  
             }  
             return val;  
            }
          }

I threw this into the program before the void setup(){}

Are you trying to add Java code to your C/C++ sketch?

That's java code. The Arduino is programmed with C++. You can't use that code as it is.

HA. Nooo. This sucks. I spent so much time trying to figure that out haha. Whenever I was looking into arduino methods online, it always showed java examples and excerpts. Crap.

aspen1135: HA. Nooo. This sucks. I spent so much time trying to figure that out haha. Whenever I was looking into arduino methods online, it always showed java examples and excerpts. Crap.

Where? I've never seen anyone pushing java code for an Arduino. You sure you weren't looking at code for the Processing language that is sometimes used on computers talking to Arduinos?