Anthony Owen
Anthony Owen

Reputation: 33

Java reading upper/lower case characters and converting them

Okay, so I have another program. This one should be rather self explanatory. This program is made to read characters from continuous java input (line after line) until the . is entered. It then converts all Upper case characters to Lower case and vice versa (Special characters or numbers are NOT modified). It then outputs the changes.

import java.io.*;
class textchanger
{
    public static void main(String[] args) throws IOException
    {
    InputStreamReader inStream = new InputStreamReader (System.in);
    BufferedReader userInput = new BufferedReader (inStream);
    String inputValues;
    char charCounter = 0;

    System.out.println("Welcome the to text modifier!");
    System.out.println("Inset any character(s) you wish: ");

    inputValues = userInput.readLine();

    while ((inputValues = userInput.readLine()) != null && inputValues.indexOf('.') == -1) {
    }
    int length = inputValues.length();

    for(int i = 0; i < length; i++) {
        charCounter = inputValues.charAt(i);
    }
    if(Character.isUpperCase(charCounter)) {
    Character.toLowerCase(charCounter);
    System.out.println(charCounter);
    }
    else if(Character.isLowerCase(charCounter)) {
    Character.toUpperCase(charCounter);
    System.out.println(charCounter);
    }

    }
}

Any help on this would be appreciated. The current error(s) that I receive is nothing being displayed after the "." character is input.

Upvotes: 1

Views: 6120

Answers (2)

Reimeus
Reimeus

Reputation: 159864

CharCounter is already defined in the scope of the main method. Replace

char CharCounter = inputValues.charAt(i);

with

CharCounter = inputValues.charAt(i);

After doing this the variable will need to be initialized as all local variables are required to be initialized in Java:

char CharCounter = 0;

And use Java naming conventions when naming variables such as charCounter.


class TextChanger {
    public static void main(String[] args) throws IOException {
        InputStreamReader inStream = new InputStreamReader(System.in);
        BufferedReader userInput = new BufferedReader(inStream);
        String inputValues;

        System.out.println("Welcome the to text modifier!");
        System.out.println("Inset any character(s) you wish: ");

        while ((inputValues = userInput.readLine()) != null
                && inputValues.indexOf('.') == -1) {
            int length = inputValues.length();
            for (int i = 0; i < length; i++) {
                char charCounter = inputValues.charAt(i);
                if (Character.isUpperCase(charCounter)) {
                    System.out.println(Character.toLowerCase(charCounter));
                } else if (Character.isLowerCase(charCounter)) {
                    System.out.println(Character.toUpperCase(charCounter));
                }
            }
        }
    }
}

Upvotes: 3

stinepike
stinepike

Reputation: 54742

you were converting the character after for loop while you are declaring the CharCounter in for loop. Use like following

   for(int i = 0; i < length; i++) {
        char CharCounter = inputValues.charAt(i);
        if(Character.isUpperCase(CharCounter)) {
            Character.toLowerCase(CharCounter);
            System.out.println(CharCounter);
        }else if(Character.isLowerCase(CharCounter)) {
            Character.toUpperCase(CharCounter);
            System.out.println(CharCounter);
        }    
    }

Upvotes: 1

Related Questions