Java>text formatter program, no use of arrays

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 3083 Views - Last Post: 16 February 2011 - 05:11 PM Rate Topic: -----

#1 petadeer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-January 11

Java>text formatter program, no use of arrays

Posted 06 February 2011 - 07:48 PM

objective: to write my own methods
rules: can't use arrays. must use String Class and associated methods.
assignment:
#

specified line WIDTH. There are three possible formatted output for an input line. These are the LEFT-JUSTIFIED, RIGHT-JUSTIFIED, and CENTERED versions. Your program must use Java methods.
#

You can assume that the line WIDTH will be at least as long as the longest word in the input line -- in other words, you do not have to worry about splitting a word and hyphenating it.
#

In situations where a word or a line cannot be centered exactly, have the word or line start out one letter to the left.
#

You can assume the maximum line WIDTH will not exceed 80.
#

Multiple white spaces such as tabs and blanks should be compressed. Tabs are denoted by \t, and should be converted to spaces first. You can assume that punctuation marks such as , ! ? . ; are always next to a word, and never surrounded by white spaces by themselves.


example:
	Welcome to the simple text formatter.
	Enter the line width:
	21
	Enter the text to be formatted:
	    Misquotations are the only quotations that are never misquoted.
	Select a format: (L)eft-justified, (R)ight-justified, (C)entered, or (Q)uit
	c
	Sorry, that's not an option.
	Select a format: (L)eft-justified, (R)ight-justified, (C)entered, or (Q)uit
	C

	123456789+123456789+1
	Misquotations are the
	only quotations that
	are never misquoted.

	Select a format: (L)eft-justified, (R)ight-justified, (C)entered, or (Q)uit
	Q
	Bye!



my last two programs have been really simple in comparison, only involving scanning input and printing out messages and last one required using the for loop to produce an ascii triangle. I really have no idea where to begin on this one... if someone could give me an idea of how to approach it or something more that would be great. thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Java>text formatter program, no use of arrays

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Java>text formatter program, no use of arrays

Posted 06 February 2011 - 08:32 PM

You can start by studying the String methods for Java here.

Read about the methods and I think you'll form ideas on how to get on your program. Part of the fun is figuring out how to use the methods to bend to your will and do your bidding. :)
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java>text formatter program, no use of arrays

Posted 06 February 2011 - 08:51 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Java>text formatter program, no use of arrays

Posted 06 February 2011 - 08:53 PM

Also, if you use String.format(), which is likely, you can find the format specifiers here:
http://download.orac...ext/Format.html
Was This Post Helpful? 1
  • +
  • -

#5 petadeer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-January 11

Re: Java>text formatter program, no use of arrays

Posted 09 February 2011 - 04:37 PM

hi guys,
I still don't have code that I need you to look over - I'm still having trouble figuring out how to begin.
Well, I do have code but it's just this...:
import java.util.*;

