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 2 of 2

Thread: making text editor using string class

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default making text editor using string class

    no arrays.
    hi guys. this is my code. I've been changing it back and forth trying various things but for the most part, this is the general skeleton. really frustrating, have spent about 30 hours on this and still can't get it so it is extremely discouraging. all help will be greatly appreciated.
    my question isn't necessarily how to fix the error but to see if my approach makes sense. if what I've done thus far can actually be completed or if my code has to be rewritten or just slightly modified to work. I have scrapped the whole thing a total of 3 times and started from scratch and now I am here.
    import java.util.*;
     
    class WhiteSpace
    {
      public static void main(String[] args)
      {
        Scanner scan = new Scanner(System.in);
        String text;                          //variable for user's input
        String cTrim;
     
        System.out.println("enter desired width");
        int width = scan.nextInt();               //desired width input
     
        System.out.println("enter desired phrase");
        scan.nextLine();
        String keyboard = scan.nextLine();        //takes a line 2b formatted
     
        cTrim = keyboard.replaceAll("^\\s+", "");  //compresses whitespace
        text = cTrim.replaceAll("\\s+", " ");
        System.out.println(text.length());
     
        int i = 0;
        int lastspace = -1;
        int linestart = 0;
        int lastprinted = 0;
        int k = 0;
     
        // hello what are you d
        // oing
        // lastspace = 5, 10, 14, 18
        // at i = 20, lastspace = 18
     
        if (text.length() < width)
          System.out.println(text);
        else
        {
          while (i < text.length())
          {
            while (i < linestart + width + 1) // while i < 21
            {
              if (text.charAt(i) == ' ') lastspace = i; //lastspace is 18
              if (i > linestart + width - 1) //i is 20 if bigger than 19
              {
                if (lastspace != -1) // 18
                {
                  for (i = lastprinted; i < lastspace; i++) // i = 0; < 18
                  {
                    System.out.print(text.charAt(i)); // 0 - 17 hello how are you
                    k++;
                  }
                  if (i == lastspace) // i = 17
                    System.out.println();
                  i = lastprinted + k; // i would be 17 + 0
                }
              }
            i++;
            }
          linestart = lastspace + 1; //linestart is now 19
          lastspace = -1;  //lastspace no longer 18
          lastprinted = i + 1; //last printed is 18
          }
        }
      }
    }

    I've pretty much been testing it with a width of 20 and the input of "hello how are you doing".
    my desired output is as follows:
    hello how are you
    doing
    so my comments are just for me to keep track of where i currently is to be accurate.

    I'm trying to do left justification. my idea here is that I detect the last space in a line and println() at that spot so that the next printed char is in a new line. then I want to detect the last space in that 2nd line and so forth and so forth. my problem is figuring out how I can print all the characters while not screwing up the incrementing i that has to continue to discover the last space in a line. hope that makes sense. all help will be greatly appreciated. thanks in advance.
    Last edited by petadeer; February 13th, 2011 at 09:40 AM.


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: making text editor using string class

    The String class has a method called lastIndexOf. Try and see if that makes your parsing easier.

Similar Threads

  1. Text File into String Array
    By mathanv in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2010, 04:30 AM
  2. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 04:31 PM
  3. about making .class into .jar
    By sibbe in forum Java Theory & Questions
    Replies: 7
    Last Post: November 13th, 2010, 12:50 PM
  4. Reading lines of a text file into a string array
    By fortune2k in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 11th, 2010, 10:56 AM
  5. Replies: 1
    Last Post: April 7th, 2010, 03:44 PM