0

The directions that were given were: Write a program that prompts for and accepts a line of text from the user, and then prints it out with every character in lower case except for the characters that are immediately after a space. These characters are to be capitalized.

NOTE: don't split the string

  • Accept a string from user
  • Turn all characters to lower case
  • Go through each character individually and capitalize the characters after the spaces

My Current Code:

import java.util.Scanner;

public class Capitalize
{
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a line of text:");
    String TextLine = input.next();
    //String FirstLetter = input.next();
    String NewTextLine = " ";
    int Length = TextLine.length();
    System.out.print("The all lower case line of text is: " + TextLine);
    for(int i = 0; i < Length; i++)
    {
        char Letter = TextLine.charAt(i);
        System.out.print(Letter);
        if(Letter != ' ') 
        {
            Letter = Character.toLowerCase(TextLine.charAt(i));
        }
        else
        {
            Letter = Character.toUpperCase(TextLine.charAt(i));
        } 
        NewTextLine = NewTextLine + Letter;
        }
        System.out.print("\nThe new text line of text is: " + NewTextLine);
    }
}

The Output Is:

Enter a line of text: aBc Def GHIJ The all lower case line of text is: aBcaBc The new text line of text is: abc

This isn't what I want to get as the output. I want to take " aBc DeF GHIJ " and get the out put of all lower case " abc def ghij " and the new text line to be " Abc Def Ghij "

Can someone please help me to figure out where I went wrong with my code? And how to fix it.

3
  • 2
    in Java, single quotes are for characters, double quotes are for Strings. Apr 10, 2013 at 0:45
  • 1
    As a side note, please follow the Java coding convention, by using camelCase for the variable names.
    – nhahtdh
    Apr 10, 2013 at 0:47
  • 1
    @nhahtdh - An ironic suggestion for a capitalization program ;-)
    – jahroy
    Apr 10, 2013 at 0:49

5 Answers 5

2

You're comparing a char with a literal String " ":

if(Letter != " ")
   char      String

Change the whitespace by literal character ' '.

if(Letter != ' ')

From Primitive Data Types:

Character and String Literals

Always use 'single quotes' for char literals and "double quotes" for String literals.

2

Change

Letter != " "

To

Letter != ' '

Double quotes are for Strings and single quotes are for Characters.

You could also do:

Letter != 040  // Octal
Letter != 32   // Integer
Latter != 0x20 // Hexadecimal

These are the ASCII values for Space.

4
  • I changed that but now get the error: The all lower case line of text is: aBcException in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.charAt(String.java:658) at Capitalize.main(Capitalize.java:16) ----jGRASP wedge2: exit code for process is 1. Apr 10, 2013 at 1:16
  • Your problem is here: for(int i = 0; i < Length; i++ { char Letter = TextLine.charAt(i-1); } When i == 0, you are grabbing charAt(-1). Apr 10, 2013 at 1:18
  • Thanks. that makes sense but I am kinda confused on the actual code part. I thought I had the right logic going on but am getting a completely wrong output. Any help would be greatly appreciated Apr 10, 2013 at 2:12
  • Use a boolean flag. I just whipped this up a min ago. Apr 10, 2013 at 2:14
1

It seems like you've defined Letter as a char somewhere. Using double-quotes (" ") denotes a String. Single quotes (' ' ) denotes a char. So, change if(Letter != " ") to if(Letter != ' ')

3
  • My problem is that I thought that was what it is but changing that i still get errors in my program. Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.charAt(String.java:658) at Capitalize.main(Capitalize.java:16) Apr 10, 2013 at 1:17
  • ` char Letter = TextLine.charAt(i-1)` : On the first iteration, i is 0. So i-1 is -1, which is an invalid index.
    – drew moore
    Apr 10, 2013 at 1:20
  • Thanks I didn't realize I was trying to start at something that wasn't there. I am kinda confused on the actual code part. I thought I had the right logic going on but am getting a completely wrong output. Any help would be greatly appreciated Apr 10, 2013 at 2:13
1

You are comparing a char to a string. A primitive type to an object.

Change your code to if(Letter != ' ')

0

As has already been pointed out, you shouldn't be using Letter != " ", but instead, you could use Character.isSpaceChar(Letter) or Charater.isWhiteSpace(Letter)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.