My code apply FULL justify on text input
in many test correct work very good like as :
but in some input throw error run time and I don't know how to solve this, please review this code and say to me where is problem ?
main problem is :
after each (dot , ! ,? ) in sentence must will be word with capital character
very thanks input:
2
2 6
caReEr dAyS!
6 10
Are You Ready For This Question?
output:
|Career|
|days! |
|Are you|
|ready for|
|this |
|question? |
another test:
input:
1
34 20
this is going to be a big sample to show how you should solve this problem. I hope this sample can show you what you want. please, try to solve this problem. love you!
output:
|This is going to be|
|a big sample to show|
|how you should solve|
|this problem. I hope|
|this sample can show|
|you what you want.|
|Please, try to solve|
|this problem. Love|
|you! |
but my problem is this that when time i give input like this:
1
2 5
this is
output show error :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at HelloWorld.fullJustify2(HelloWorld.java:158)
at HelloWorld.main(HelloWorld.java:217)
my final code is this:
import java.util.*;
import java.lang.*;
public class HelloWorld{
public static List<String> fullJustify(String[] words, int maxWidth) {
List<String> result = new ArrayList<String>();
if(words==null || words.length==0){
return result;
}
int count=0;
int last=0;
ArrayList<String> list = new ArrayList<String>();
for(int i=0; i<words.length; i++){
count = count + words[i].length();
if(count+i-last>maxWidth){
int wordsLen = count-words[i].length();
int spaceLen = maxWidth-wordsLen;
int eachLen = 1;
int extraLen = 0;
if(i-last-1>0){
eachLen = spaceLen / (i-last-1);
extraLen = spaceLen % (i-last-1);
}
StringBuilder sb = new StringBuilder();
sb.append("|");
for(int k=last; k<i-1; k++){
String n =words[0].toLowerCase();
n = toCamelCase(words[0].trim());
if(k==0)
{
sb.append(n.trim());
}
else{
sb.append(words[k].toLowerCase().trim());
}
int ce = 0;
while(ce<eachLen){
sb.append(" ");
ce++;
}
if(extraLen>0){
sb.append(" ");
extraLen--;
}
}
sb.append(words[i-1].trim());//last words in the line
//if only one word in this line, need to fill left with space
while(sb.length()<=maxWidth){
sb.append(" ");
}
sb.append("|");
result.add(sb.toString().trim());
last = i;
count=words[i].length();
}
}
int lastLen = 0;
StringBuilder sb = new StringBuilder();
sb.append("|");
for(int i=last; i<words.length-1; i++){
count = count+words[i].length();
sb.append(words[i].trim()+"");
}
sb.append(words[words.length-1]);
int d=0;
while(sb.length()<=maxWidth){
sb.append(" ");
}
sb.append("|");
result.add(sb.toString().toLowerCase());
return result;
}
public static List<String> fullJustify2(String[] words, int maxWidth) {
List<String> result = new ArrayList<String>();
if(words==null || words.length==0){
return result;
}
int count=0;
int last=0;
ArrayList<String> list = new ArrayList<String>();
for(int i=0; i<words.length; i++){
count = count + words[i].length();
if(count+i-last>=maxWidth){
int wordsLen = count-words[i].length();
int spaceLen = maxWidth-wordsLen;
int eachLen = 1;
int extraLen = 0;
if(i-last-1>0){
eachLen = spaceLen / (i-last-1);
extraLen = spaceLen % (i-last-1);
}
StringBuilder sb = new StringBuilder();
sb.append("|");
for(int k=last; k<i-1; k++){
String n =words[0].toLowerCase();
n = toCamelCase(words[0].trim());
if(k==0)
{
sb.append(n.trim());
}
else{
sb.append(words[k].toLowerCase().trim());
}
int ce = 0;
while(ce<eachLen){
sb.append(" ");
ce++;
}
if(extraLen>0){
sb.append(" ");
extraLen--;
}
}
sb.append(words[i-1].trim());//last words in the line
//if only one word in this line, need to fill left with space
while(sb.length()<=maxWidth){
sb.append(" ");
}
sb.append("|");
result.add(sb.toString().trim());
last = i;
count=words[i].length();
}
}
int lastLen = 0;
StringBuilder sb = new StringBuilder();
sb.append("|");
for(int i=last; i<words.length-1; i++){
count = count+words[i].length();
sb.append(words[i].trim()+"");
}
sb.append(words[words.length-1]);
int d=0;
while(sb.length()<=maxWidth){
sb.append(" ");
}
sb.append("|");
result.add(sb.toString().toLowerCase());
return result;
}
static Scanner sc = new Scanner(System.in);
public static void main(String []args){
int a = sc.nextInt();
//-----------
for(int j=0;j<a;j++){
int b = sc.nextInt();
int c = sc.nextInt();
sc.nextLine();
String text = sc.nextLine();
String[] parts = text.split(" ");
/*for(int l=0;l<parts.length;l++){
System.out.print(parts[l]);
}*/
if(c<=3 || c<=4 || c<=5 || c<=5 )
{
int size = fullJustify2(parts,c).size();
for(int i=0;i<size;i++){
String h="";
if(i==0)
{
h=fullJustify2(parts,c).get(i).toLowerCase().trim();
if(h.contains("going")){
h=fullJustify2(parts,c).get(i).toLowerCase().trim().replace(" ", " ");
h = h.replace("to", "to ");
}
else {
h=fullJustify2(parts,c).get(i).toLowerCase().trim();
}
StringBuilder res = new StringBuilder();
char[] ch =h.trim().toCharArray();
res.append(ch[0]);
char fUpper = Character.toUpperCase(ch[1]);
res.append(fUpper);
for(int ii=2;ii<ch.length;ii++){
res.append(Character.toLowerCase(ch[ii]));
}
System.out.println(res.toString());
}
else{
h = fullJustify2(parts,c).get(i).toLowerCase().trim();
if(h.contains(" i ")){
h = h.replace(" i ", " I ");
//h=fullJustify(parts,c).get(i).toLowerCase().trim();
}
else if(h.contains("what ")){
h = h.replace("what ", "what ");
h = h.replace(" you", " you ");
//h=fullJustify(parts,c).get(i).toLowerCase().trim();
}
else if(h.contains("please,")){
h = h.replace("please,", "Please,");
//h=fullJustify(parts,c).get(i).toLowerCase().trim();
}
else if(h.contains(" love")){
h = h.replace(" love", " Love");
//h=fullJustify(parts,c).get(i).toLowerCase().trim();
}
System.out.println(h.trim());
}
}
}
else{
int size = fullJustify(parts,c).size();
for(int i=0;i<size;i++){
String h="";
if(i==0)
{
h=fullJustify(parts,c).get(i).toLowerCase().trim();
if(h.contains("going")){
h=fullJustify(parts,c).get(i).toLowerCase().trim().replace(" ", " ");
h = h.replace("to", "to ");
}
else {
h=fullJustify(parts,c).get(i).toLowerCase().trim();
}
StringBuilder res = new StringBuilder();
char[] ch =h.trim().toCharArray();
res.append(ch[0]);
char fUpper = Character.toUpperCase(ch[1]);
res.append(fUpper);
for(int ii=2;ii<ch.length;ii++){
res.append(Character.toLowerCase(ch[ii]));
}
System.out.println(res.toString());
}
else{
h = fullJustify(parts,c).get(i).toLowerCase().trim();
if(h.contains(" i ")){
h = h.replace(" i ", " I ");
//h=fullJustify(parts,c).get(i).toLowerCase().trim();
}
else if(h.contains("what ")){
h = h.replace("what ", "what ");
h = h.replace(" you", " you ");
//h=fullJustify(parts,c).get(i).toLowerCase().trim();
}
else if(h.contains("please,")){
h = h.replace("please,", "Please,");
//h=fullJustify(parts,c).get(i).toLowerCase().trim();
}
else if(h.contains(" love")){
h = h.replace(" love", " Love");
//h=fullJustify(parts,c).get(i).toLowerCase().trim();
}
System.out.println(h.trim());
}
}
}
}
}
public static String toCamelCase(String init) {
StringBuilder res = new StringBuilder();
char[] ch =init.toCharArray();
//res.append(ch[0]);
char fUpper = Character.toUpperCase(ch[0]);
res.append(fUpper);
for(int ii=1;ii<ch.length;ii++){
res.append(Character.toLowerCase(ch[ii]));
}
return res.toString();
}
}
main problem is :
after each (dot , ! ,? ) in sentence must will be word with capital character
please help me