Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Help needed with code

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help needed with code

    The code is suppose to recieve a file, & replace it with another (suppose to be an app also when I recompile) But when I compile I get errors, to be honest I'm not sure if the code is correct, but here is the code & error
    import java.io.InputStream;
    import java.io.FileReader;
    import java.io.PrintWriter;
    import java.util.*;
     
    public class Replacer
    {
    	public static void main(String[] args) throws IOException
       {
     
          String origFile = args[0]; 
     
          File inputFile= new File("\AppData\Roaming\.minecraft\bin\minecraft.jar\title\csplashes.txt");
          Scanner input= new Scanner ("\AppData\Roaming\.minecraft\bin\minecraft.jar\title\comma.txt");
     
     
          while(input.hasNextLine())
              {
        	  String aLine= input.nextLine();
     
        	  aLine= new StringBuffer(aLine).change().toString();
        	  System.out.println(aLine);
              }
          input.close();
     
       }
    }


    error
    Microsoft Windows [Version 6.0.6001]
    Copyright (c) 2006 Microsoft Corporation.  All rights reserved.
     
    C:\Users\Owner>javac C:\Users\Owner\Desktop\Replacer.java
    C:\Users\Owner\Desktop\Replacer.java:13: illegal escape character
          File inputFile= new File("\AppData\Roaming\.minecraft\bin\minecraft.jar\ti
    tle\csplashes.txt");
                                     ^
    C:\Users\Owner\Desktop\Replacer.java:13: illegal escape character
          File inputFile= new File("\AppData\Roaming\.minecraft\bin\minecraft.jar\ti
    tle\csplashes.txt");
                                             ^
    C:\Users\Owner\Desktop\Replacer.java:13: illegal escape character
          File inputFile= new File("\AppData\Roaming\.minecraft\bin\minecraft.jar\ti
    tle\csplashes.txt");
                                                     ^
    C:\Users\Owner\Desktop\Replacer.java:13: illegal escape character
          File inputFile= new File("\AppData\Roaming\.minecraft\bin\minecraft.jar\ti
    tle\csplashes.txt");
                                                                    ^
    C:\Users\Owner\Desktop\Replacer.java:13: illegal escape character
          File inputFile= new File("\AppData\Roaming\.minecraft\bin\minecraft.jar\ti
    tle\csplashes.txt");
     
        ^
    C:\Users\Owner\Desktop\Replacer.java:14: illegal escape character
          Scanner input= new Scanner ("\AppData\Roaming\.minecraft\bin\minecraft.jar
    \title\comma.txt");
                                        ^
    C:\Users\Owner\Desktop\Replacer.java:14: illegal escape character
          Scanner input= new Scanner ("\AppData\Roaming\.minecraft\bin\minecraft.jar
    \title\comma.txt");
                                                ^
    C:\Users\Owner\Desktop\Replacer.java:14: illegal escape character
          Scanner input= new Scanner ("\AppData\Roaming\.minecraft\bin\minecraft.jar
    \title\comma.txt");
                                                        ^
    C:\Users\Owner\Desktop\Replacer.java:14: illegal escape character
          Scanner input= new Scanner ("\AppData\Roaming\.minecraft\bin\minecraft.jar
    \title\comma.txt");
                                                                       ^
    C:\Users\Owner\Desktop\Replacer.java:14: illegal escape character
          Scanner input= new Scanner ("\AppData\Roaming\.minecraft\bin\minecraft.jar
    \title\comma.txt");
     
           ^
    10 errors
     
    C:\Users\Owner>


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help needed with code

    illegal escape character
    the backslash \ (called the escape character) is a special character for the compiler. It allows you to tell the compiler to ignore the ordinary meaning of the character that follows it. For example if you want a " in a String you precede it with the \ : "A dbl quote: \" "
    To use the \ in a String you must "escape" by preceding it with another \ for example \\
    The first \ tells the compiler to take the second \ as an ordinary character

    There are only a few characters that can be "escaped": n r " ' \ etc

  3. The Following User Says Thank You to Norm For This Useful Post:

    mmkmmk3 (July 29th, 2011)

  4. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Help needed with code

    The 'backslash' or 'escape' character is used in Java Strings to include a character that might otherwise have some special meaning. For example if you wanted a String that was just a single quote character you would code "\"" . See the bit on 'escape characters' at Characters (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

    The compiler is complaining that \A is not on its list of known escapes. You could try doubling-up those backslashes in your Windows-style file paths, reading them from a Preferences object (so they're stored un-escaped elsewhere) or you could build your File objects a directory at a time using File root = new File(/* appropriate root - see File.listRoots() */; File fAppDir = new File(root, "AppData");

    For portable Java code, try to avoid hard-coding any OS-specific Strings (like Windows file paths) in your apps.

  5. The Following User Says Thank You to Sean4u For This Useful Post:

    mmkmmk3 (July 29th, 2011)

  6. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with code

    Quote Originally Posted by Norm View Post
    the backslash \ (called the escape character) is a special character for the compiler. It allows you to tell the compiler to ignore the ordinary meaning of the character that follows it. For example if you want a " in a String you precede it with the \ : "A dbl quote: \" "
    To use the \ in a String you must "escape" by preceding it with another \ for example \\
    The first \ tells the compiler to take the second \ as an ordinary character

    There are only a few characters that can be "escaped": n r " ' \ etc
    Quote Originally Posted by Sean4u View Post
    The 'backslash' or 'escape' character is used in Java Strings to include a character that might otherwise have some special meaning. For example if you wanted a String that was just a single quote character you would code "\"" . See the bit on 'escape characters' at Characters (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

    The compiler is complaining that \A is not on its list of known escapes. You could try doubling-up those backslashes in your Windows-style file paths, reading them from a Preferences object (so they're stored un-escaped elsewhere) or you could build your File objects a directory at a time using File root = new File(/* appropriate root - see File.listRoots() */; File fAppDir = new File(root, "AppData");


    Thanks guys, never knew that ( I guess thats what happens when your new (going to take java next year at com. college)) It got rid of those errors, but came back with these

    Microsoft Windows [Version 6.0.6001]
    Copyright (c) 2006 Microsoft Corporation.  All rights reserved.
     
    C:\Users\Owner>javac C:\Users\Owner\Desktop\Replacer.java
    C:\Users\Owner\Desktop\Replacer.java:8: cannot find symbol
    symbol  : class IOException
    location: class Replacer
            public static void main(String[] args) throws IOException
                                                          ^
    C:\Users\Owner\Desktop\Replacer.java:13: cannot find symbol
    symbol  : class File
    location: class Replacer
          File inputFile= new File("\\AppData\\Roaming\\.minecraft\\bin\\minecraft.j
    ar\\title\\splashes.txt");
          ^
    C:\Users\Owner\Desktop\Replacer.java:13: cannot find symbol
    symbol  : class File
    location: class Replacer
          File inputFile= new File("\\AppData\\Roaming\\.minecraft\\bin\\minecraft.j
    ar\\title\\splashes.txt");
                              ^
    C:\Users\Owner\Desktop\Replacer.java:21: cannot find symbol
    symbol  : method change()
    location: class java.lang.StringBuffer
              aLine= new StringBuffer(aLine).change().toString();
                                            ^
    4 errors
     
    C:\Users\Owner>

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help needed with code

    cannot find symbol
    symbol : class IOException
    You need to add an import statement for the IOException class and the File class

    cannot find symbol
    symbol : method change()
    Where is the method: change() defined? What class is it in? The compiler can't find it.

  8. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with code

    well my code is at my first post, method change() is line 20
     aLine= new StringBuffer(aLine).change().toString();
    Its in the same class... It might be a bad rootword I'm actually not entirely sure that should be there. I wanted to change so I put that, I know java has a keyword of Exchanger, but I was unsure what it exactly did, so yea.

    The first error that your talking about (symbol: Class IOException) I'm not entirely sure what you mean, can you explain some more, like I said I'm quite new to the language of Java.

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help needed with code

    Look at the top of your source file. There are import statements there. The import statement is used by the compiler to find class definitions for classes that are in a package. You need to add an import statement for the package that contains the IOException class. Read the API doc for the IOException class to see what package it is in.

    new StringBuffer(aLine).change().toString();
    Why are you coding the call to the change() method here?

    Its in the same class...
    What class is the change() method in?


    Here is the link to the API docs:Java Platform SE 6

  10. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with code

    Quote Originally Posted by Norm View Post
    Look at the top of your source file. There are import statements there. The import statement is used by the compiler to find class definitions for classes that are in a package. You need to add an import statement for the package that contains the IOException class. Read the API doc for the IOException class to see what package it is in.

    new StringBuffer(aLine).change().toString();
    Why are you coding the call to the change() method here?


    What class is the change() method in?


    Here is the link to the API docs:Java Platform SE 6

    Oh I see what you mean, It might not be in any code. I was just looking through other java code to see how everything worked, & thought it might work. I'm just trying to make something that replaces one file with one you create in the applet by typing in a new line.

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help needed with code

    Is your program working now?

  12. #10
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with code

    Quote Originally Posted by Norm View Post
    Is your program working now?
    No, I'm still not sure whats wrong, or what to replace

  13. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help needed with code

    Post your current error messages here.

    Have you removed the line of code with the change() method call in it?
    If not, why do you have that line of code in your program?

  14. #12
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with code

    Quote Originally Posted by Norm View Post
    Post your current error messages here.

    Have you removed the line of code with the change() method call in it?
    If not, why do you have that line of code in your program?

    Well I haven't changed anything since last error... the reason that line is their is because I was looking at another person's code & saw what it did, so I copied that line but changed the rootword

  15. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help needed with code

    If you remove that line, what happens?

  16. #14
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with code

    Quote Originally Posted by Norm View Post
    If you remove that line, what happens?
    Microsoft Windows [Version 6.0.6001]
    Copyright (c) 2006 Microsoft Corporation.  All rights reserved.
     
    C:\Users\Owner>javac C:\Users\Owner\Desktop\BATCH FILES\Replacer.java
    javac: invalid flag: C:\Users\Owner\Desktop\BATCH
    Usage: javac <options> <source files>
    use -help for a list of possible options
     
    C:\Users\Owner>javac C:\Users\Owner\Desktop\Replacer.java
    C:\Users\Owner\Desktop\Replacer.java:8: cannot find symbol
    symbol  : class IOException
    location: class Replacer
            public static void main(String[] args) throws IOException
                                                          ^
    C:\Users\Owner\Desktop\Replacer.java:13: cannot find symbol
    symbol  : class File
    location: class Replacer
          File inputFile= new File("\\AppData\\Roaming\\.minecraft\\bin\\minecraft.j
    ar\\title\\splashes.txt");
          ^
    C:\Users\Owner\Desktop\Replacer.java:13: cannot find symbol
    symbol  : class File
    location: class Replacer
          File inputFile= new File("\\AppData\\Roaming\\.minecraft\\bin\\minecraft.j
    ar\\title\\splashes.txt");
                              ^
    3 errors
     
    C:\Users\Owner>
    this

  17. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help needed with code

    Well I haven't changed anything
    Have you read posts#5 & #7?
    I guess there is nothing I can do if you don't want to make the recommended changes.

  18. #16
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with code

    Quote Originally Posted by Norm View Post
    Have you read posts#5 & #7?
    I guess there is nothing I can do if you don't want to make the recommended changes.
    yea, but I'm not entirly sure what class I should add

  19. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help needed with code

    Read the API doc, find the class definitions that the compiler can not find. Get the package that those classes are in (its near the very top of the page for the class's doc, above the Class.... and below the horizontal line) and add an import statement for that package to your code.
    For example, you code has this import statement:
    import java.io.InputStream;

    go read the API doc for the InputStream class and see where the package (java.io) is defined for that class. Then look at the API doc for the classes the compiler can't find and get the package there.

  20. #18
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with code

    Quote Originally Posted by Norm View Post
    Read the API doc, find the class definitions that the compiler can not find. Get the package that those classes are in (its near the very top of the page for the class's doc, above the Class.... and below the horizontal line) and add an import statement for that package to your code.
    For example, you code has this import statement:
    import java.io.InputStream;

    go read the API doc for the InputStream class and see where the package (java.io) is defined for that class. Then look at the API doc for the classes the compiler can't find and get the package there.
    I spent about two hours looking through it with no class that could change one file to another, should I keep looking?

  21. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help needed with code

    class that could change one file to another
    What does that have to do with solving the compiler errors?

    What do you mean by "change one file to another"?
    Do you mean rename an existing file? See the File class.
    Do you mean change the contents of a file? There are several classes you can use to write out to a file.

  22. #20
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with code

    Quote Originally Posted by Norm View Post
    What does that have to do with solving the compiler errors?

    What do you mean by "change one file to another"?
    Do you mean rename an existing file? See the File class.
    Do you mean change the contents of a file? There are several classes you can use to write out to a file.
    well thats why .change is on that line, it used to have .reverse, because I retrieved it from another code, after I got aggravated with mine... yes I'm trying to replace one file with another

Similar Threads

  1. Help needed with calculator code
    By andys in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 21st, 2011, 08:02 PM
  2. making my code a little better, if needed.
    By vendetta in forum Object Oriented Programming
    Replies: 4
    Last Post: February 11th, 2010, 03:40 AM
  3. code needed for the following problem
    By romilc in forum Java Theory & Questions
    Replies: 1
    Last Post: October 11th, 2009, 10:05 AM
  4. Urgent code needed
    By subhvi in forum AWT / Java Swing
    Replies: 4
    Last Post: August 27th, 2009, 12:55 AM
  5. HELP "code needed"
    By Dave in forum File I/O & Other I/O Streams
    Replies: 14
    Last Post: August 26th, 2009, 11:06 AM