I've been working on this program for a homework assignment in my Intro to Java course. It is supposed to take input from the user and then decode or encode it, depending on what the user typed into the console. I have everything working, the program takes in input fine and then correctly decides if it needs to be encoded or decoded. However, I now need to rewrite my encode method (in my code class) to actually do some encoding, instead of just outputting "Encode called." I cannot get this to work, I've tried writing something like this (I realize that this is not even close to right yet, I will also need an array):
public static String encode (String message)
{
int n;
for(n = 0; n <= s.length(); n++)
{
s1 = s.substring(n);
if(Code.CODE1.indexOf(s1) == 1)
return true;
But I always get the error "Can't find symbol" in regards to the variable s in s.length. I thought static methods could call variables from other static methods. Am I misreading something, or is just an illegal way to call a variable in the first place?
Any help would be greatly appreciated, on how to deal with this problem and even how to go about using an array to store the results of the encoding. Any tips would be great! Here's the rest of my code:
import java.util.Scanner;
public class Enc1
{
public static void main (String []args)
{
//String message = args[0];
//String codedMessage = args[1];
boolean valid = false;
int i;
int j;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a code");
String code = keyboard.nextLine();
System.out.println(code);
if (isCoded(code))
System.out.println(decode(code));
else
System.out.println(encode(code));
}
public class Code{
private static final String CODE1 = "HTNZUL5m3lDQchenpIuORws1jPgVtzKWbfBxXSArdayCJkvqGiF2YoM4E";
public static final String CODE2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ,.;:";
}
public static String encode (String message)
{
message = "Encode called";
return message;
//CODE1.charAt(0); //0 will equal H
//CODE1.indexOf(CODE1, 0); //Not sure what it returns
}
public static String decode (String codedMessage)
{
codedMessage = "Decode called";
return codedMessage;
}
public static boolean isCoded(String s){
int n;
int m;
boolean valid = true;
String s1;
for(n = 0; n <= s.length(); n++)
{
s1 = s.substring(n);
if(Code.CODE1.indexOf(s1) == 1)
return false;
}
for(m = 5; m <= s.length()-1; m = m+6)
{
if(s.charAt(m) == ' ')
valid = true;
else
return false;
}
return valid;
}
}
Edited by: illold3chordme on Oct 29, 2007 10:17 AM