Java formatting assignment.
Hey, new assignment. This time my program needs to read in a width of a line and then format a sentence to create margins.
For example,
This is an example
of text that will
have straight left
and right margins
after formatting
needs to become:
This..is..an.example
of..text..that..will
have..straight..left
and...right..margins
after.....formatting
(In java this looks straight)
The spaces also need to be replaced with .'s (which the program does already) to show it works correctly.
Code :
class Main
{
static public void main (String args[]){
System.out.print("#Width: ");
int width = 20;
System.out.println( width );
System.out.print("#Enter Text: ");
String text = BIO.getString();
text = text.replace( " ", ".");
char ch = '.';
char letters[] = text.toCharArray();
int amtofchr = text.length();
int add = count( text, ch) / amtofchr;
while ( ! text.equals ( "END" )){
System.out.println();
System.out.printf( "Amount Of Spaces: %d", count( text, ch ));
System.out.println();
System.out.printf( "Amount Of Letters: %d", amtofchr );
System.out.println();
System.out.printf( "Amount of Spaces To Add: %d", add );
System.out.println();
System.out.println();
System.out.println();
for ( int i = 0; i<=width; ){
for ( int z = 0; z<=width; ){
while ( i < text.length() ){
System.out.print(letters[i]);
i++;
z++;
if ( z == width ){
System.out.println();
z = 0;
}
}
}
}
break;
}
}
public static int count( String s, char c )
{
String str = s.toLowerCase();
int count = 0;
for ( int i = 0; i < str.length(); i++ )
{
if ( str.charAt(i) == c )
count++;
}
return count;
}
}
heres my code atm, ive managed to get the program to basically print out the char array of the string.
I have a theory on how it could work:
input string
replace spaces with .'s
convert string into string array split at .
calculate how many spaces the sentence has.
calculate how many characters are left to add till the text.length = 20 (i.e. add= width - text.length)
calculate how many spaces to add to each word in the array. (i.e. add / howmanyspaces)
somehow add a space to each word in the array :S
Print the array out.
Is this how you would go about it?
Also tbh you can ignore most of my code, mainly using it to calculate things for the sentence, most of the Amount of spaces stuff will deleted in the final program.
Thanks for any help :)
somehow add the spaces to the word in the array.
Re: Java formatting assignment.
UPDATE
Been spending about half my night on it but im still confused on where to add a loop or something. From my current progress below i understand that the if statement is finishing and then the next word is being called before another . is generated but i dont know where to move anything. Everything i have tried has failed :(
Results:
[C] Please wait for processing
[C] Elapsed time to process request 0.792 seconds
DataSet 1
----------Compilation output--------------------------------------------
javac Main.java
classes in application
class Main
----------Data used as input was----------------------------------------
20
This is an example
of text that will
have straight left
and right margins
after formatting
END
----------Expected answer was-------------------------------------------
This..is..an.example
of..text..that..will
have..straight..left
and...right..margins
after.....formatting
----------Your answer however was [ Excluding lines containing a # ] ---
This..is..an.example
of..text..that.will
have..straight..left.
and..right..margins.
after..formatting..
----------Differences are-----------------------------------------------
2,5c2,5
< of..text..that.will
< have..straight..left.
< and..right..margins.
< after..formatting..
---
> of..text..that..will
> have..straight..left
> and...right..margins
> after.....formatting
------------------------------------------------------------------------
[S] Sorry exercise ci101.java/3.5 was not completed successfully
Code :
class Main
{
public static void main( String args[] )
{
System.out.print("#Enter Width Of Paragraph:");
int width = BIO.getInt();
System.out.print("#Enter Word Here:");
String text = BIO.getString();
char ch = ' ';
int numberofspaces = count( text, ch);
int numberofspacesadd = width - text.length();
int spaceratio = numberofspacesadd % numberofspaces;
String[] words = text.split(" ");
while ( ! text.equals( "END" )){
for (int i = 0; i<words.length; i++){
System.out.print( words[i]);
if ( i < numberofspaces ){
System.out.print( "." );
}
if ( i < spaceratio ){
System.out.print( "." );}
}
System.out.println();
System.out.print("#Enter Word Here:");
text = BIO.getString();
words = text.split(" ");
}
}
public static int count( String s, char c )
{
String str = s.toLowerCase();
int count = 0;
for ( int i = 0; i < str.length(); i++ )
{
if ( str.charAt(i) == c )
count++;
}
return count;
}
}