Results 1 to 6 of 6
  1. #1
    maddiexc is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Help with String arrays

    Hi.. I have to make a program which gets a phrase from the user then returns to the user an encrypted code of the phrase they entered. We were to use arrays or enumerated lists to hold the data. Im using ROT13 to encode (replaces each English letter with the one 13 places forward or back along the alphabet). I need some guidance as to what direction i should take with my code.. I keed getting errors saying "illegal start of expression" etc. Im not really sure on the proper way to use string arrays to complete this task. So some help would be appreciated!

    Here are my errors.
    60 errors found:
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 9]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:9: illegal start of expression
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 9]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:9: illegal start of expression
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 9]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:9: ';' expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 9]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:9: '(' or '[' expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 9]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:9: not a statement
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 9]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:9: ';' expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: ']' expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: illegal start of type
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: ';' expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: illegal start of type
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: ';' expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: illegal start of type
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: illegal start of type
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: illegal start of type
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: illegal start of type
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: illegal start of type
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: illegal start of type
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: illegal start of type
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: illegal start of type
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: illegal start of type
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: illegal start of type
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: illegal start of type
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: illegal start of type
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 10]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:10: <identifier> expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 12]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:12: class, interface, or enum expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 13]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:13: class, interface, or enum expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 15]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:15: class, interface, or enum expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 16]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:16: class, interface, or enum expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 18]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:18: class, interface, or enum expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 19]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:19: class, interface, or enum expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 21]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:21: class, interface, or enum expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 22]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:22: class, interface, or enum expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 24]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:24: class, interface, or enum expected
    File: F:\COMPSCI 12\J4_1_Encryption.java [line: 28]
    Error: F:\COMPSCI 12\J4_1_Encryption.java:28: class, interface, or enum expected


    Thanks
    Java Code:
    import java.io.*;
    
    public class J4_1_Encryption
    {
      public static void main (String [] args) throws IOException
      {
        BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
        
        public static String letterA [25] = new String {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
                                                      "Q","R","S","T","U","V","W","X","Y","Z"};
        public static String letterB [25] = new String {"N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
                                                      "A","B","C","D","E","F","G","H","I","J","K","L","M"};
        
        System.out.println ("Please enter a phrase: ");
        String message = myInput.readLine();
        
        int x = 0; 
        while (x <= message.length){
          x++;
          String text = message;
          String letter = Character.toString(text.charAt(x));
          
          int i = 0;
          while(letter = letterA[i]){
            i++;
            String phrase = letter + letterB[i]
          }
        }
        System.out.println (phrase);
      }
    }
    Last edited by maddiexc; 09-13-2013 at 12:10 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Help with String arrays

    It would help if you would show the code so it fits in the view. Also, don't use == to compare strings, use equals. And show all the error messages and ensure the line numbers match the given error messages.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    maddiexc is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: Help with String arrays

    It would help if you would show the code so it fits in the view. Also, don't use == to compare strings, use equals. And show all the error messages and ensure the line numbers match the given error messages.

    Regards,
    Jim
    Hi Jim, i posted the errors i was getting in java with the code. Im not really sure on how to fix them because im new to java and dont really know what they mean.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Help with String arrays

    Your array initialization is incorrect. It should be

    String [] foo = {"a","b","c"};
    or
    String [] foo = new String[] {"a","b",'c"};


    And statement 26 needs a terminating semi-colon. And you need to read up on variable scope. The variable phrase is declared within the scope of the while loop.

    Regards,
    Jim
    Last edited by jim829; 09-13-2013 at 01:09 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    maddiexc is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: Help with String arrays

    I fixed it up a bit.. and i got no errors! but when i compile it, nothing happens. It allows me to enter a phrase and this comes up after: java.lang.ArrayIndexOutOfBoundsException: 26
    at J4_1_EncryptionVer2.main(J4_1_EncryptionVer2.java: 28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)

    Heres my code i fixed up a bit.
    Java Code:
    import java.io.*;
    
    public class J4_1_EncryptionVer2
    {
      public static void main (String [] args) throws IOException
      {
        BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
        
       String letterA [] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
       String letterB [] = {"N","O","P","Q","R","S","T","U","V","W","X","Y","Z","A","B","C","D","E","F","G","H","I","J","K","L","M"};
        
        System.out.println ("Please enter a phrase: ");
        String message = myInput.readLine();
        
        int x = 0; 
        while (x <= message.length()){
          x++;
          String text = message;
          String letter = Character.toString(text.charAt(x));
          
          int i = 0;
    
         if(letter == letterA[i]){
            System.out.println (letterB[i]);
          }
          while (letter != letterA[i]){
            i++;
            if(letter == letterA[i]){
            System.out.println (letterB[i]);
          }
       
        }
      }
    }
    }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Help with String arrays

    Arrays are indexed starting at 0 so you need to figure out why 26 is out of bounds. And I told you in a previous post that you can't use regular operators like == or != to compare strings.

    You should use print statements to help debug your code. Don't rely on this forum too much or it will become too much of a crutch and you won't learn anything.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. String and char arrays
    By leeroijenkiins in forum New To Java
    Replies: 5
    Last Post: 05-08-2013, 05:01 AM
  2. string arrays and copying
    By ztomtomz in forum New To Java
    Replies: 1
    Last Post: 11-18-2012, 05:26 AM
  3. How do you use an arraylist of string arrays
    By uhertz in forum New To Java
    Replies: 1
    Last Post: 01-16-2012, 09:38 AM
  4. using selection sort on 2 string arrays
    By augiechimpo in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 04:59 AM
  5. String arrays
    By myka in forum New To Java
    Replies: 1
    Last Post: 03-18-2010, 07:49 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •