5 Replies - 335 Views - Last Post: 27 December 2012 - 01:34 PM Rate Topic: -----

#1 Wtf_Jingle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-December 12

"Margin" formatting

Posted 23 December 2012 - 09:03 AM

Hello, I am new to Java as well as this forum.

I am an absolute beginner to Java. The program takes a width that the user inputs, and also a line of text (that can't be longer than the width.) The program then removes the spaces in the sentence and works out how many "spare" characters there are in the sentence. The spaces are then placed back in and replaced by dots (.) and the remaining characters are distributed across the spaces as dots also.

It doesn't make much sense when I try to explain it sadly, so here is my code for the program. Hopefully this clarifies the above.

class Main
{
    public static void main(String args[])
    {
        System.out.print("#Enter Line Width:  ");
        int lineWidth = BIO.getInt();

        System.out.print("#Enter Line Text:  ");
        String lineText = BIO.getString();
                  
        while(! lineText.equals("END"))
           {
            String lineLC = lineText.toLowerCase();
            String [] lineSentence = lineText.split(" ");       //Splitting to individual words to be printed

            int count = 0;
            for(int i=0; i<lineLC.length(); i++)
            {
                if(lineLC.charAt(i) == ' ')
                    count++;
            }
            
            String noSpaces = lineLC.replaceAll("\\s+","");

            int gapsCurrently = count;          //How many gaps(Spaces)
            int spacesToAdd = lineWidth - noSpaces.length();      //How many to add to the sentence
            int spacesAddedAtGap = spacesToAdd/gapsCurrently;       //How many spaces added to each gap
            int spaceDistribution = spacesToAdd%gapsCurrently;      //How many spaces to be distributed across the gaps

            for( int i=0; i<lineSentence.length; i++)
            {
                System.out.print(lineSentence[i]);                
                                   
                if(gapsCurrently > i)
                {
                    System.out.print(".");
                }                
                if(spacesToAdd > i)
                {
                    System.out.print(".");
                }
            }    
            
            System.out.print("#Enter Line Text:  ");
            lineText = BIO.getString();
        }               
    }
}


I have tried to methods as my lecturer believes they are easier, but I cannot get my head around them. The last 'for statement' is the problem with the program, I think.

Any suggestions would be grateful, Jingle.

Is This A Good Question/Topic? 0
  • +

Replies To: "Margin" formatting

#2 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: "Margin" formatting

Posted 23 December 2012 - 09:18 AM

Howdy and welcome to Dream.In.Code, this site is pretty rad. Also if, you are an absolute beginner I am confused as to why you are learning about Arrays right off the bat. I'd suggest taking a look at this:
http://www.dreaminco...a-for-n00blets/
http://www.dreaminco...ogramming-java/

This post has been edited by k3y: 23 December 2012 - 09:20 AM

Was This Post Helpful? 1
  • +
  • -

#3 Wtf_Jingle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-December 12

Re: "Margin" formatting

Posted 23 December 2012 - 09:47 AM

Cheers! I shall have a look at those.

Im at University and everyone is at different levels so they cover everything to get everyone up to the same standard. To clarify by what I meant as a beginner, is that I have never done programming before the start of my course :(. Which is why we have done arrays.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,126
  • Joined: 27-December 08

Re: "Margin" formatting

Posted 23 December 2012 - 01:00 PM

The String split() method actually removes the whitespace and returns tokens with the remaining characters. So between element 0 and 1, there should be a period. Then between elements 1 and 2, etc. So you will want to use a for loop and String concatenation here.
Was This Post Helpful? 1
  • +
  • -

#5 Wtf_Jingle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-December 12

Re: "Margin" formatting

Posted 27 December 2012 - 08:35 AM

What do you mean by elements?

Sorry about the slow reply
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,126
  • Joined: 27-December 08

Re: "Margin" formatting

Posted 27 December 2012 - 01:34 PM

An element is part of an array. So if I have the array:
String[] s = new String[]{"Test", "Testing", "Tested"};



Each of the three Strings are elements of the array.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1