user2157845
user2157845

Reputation: 47

Capitalization Java Program

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

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.

Upvotes: 0

Views: 850

Answers (5)

MadProgrammer
MadProgrammer

Reputation: 347194

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

Upvotes: 0

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48600

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.

Upvotes: 2

sazoo
sazoo

Reputation: 127

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

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

Upvotes: 1

drew moore
drew moore

Reputation: 32670

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 != ' ')

Upvotes: 1

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

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.

Upvotes: 2

Related Questions