10 Replies - 10410 Views - Last Post: 20 February 2008 - 08:57 PM Rate Topic: -----

#1 scholio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 11-February 08

illegal start of expression - help with calling a method

Post icon  Posted 17 February 2008 - 08:52 PM

public class Encryptocoder 
{
		public static void main (String[]args);
		{
		String code = "WHERE IS THE LOVE?";
		int key = 5;
		int len = code.length();
		int i;
		
		result += Encrypt (code); //call encrypt method
		
		result2 += Decrypt (output); //call decrypt method
		
		String output = "";
		output = output + (char)((int)code.charAt(0)+ key);
		System.out.println ("You entered this String: " + code);
		System.out.println ("The length of the String is:" + len);
		System.out.println ("The encryption key is : " + key);
		}
		
{
private static String Encrypt(); //encryption method

   {
		for (i = 0; i < len; i++)
	  {
		int current = (int)code.charAt(0);
		int difference = current - (int)'A';
		int resultingChar = (distFromA + key)%26;
		output = output + (char)(resultingChar + (int)'A');
	  }
	System.out.println("This is the String encrypted: " + output);

	}
	}
{	
private static String Decrypt(); //decryption method
	{

	String code = "" + output;
	int key = 5;
	int len = code.length();
	int i;
	
	output = output + (char)((int)code.charAt(0)- key);
	System.out.println ("The encrypted String is" + output);
	System.out.println ("The length of the String is:" + len);
	
	{
	for (i = 0; i < len; i++)
	  {
		int current = (int)code.charAt(0);
		int difference = current + (int)'A';
		int resultingChar = (distFromA - key)%26;
		decrypt = decrypt + (char)(resultingChar + (int)'A');
	  }
	  System.out.println("The decrypted String is: " + decrypt);
	}
	}	
}	
}




hi all

i am working an a encode/decode program. the way it encodes is that a key number is chosen in my case 5, if the key was then the first letter of the string/message then has 3 added to it, so if 'a' was the first letter, after encryption it would be a 'd'

anyways, in my program i have a class and an encrypt method and a decrypt method. the encrypt method basically adds the key number to the message, and the decrypt method subtracts it.

i think i have it done right, but am not too sure about how i went about placing the methods inside the class. i am having a compile error stating "illegal start of expression" it could be due to {brackets} or something else. have i correctly called my methods in the program?

any help appreciated

cheers

Is This A Good Question/Topic? 0
  • +

Replies To: illegal start of expression - help with calling a method

#2 dontKnowJava  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 221
  • Joined: 29-September 07

Re: illegal start of expression - help with calling a method

Posted 17 February 2008 - 09:00 PM

get rid of semicolon after your main public static void main (String[]args);<--
there might be other errors thats just the first thing i saw

This post has been edited by dontKnowJava: 17 February 2008 - 09:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 scholio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 11-February 08

Re: illegal start of expression - help with calling a method

Posted 18 February 2008 - 05:32 PM

dontknowjava, thanks i corrected the semicolon error you noted but my program still does not compile. initially, the semicolon error did not come up when i compiled it. it still just states an "illegal start of expression"

ideas appreciated,

cheers
Was This Post Helpful? 0
  • +
  • -

#4 dontKnowJava  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 221
  • Joined: 29-September 07

Re: illegal start of expression - help with calling a method

Posted 18 February 2008 - 07:28 PM