class Format {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Welcome to the simple text formatter.");
    System.out.println("Enter the desired line width");
    int width; //desired line width
    width = scan/nextInt();
    System.out.println("Enter the text to be formatted");
    String textInput; //text to be formatted
    textInput = scan.nextLine();
    System.out.print("Select a format: (L)eft-justified, (R)ight-justified");
    System.out.println(", (C)enterd, or (Q)uit");
    char formatChoice; //format type selection
    scan.nextChar();
  }

  public static String Left() {



I looked at the String methods list and thought about using Trim() but that only omits the leading and trailing white space. how can I remove the white space between words if its more than 1?

also... for the justification, I could use printf() for the Left and Right, right? but what about centered...
Was This Post Helpful? 0
  • +
  • -

#6 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Java>text formatter program, no use of arrays

Posted 09 February 2011 - 04:59 PM

For the centered part you could print blanks and put your input at the center. Specify a maximum amount of width. Like 30 or something. To do the centering, get the width, subtract it on your max width, then divide the result from maxwidth by two. Then add spaces at the front at the end of the string based on the result of the preceding equation, so it would look like this: (spaces are denoted as underscores)

hat = width 3
width - maxwidth = 27
27/2 = 13
this means 13 at the front, and thirteen at the end
_____________hat_____________
Was This Post Helpful? 1
  • +
  • -

#7 petadeer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-January 11

Re: Java>text formatter program, no use of arrays

Posted 09 February 2011 - 05:04 PM

Ah,
if(StringUtils.hasLength(sText)
 {
     sText= sText.replaceAll("\\s+", " AND ");		   
}



I found this in someone's explanation elsewhere and I guess in my case I can apply it by doing
if(StringUtils.hasLength(sText)
 {
     sText= sText.replaceAll("\\s+", " ");		   
}


to compress the space. I'm going to assume that "//s+" means one or more spaces but where do I go about finding the meaning for something like this? //s+ is too short/general to type into Google and I can't find it in my book... I have a lot of trouble going through the String classes because usually there are concepts in them used to explain whatever concept it is that I'm trying to learn that I haven't been introduced to yet, so I end up more confused. explaining one concept I'm unfamiliar with with more foreign concepts.

also, why can't I edit my previous post just to include updates so I don't double post? I tried to, but it said I don't have permission.
Was This Post Helpful? 0
  • +
  • -

#8 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Java>text formatter program, no use of arrays

Posted 09 February 2011 - 05:16 PM

I didn't find what "\\s+" means but I think this is it.

According to Google "\s" means whitespace in regular expressions. Regular expressions are codes that are used to find specific characters inside a string. I'm not well versed with regular expressions(heck, I haven't even used them yet. Maybe you could use regular expressions with Java, but I'm not the one you really wanna ask about it since I don't know stuff about it. But theoretically if you can use RegExp with Java you could use it to find the whitespaces on your input.

(After Googling)

Confirmed! Java can use regular expressions. Try reading about it at this link.
http://www.regular-e....info/java.html

Anyway to your last question it is better if you just post so we could just read your latest progress other than looking at one post and referring to that post every time which will suck because we won't know what you did last and how you got there. Posting is free and adds numbers to your stat that is named "Posts". I'll let you in on a little secret: once it reaches a certain number you'll win a trip to Disneyland. Said number: I don't know.
Was This Post Helpful? 0
  • +
  • -

#9 petadeer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-January 11

Re: Java>text formatter program, no use of arrays

Posted 11 February 2011 - 05:37 AM

ok. when using Scanner class, (it's the only input taker I'm familiar with), I use scan.nextLine(), which allows me to input text to be formatted. however, when I use scan.nextInt() preceding it in my main and input a number, the program never lets me get to the part that I input a line.

for example:
class WhiteSpace
{
  public static void main(String[] args)
  {
    Scanner scan = new Scanner(System.in);
    String text;                              //variable for user's input
    System.out.println("enter desired phrase");

    String keyboard = scan.nextLine();        //takes a line 2b formatted
    
    text = keyboard.replaceAll("\\s+", " ");  //compresses whitespace
    System.out.println(text);                 //print modified

  }
}


this code will let me enter a phrase such as "hello dude" and it will output "hello dude".

edit: there are several spaces between my first "hello" and "dude" but for some reason it doesn't appear once I post so they look identical... but my function compresses the white space

however,
class WhiteSpace
{
  public static void main(String[] args)
  {
    Scanner scan = new Scanner(System.in);
    String text;                              //variable for user's input
    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
    
    text = keyboard.replaceAll("\\s+", " ");  //compresses whitespace
    System.out.println(text);                 //print modified
  }
}


this no longer lets me input a string.
the output is:
enter desired width:
25    <--- I enter 25
enter desired phrase:



and I'm never allowed to enter any text. what the heck?!?!?

I looked at the Scanner class and the description for nextInt() is "Scans the next token of the input as an int". the description is similar for nextFloat, nextDouble, etc. But the description for nextLine() is as follows: "Advances this scanner past the current line and returns the input that was skipped."

I have no idea what that means... What do I do now?

This post has been edited by petadeer: 11 February 2011 - 05:40 AM

Was This Post Helpful? 0
  • +
  • -

#10 petadeer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-January 11

Re: Java>text formatter program, no use of arrays

Posted 11 February 2011 - 06:02 AM

if I include an 'extra' scan.nextLine(); right above the String keyboard = scan.nextLine();, THEN it takes input from me and formats it. but why on earth do I need two? the description of the method uses vague pronouns so I have no idea what is referring to/what it means
Was This Post Helpful? 0
  • +
  • -

#11 petadeer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-January 11

Re: Java>text formatter program, no use of arrays

Posted 11 February 2011 - 06:52 AM

ok, I am quite proud of what I have so far. but I am still discouraged as to whether I can finish everything in 24 hours worth of time.

class WhiteSpace
{
  public static void main(String[] args)
  {
    Scanner scan = new Scanner(System.in);
    String text;                              //variable for user's input

    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

    text = keyboard.replaceAll("\\s+", " ");  //compresses whitespace
//    System.out.println(text);                 //print modified

    if (text.length() > width)     //when compressed input still has more
    {                              //chars than desired width
      for (int i = 0; i < width; i++)
      {                                 //prints each char in line until
        System.out.print(text.charAt(i)); //reach max width
      }
      System.out.println();                //jumps line once hit max width

      for (int i = width; i < 2*width; i++)  //this continues
      {                                     //obviously not dynamic
        System.out.print(text.charAt(i));   //would not work with very
      }                                     //large inputs
      System.out.println();
    }
    else
      System.out.println(text)
  }
}


I have accomplished thus far:
-take desired max width
-take input to be formatted
-compress the white space
-if, after, compressed, the number of characters is still larger than the desired width, print out the number of characters up until that point then jump one line.

however, I got 3 problems (possibly more I haven't caught) so far.
First, my code for the line going down isn't dynamic. I can't just write enough conditionals for n*width forever.
Second, if I set the max width at 5, and input hello dude, then I get:
hello
 dude


and it's all gravy. but if I enter "hellodude", which is only 9 characters, I get an error. I guess the if statement wants to print out that 10th charAt(i) but can't.
what should I do in this situation?
Third, I need the program to understand... that if my last word bleeds into the max width, it needs to drop the whole word, not just drop it mid-word.

so if max width is 10, I need
hello
dudette

instead of:
hello dude
tte

Thanks for the help, but it's bed time at 6 am. see ya'll tomorrow.
Was This Post Helpful? 0
  • +
  • -

#12 petadeer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-January 11

Re: Java>text formatter program, no use of arrays

Posted 11 February 2011 - 06:04 PM

I changed my code a fair amount since post 11 so you can ignore that one if you want/haven't already...
here is how my code is sitting as of now:
    int i = 0;
    int n = 1;  
    if (text.length() < width)
      System.out.println(text);
    else
    {
      while (i < text.length())
      {
        if (text.length() > width)
        {
          System.out.print(text.charAt(i));
          i++;
          if (i == width*n)
          {
            System.out.println();
            n++;
          }
        }
      }
      System.out.println();
    }


the bit that compresses the white space is all done before this section. so regardless of whatever was inputted, "text" here will be the compressed version with only one space between each word.

I am still stuck on how to allow my program to detect whether the last word at the end of each line needs to be jumped down entirely rather than mid-word.

for input: hello mister how are you doing are you eating noodles today
I get output:
hello mister how are
 you doing are you e
ating noodles today


but I need:
hello mister how are
you doing are you
eating noodles today


so there are obviously 2 differences here.
first, the "you" is justified to the left so that a new line doesn't start with a space " ".
second, eating is detected as a word and instead of being separated, the whole word is moved down one line.

any ideas? :)

This post has been edited by petadeer: 11 February 2011 - 06:33 PM

Was This Post Helpful? 0
  • +
  • -

#13 petadeer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-January 11

Re: Java>text formatter program, no use of arrays

Posted 12 February 2011 - 08:49 AM

NO ARRAYS

IF SOMEONE COULD TAKE THE TIME TO HELP ME THAT WOULD BE REALLY TRULY SINCERELY ETERNALLY APPRECIATED

my other thread seems to be too cluttered here is my most updated revision

I'll try to be clear with what I'm doing and my thought process...
to know when to skip lines, I made the conditional that if variable i is equal to the width AND i is a white space, the program goes into the next line and increments i.
if i is NOT a white space, then I'm assuming it's in the middle of a word, so I subtract i-- in a loop until it reaches a space then jumps into the nextline.
HOWEVER, my problem is that the variable i doesn't equal width until it is obviously at the last char of the line, meaning that if it WERE in the middle of a word, the few characters before it would have already been printed. I can't figure it out.

googling hasn't been much help because all suggestions lead to using other methods I'm unfamiliar with. again, thanks if you can help it'd be greatly appreciated.
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;
    String nuLine, nuuLine;

    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 n = 1;
    int k = 0;
    if (text.length() < width)
      System.out.println(text);
    else
    {
      while (i < text.length())
      {
        if (i == width*n - 1 && text.charAt(i) == ' ')
        {
          System.out.println();
          n++;
          i++;
        }
        if (i == width*n -1 && text.charAt(i) != ' ')
        {
          while (text.charAt(i) != ' ')
          {
            i--;
            k++;
          }
          System.out.println();
          i = i + k;
          n++;
        }
        if (i < text.length())
        {
          System.out.print(text.charAt(i));
        }
        i++;
      }   
      System.out.println();
    }
  }
}



this is a quick variation I made a few minutes ago, but I've been up for 24 hrs and am dead tired so I really only debugged my first "if" for when the last character in the line is a " ". the problem then is that when i resets to 0 once the conditional is met, line 3 would have a problem...

    int i = 0;
    int n = 1;
    int k = 0;
    int j;
    if (text.length() < width)
      System.out.println(text);
    else
    {
      while (i < text.length())
      {
        if (i == width*n - 1 && text.charAt(i) == ' ')
        {
          i = 0;
          while (i < width*n)
          {
            System.out.print(text.charAt(i));
            i++;
          }
          System.out.println();
          n++;
        }
        if (i == width*n - 1 && text.charAt(i) != ' ')
        {
          while (text.charAt(i) != ' ')
          {
            i--;
            k++;
          }
          j = i;
          i = 0;
          while (i < j)
          {
            System.out.print(text.charAt(i));
          }
          System.out.println();
          while (i < text.length())
            System.out.print(text.charAt(i));
//          System.out.println();
  //        i = i + k;
    //      n++;
        }
      i++;
      }    
      System.out.println();
    }


Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,376
  • Joined: 27-December 08

Re: Java>text formatter program, no use of arrays

Posted 12 February 2011 - 01:54 PM

Duplicate threads merged. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

#15 Guest_KufNayr*


Reputation:

Re: Java>text formatter program, no use of arrays

Posted 14 February 2011 - 09:51 PM

bump
Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2