1

I have tried debugging this code, and just cannot find what is wrong.

I am trying to eliminate spaces and capitalize any instance of the word "I" with this code. Then I am trying to put the sentence back together with only one space in between the words. As of right now, if there are 4 spaces in the original string, there will still be 4 spaces in the revised string.

Thanks

public class Sentencefix {

    public static void main(String[] args) {

        System.out.println("Enter your input sentece");
        String input = IO.readString();

        String[] arr = input.split(" ");
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i].trim();
            arr[i] = arr[i].toLowerCase();
            if (arr[i] == "i") {
                arr[i] = arr[i].toUpperCase();
            }

        }
        input = "";
        for (int j = 0; j < arr.length; j++) {
            input = input + ' ' + arr[j];
        }
        input = input.trim();
        char ch = input.charAt(0);
        ch = Character.toUpperCase(ch);
        input = ch + input.substring(1, input.length());
        if (input.charAt(input.length() - 1) != '.' && input.charAt(input.length() - 1) != '!' && input.charAt(input.length() - 1) != '?') {
            input = input + '.';
        }

        System.out.println(input);
    }
}

edit posted the whole code

mkobit
  • 43,979
  • 12
  • 156
  • 150
  • what's your input and what's the expected output and what's the current undesired output ? Example : I have 'asd asd asd i', I want 'asd asd asd I' but i get 'AsD aSd ASD i'. – Nico Dec 11 '14 at 21:41
  • It's always better to include a tag for the language you're using, so that it gets to the people familiar with that language. – Ken White Dec 11 '14 at 21:41
  • sorry it is java and the input is just a string. and the current undersired output is lowercase i's and multiple spaces in between words, if there are multiple spaces in the input string. – NewCoder7117 Dec 11 '14 at 21:46
  • Write your input output in plain text. It will be more obvious to everyone trying to help you. – Nico Dec 11 '14 at 21:51
  • input: " we will be tHERE i think" current output: "we will be there i think" desired "we will be there I think" (Spaces aren't showing on this, assume 3 spaces after "will" in input and current output – NewCoder7117 Dec 11 '14 at 21:53
  • what about space before first word and after last word. do you want that space? – hasan Dec 11 '14 at 22:24

3 Answers3

0

I see two problem.

The first is the way you compare string. Understand that if your "i" string is not uppercase, it means that the if is wrong. This will help you : Java String.equals versus ==

The second thing, is how split works. When you have three space between a word, the first space and last space will be in the array of your split. The result of "aaa aaa".split() will be and array like this :

[0]="aaa"
[1]=" "
[2]=" "
[3]="aaa"

When you " ".trim(), you get and empty string "". At the end, when you loop to reconstruct the whole thing, you append the empty string to your string.

Community
  • 1
  • 1
Nico
  • 6,395
  • 4
  • 25
  • 34
0

compare string using the equals instead of == so line

if(arr[i] == "i") 

becomes

if("i".equals(arr[i]))

For more detail have a look at this: http://www.thejavageek.com/2013/07/27/string-comparison-with-equals-and-assignment-operator/

Pirulino
  • 758
  • 1
  • 9
  • 20
0

I just need to show how it could be simple if we just thought simple.

String output = "";
for (int i=0;i<input.length();i++)
    if (input.charAt(i)=='i')
        input += "I";
    else if (i==input.length()-1 || (input.charAt(i)==' ' && input.charAt(i+1)!=' '))
        output += input.charAt(i);
input = output;
System.out.println(input);
hasan
  • 23,815
  • 10
  • 63
  • 101