there are a lot of random brackets like these
{<--
private static String Encrypt(); //encryption method
{<--
private static String Decrypt(); //decryption method
and same thing on the closing side also some of your variables are defined as local like i. in your Encrypt method you use an i but dont declare it
Was This Post Helpful? 0
  • +
  • -

#5 scholio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 11-February 08

Re: illegal start of expression - help with calling a method

Posted 18 February 2008 - 09:40 PM

i removed some of the extra brackets, how do i know how many i should have? do i need them to separate methods?

as for declaring i in the methods, do i just put an int in front of i in the for loops? do i also need to declare "len" in the for loops?

i tried it and i still got compilation errors ("illegal start of type" jgrasp). as well as another error pertaining to a println statement (<identifier> expected) for "System.out.println("This is the String encrypted: " + output);"

here is my code now, i am only getting two compilation errors, need help declaring i in the for loops

public class Encryptocoder  
{
		public static void main (String[]args)
		{
		String code = "WHERE IS THE LOVE?";
		int key = 5;
		int len = code.length();
		int i;
		
		result += Encrypt (code); //call encrypt method
		
		result2 += Decrypt (output); //call decrypt method
		
		String output = "";
		output = output + (char)((int)code.charAt(0)+ key);
		System.out.println ("You entered this String: " + code);
		System.out.println ("The length of the String is:" + len);
		System.out.println ("The encryption key is : " + key);
		}
		
private static String Encrypt(); //encryption method

		for (i = 0; i < len; i++)
	  {
		int current = (int)code.charAt(0);
		int difference = current - (int)'A';
		int resultingChar = (distFromA + key)%26;
		output = output + (char)(resultingChar + (int)'A');
	  }
	System.out.println("This is the String encrypted: " + output);

		
private static String Decrypt(); //decryption method
	{

	String code = "" + output;
	int key = 5;
	int len = code.length();
	int i;
	
	output = output + (char)((int)code.charAt(0)- key);
	System.out.println ("The encrypted String is" + output);
	System.out.println ("The length of the String is:" + len);
	
	{
	for (i = 0; i < len; i++)
	  {
		int current = (int)code.charAt(0);
		int difference = current + (int)'A';
		int resultingChar = (distFromA - key)%26;
		decrypt = decrypt + (char)(resultingChar + (int)'A');
	  }
	  System.out.println("The decrypted String is: " + decrypt);
	}
	}
}	


Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: illegal start of expression - help with calling a method

Posted 18 February 2008 - 10:17 PM

For declaring i for your for loops, use this for (int i = 0; i < len; i++). You also have code outside your methods, such as this one


private static String Encrypt(); //encryption method

		for (i = 0; i < len; i++)
	  {
		int current = (int)code.charAt(0);
		int difference = current - (int)'A';
		int resultingChar = (distFromA + key)%26;
		output = output + (char)(resultingChar + (int)'A');
	  }
	System.out.println("This is the String encrypted: " + output);




should be

private static String Encrypt(); //encryption method
{
		for (i = 0; i < len; i++)
	  {
		int current = (int)code.charAt(0);
		int difference = current - (int)'A';
		int resultingChar = (distFromA + key)%26;
		output = output + (char)(resultingChar + (int)'A');
	  }
	System.out.println("This is the String encrypted: " + output);
}




Your decrypt method, which currently looks like this:


private static String Decrypt(); //decryption method
	{

	String code = "" + output;
	int key = 5;
	int len = code.length();
	int i;
	
	output = output + (char)((int)code.charAt(0)- key);
	System.out.println ("The encrypted String is" + output);
	System.out.println ("The length of the String is:" + len);
	
	{
	for (i = 0; i < len; i++)
	  {
		int current = (int)code.charAt(0);
		int difference = current + (int)'A';
		int resultingChar = (distFromA - key)%26;
		decrypt = decrypt + (char)(resultingChar + (int)'A');
	  }
	  System.out.println("The decrypted String is: " + decrypt);
	}




Should look like this:


private static String Decrypt(); //decryption method
{

	String code = "" + output;
	int key = 5;
	int len = code.length();
	int i;
	
	output = output + (char)((int)code.charAt(0)- key);
	System.out.println ("The encrypted String is" + output);
	System.out.println ("The length of the String is:" + len);
	
	
	for (i = 0; i < len; i++)
	  {
		int current = (int)code.charAt(0);
		int difference = current + (int)'A';
		int resultingChar = (distFromA - key)%26;
		decrypt = decrypt + (char)(resultingChar + (int)'A');
	  }
	  System.out.println("The decrypted String is: " + decrypt);
}




Hope that helps :)

This post has been edited by PsychoCoder: 18 February 2008 - 10:17 PM

Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: illegal start of expression - help with calling a method

Posted 19 February 2008 - 12:21 AM

You also need to make sure you remove all semi-colons from your method declarations. Semi-colons should only be on the end of a statement.

These lines of code:
private static String Encrypt(); //encryption method
private static String Decrypt(); //decryption method


Should be like this:
private static String Encrypt() //encryption method
private static String Decrypt() //decryption method

Was This Post Helpful? 0
  • +
  • -

#8 scholio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 11-February 08

Re: illegal start of expression - help with calling a method

Posted 19 February 2008 - 07:33 PM

thanks psychocoder and jayman9. i did what you suggested but still yielded compilation errors, more so actually. a quick question why do i need to declare the variable 'i' for the for loop when i have already declared it outside of the methods?

after doing what ya'll suggested i kinda concluded that maybe i needed to declare the variable distFromA as well as key in this portion of my code, both encrypt and decrypt methods. question is, i have already declared and set a value for key outside the for loop? do i still have to put an int in front of key?

 for (int i = 0; i < len; i++)
	  {
		int current = (int)code.charAt(0);
		int difference = current + (int)'A';
		int resultingChar = ( int distFromA - int key)%26;
		decrypt = decrypt + (char)(resultingChar + (int)'A');
	  }
	  System.out.println("The decrypted String is: " + decrypt);



and if i add parentheses around int in in front of distFromA and key to get code like this
 for (int i = 0; i < len; i++)
	  {
		int current = (int)code.charAt(0);
		int difference = current - (int)'A';
		int resultingChar = ((int) distFromA + (int) key)%26;
		output = output + (char)(resultingChar + (int)'A');



when put int in front of distFromA I was still getting compilation errors. the errors ask for a ')' around int, but after i enter the parentheses i get another error pertaining to that line of code about an illegal start of expression

also a slew of other compilation errors saying "Encrypt() in Encryptocoder cannot be applied to (java.lang.String)" as well as "cannot find symbol" for output, distFromA, len, decrypt which have either been declared outside the loops or i have just declared, as in distFromA

it seems that if i change or add something it triggers something else such as

this is my current code now

public class Encryptocoder  
{
		public static void main (String[]args)
		{
		String code = "WHERE IS THE LOVE?";
		int key = 5;
		int len = code.length();
		int i;
		
		result += Encrypt (code); //call encrypt method
		
		result2 += Decrypt (output); //call decrypt method
		
		String output = "";
		output = output + (char)((int)code.charAt(0)+ key);
		System.out.println ("You entered this String: " + code);
		System.out.println ("The length of the String is:" + len);
		System.out.println ("The encryption key is : " + key);
		}
		
private static String Encrypt() //encryption method
{
		for (int i = 0; i < len; i++)
	  {
		int current = (int)code.charAt(0);
		int difference = current - (int)'A';
		int resultingChar = ((int) distFromA + (int) key)%26;
		output = output + (char)(resultingChar + (int)'A');
	  }
	System.out.println("This is the String encrypted: " + output);
}		

private static String Decrypt() //decryption method
{

	String code = "" + output;
	int key = 5;
	int len = code.length();
	int i;
	
	output = output + (char)((int)code.charAt(0)- key);
	System.out.println ("The encrypted String is" + output);
	System.out.println ("The length of the String is:" + len);
	
	
	for (int i = 0; i < len; i++)
	  {
		int current = (int)code.charAt(0);
		int difference = current + (int)'A';
		int resultingChar = ((int) distFromA - (int) key)%26;
		decrypt = decrypt + (char)(resultingChar + (int)'A');
	  }
	  System.out.println("The decrypted String is: " + decrypt);
}
}




this code is becoming pretty frustrating am i really doing something that fatally wrong?

i have made a class, and two methods, called the methods but to no avail, have i correctly called the methods to begin with?
Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: illegal start of expression - help with calling a method

Posted 19 February 2008 - 07:51 PM

Why don't you copy and paste the exact error messages that you are getting. This will make it easier for us to help you trouble shoot the problems.
Was This Post Helpful? 0
  • +
  • -

#10 scholio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 11-February 08

Re: illegal start of expression - help with calling a method

Posted 19 February 2008 - 09:28 PM

will do, my latest version of my code
public class Encryptocoder  
{
		public static void main (String[]args)
		{
		String code = "WHERE IS THE LOVE?";
		int key = 5;
		int len = code.length();
		int i;
		
		result += Encrypt (code); //call encrypt method
		
		result2 += Decrypt (output); //call decrypt method
		
		String output = "";
		output = output + (char)((int)code.charAt(0)+ key);
		System.out.println ("You entered this String: " + code);
		System.out.println ("The length of the String is:" + len);
		System.out.println ("The encryption key is : " + key);
		}
		
private static String Encrypt() //encryption method
{
		for (int i = 0; i < len; i++)
	  {
		int current = (int)code.charAt(0);
		int difference = current - (int)'A';
		int resultingChar = ((int) distFromA + (int) key)%26;
		output = output + (char)(resultingChar + (int)'A');
	  }
	System.out.println("This is the String encrypted: " + output);
}		

private static String Decrypt() //decryption method
{

	String code = "" + output;
	int key = 5;
	int len = code.length();
	int i;
	
	output = output + (char)((int)code.charAt(0)- key);
	System.out.println ("The encrypted String is" + output);
	System.out.println ("The length of the String is:" + len);
	
	
	for (int i = 0; i < len; i++)
	  {
		int current = (int)code.charAt(0);
		int difference = current + (int)'A';
		int resultingChar = ((int) distFromA - (int) key)%26;
		decrypt = decrypt + (char)(resultingChar + (int)'A');
	  }
	  System.out.println("The decrypted String is: " + decrypt);
}
}




*****the errors during attempted compilation using jgrasp*******

Encryptocoder.java:18: cannot find symbol
symbol  : variable result
location: class Encryptocoder
		result += Encrypt (code); //call encrypt method
				^
Encryptocoder.java:18: Encrypt() in Encryptocoder cannot be applied to (java.lang.String)
		result += Encrypt (code); //call encrypt method
						  ^
Encryptocoder.java:20: cannot find symbol
symbol  : variable result2
location: class Encryptocoder
		result2 += Decrypt (output); //call decrypt method
				^
Encryptocoder.java:20: cannot find symbol
symbol  : variable output
location: class Encryptocoder
		result2 += Decrypt (output); //call decrypt method
									^
Encryptocoder.java:31: cannot find symbol
symbol  : variable len
location: class Encryptocoder
		for (int i = 0; i < len; i++)
							^
Encryptocoder.java:33: cannot find symbol
symbol  : variable code
location: class Encryptocoder
		int current = (int)code.charAt(0);
						   ^
Encryptocoder.java:35: cannot find symbol
symbol  : variable distFromA
location: class Encryptocoder
		int resultingChar = ((int) distFromA + (int) key)%26;
								   ^
Encryptocoder.java:35: cannot find symbol
symbol  : variable key
location: class Encryptocoder
		int resultingChar = ((int) distFromA + (int) key)%26;
													 ^
Encryptocoder.java:36: cannot find symbol
symbol  : variable output
location: class Encryptocoder
		output = output + (char)(resultingChar + (int)'A');
		^
Encryptocoder.java:36: cannot find symbol
symbol  : variable output
location: class Encryptocoder
		output = output + (char)(resultingChar + (int)'A');
				 ^
Encryptocoder.java:38: cannot find symbol
symbol  : variable output
location: class Encryptocoder
	System.out.println("This is the String encrypted: " + output);
														  ^
Encryptocoder.java:44: cannot find symbol
symbol  : variable output
location: class Encryptocoder
	String code = "" + output;
					   ^
Encryptocoder.java:49: cannot find symbol
symbol  : variable output
location: class Encryptocoder
	output = output + (char)((int)code.charAt(0)- key);
	^
Encryptocoder.java:49: cannot find symbol
symbol  : variable output
location: class Encryptocoder
	output = output + (char)((int)code.charAt(0)- key);
			 ^
Encryptocoder.java:50: cannot find symbol
symbol  : variable output
location: class Encryptocoder
	System.out.println ("The encrypted String is" + output);
													^
Encryptocoder.java:54: i is already defined in Decrypt()
	for (int i = 0; i < len; i++)
			 ^
Encryptocoder.java:58: cannot find symbol
symbol  : variable distFromA
location: class Encryptocoder
		int resultingChar = ((int) distFromA - (int) key)%26;
								   ^
Encryptocoder.java:59: cannot find symbol
symbol  : variable decrypt
location: class Encryptocoder
		decrypt = decrypt + (char)(resultingChar + (int)'A');
		^
Encryptocoder.java:59: cannot find symbol
symbol  : variable decrypt
location: class Encryptocoder
		decrypt = decrypt + (char)(resultingChar + (int)'A');
				  ^
Encryptocoder.java:61: cannot find symbol
symbol  : variable decrypt
location: class Encryptocoder
	  System.out.println("The decrypted String is: " + decrypt);
													   ^
20 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
 

Was This Post Helpful? 0
  • +
  • -

#11 scholio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 11-February 08

Re: illegal start of expression - help with calling a method

Posted 20 February 2008 - 08:57 PM

i whipped out my java textbook and tried somethings, this is what my code tentatively looks like, it still doesn't compile but now i only have two errors instead of twenty

for some reason, i feel the things that i have put in just don't feel right, is it? am i moving closer

when i commented out the methods, it compiled fine, so something about calling the method is wrong

***jgrasp errors***

 
Encryptocoder.java:18: ';' expected
		result = String Encrypt(code); //call encrypt method
								^
Encryptocoder.java:20: ';' expected
		result2 = String Decrypt(output); //call decrypt method
								 ^



*************************
 
public class Encryptocoder  
{
		public static void main (String[]args)
		{
		String code = "WHERE IS THE LOVE?";
		int key = 5;
		int len = code.length();
		int i;
		
		result = String Encrypt(code); //call encrypt method
		
		result2 = String Decrypt(output); //call decrypt method
		
		String output = "";
		output = output + (char)((int)code.charAt(0)+ key);
		System.out.println ("You entered this String: " + code);
		System.out.println ("The length of the String is:" + len);
		System.out.println ("The encryption key is : " + key);
		}
		
public void Encrypt(char output) //encryption method
{
		int key = 5;
		int len = code.length();
		int i;

		for (i = 0; i < len; i++)
	  {
		int current = (int)code.charAt(0);
		int difference = current - (int)'A';
		int resultingChar = ((int) distFromA + (int) key)%26;
		output = output + (char)(resultingChar + (int)'A');
	  }
	System.out.println("This is the String encrypted: " + output);
}		

public void Decrypt(char decrypt) //decryption method
{

	String code = "" + output;
	int key = 5;
	int len = code.length();
	int i;
	
	output = output + (char)((int)code.charAt(0)- key);
	System.out.println ("The encrypted String is" + output);
	System.out.println ("The length of the String is:" + len);
	
	
	for (int i = 0; i < len; i++)
	  {
		int current = (int)code.charAt(0);
		int difference = current + (int)'A';
		int resultingChar = ((int) distFromA - (int) key)%26;
		decrypt = decrypt + (char)(resultingChar + (int)'A');
	  }
	  System.out.println("The decrypted String is: " + decrypt);
}
}



what is the best way for me to call methods for my situation?

is this version any closer to working and compiling correctly than my previous code attempt?

This post has been edited by scholio: 20 February 2008 - 08:58 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1