Results 1 to 9 of 9
  1. #1
    hotice1027 is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default String Conversion....

    Hello,

    I am stuck on this project that is supposed to accept a test file as an input, reverse the text (ex: Hello there!....!ereht olleH) and then output to another file. So if I run this program using the second file as an input, then text comes back to normal. There are two classes - ReverseLine and a ReverseLineTester.
    I have tried to compile it and I get the error message:

    1 error found:
    File: C:\Documents and Settings\desktop\Java Excercises\Reverse Line\ReverseLineTester.java [line: 26]
    Error: C:\Documents and Settings\desktop\Java Excercises\Reverse Line\ReverseLineTester.java:26: cannot find symbol
    symbol : method reverseLine(java.lang.String)
    location: class ReverseLineTester


    Here is what I have so far:

    Java Code:
    import java.io.FileReader;
    import java.io.PrintWriter;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    
    
    public class ReverseLineTester
    {
      
      public static void main(String[] args)
      {
        
       try
       {
         for (int i = 1; i < args.length; i++)
         {
           FileReader reader = new FileReader(args[i]);
           Scanner in = new Scanner(reader);
           PrintWriter out = new PrintWriter("File_2.txt");
           int lineNumber = 1;
           
           while (in.hasNextLine())
           {
             String line = in.nextLine();
             System.out.print(reverseLine(line));
             lineNumber++;
           }
         }
       }
       
       catch (FileNotFoundException ex)
       {
         System.out.print("File not found!");
       }
        
        
      }
      
      
      
    }

    Java Code:
    public class ReverseLine
    {
      private String line;
      
      public ReverseLine()
      {
      }
      
      
      public ReverseLine(String myline)
      {
        line = myline;
      }
      
      public String reverseLine(String str)
      {
        StringBuffer buffer = new StringBuffer(str);
        buffer = buffer.reverse();
        String reverseString = buffer.toString();
        return reverseString;
      }
      
      
    }
    Last edited by hotice1027; 11-29-2008 at 07:09 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Look at this statement,

    Java Code:
    System.out.print(reverseLine(line));
    You are referring that method in wrong way. reverseLine() method is not in the ReverseLineTester class, it's on other class. To access that you need an instance to the class.


  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Questions ...

    • What is the number of arguments that your program is receiving? Because if it's just one input file name, then nothing is going to happen:

    Java Code:
    for (int i = 1; i < args.length; i++)
    The compare condition would be 1<1 and the for loop would not execute. To avoid this, change the initial variable assignment to int i = 0.
    • Another question... what is the for loop for?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    hotice1027 is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    Oh, man, I see it now!
    Thanks!
    How is my ReverseLine class looking?

  5. #5
    hotice1027 is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    Yes, that's true. I don't think I need a loop here.
    Program supposed to run from the CLI (ex: ReverseLineTester File_1)
    So I probably should get rid of "for" line and change

    FileReader reader = new FileReader(args[i]);
    to
    FileReader reader = new FileReader(args[1]);

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default index based

    Remember, arrays are index based, which means that their first element is 0 (args is nothing but an array). Therefore if you only have one argument it should be referenced as args[0], not args[1] (which would be the second element of the array).

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    hotice1027 is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    <CODE>import java.io.FileReader;
    import java.io.PrintWriter;
    import java.io.FileNotFoundException;
    import java.util.Scanner;



    public class ReverseLineTester
    {

    public static void main(String[] args)
    {

    try
    {
    String rline = "";
    FileReader fileIn = new FileReader(args[0]);
    Scanner in = new Scanner(fileIn);
    PrintWriter fileOut = new PrintWriter("File_2.txt");
    ReverseLine rn = new ReverseLine(rline);
    int lineNumber = 1;

    while (in.hasNextLine())
    {
    String line = in.nextLine();
    rn.reverseLine(line);
    fileOut.print(line);
    lineNumber++;
    }

    }

    catch (FileNotFoundException ex)
    {
    System.out.print("File not found!");
    }


    }



    }</CODE>

    Is this the way to go?

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Does it work?

    I'm assuming that you haven't finished the program because you have a variable, lineNumber, that's defined (wrongly defined) but isn't used. So... the questions is:
    • Does the program work? ...does it do what you want it to do?

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    hotice1027 is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    Your assumptions are correct - it does not.
    It compiles, but does not create the second file with the text backwards (File_2.txt)

    I thought lineNumber is sort of a counter to go through the file line by line...

    Could you also tell me if ReverseLine class is all right?

    Thanks!

Similar Threads

  1. String to Integer Conversion in JSP
    By vinikz in forum New To Java
    Replies: 8
    Last Post: 11-10-2010, 02:45 PM
  2. [SOLVED] String conversion
    By skiing in forum New To Java
    Replies: 13
    Last Post: 11-30-2008, 03:17 AM
  3. string conversion??
    By j2vdk in forum New To Java
    Replies: 13
    Last Post: 09-19-2008, 03:35 PM
  4. bytearray to string conversion
    By mew in forum New To Java
    Replies: 1
    Last Post: 01-28-2008, 10:39 AM
  5. String to Integer conversion
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-17-2007, 03:59 PM

Posting Permissions

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