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.


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
    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");
        String keyboard = scan.nextLine();        //takes a line 2b formatted
        cTrim = keyboard.replaceAll("^\\s+", "");  //compresses whitespace
        text = cTrim.replaceAll("\\s+", " ");
        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)
          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
                  if (i == lastspace) // i = 17
                  i = lastprinted + k; // i would be 17 + 0
          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
    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 10:40 AM.

  2. #2
    Junior Member
    Join Date
    Feb 2011
    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, 05: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, 05:31 PM
  3. about making .class into .jar
    By sibbe in forum Java Theory & Questions
    Replies: 7
    Last Post: November 13th, 2010, 01: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, 11:56 AM
  5. Replies: 1
    Last Post: April 7th, 2010, 03:44 PM