See the code below,

import java.io.*;
class Test1
{
  public static void main (String arge[]) throws IOException
  {
  InputStreamReader values = new InputStreamReader (System.in);
  BufferedReader br = new BufferedReader (values);
  String value,store;
  char ch;
  System.out.print ("Input String: ");
  value = br.readLine();
  store = '';
  int ln = value.length();
  for (int i=0; i<ln; i++)
  {
    ch=value.CharAt(i);
    Switch (ch)
    {
      case ' ':
      break;
      default:
      store+=ch;
    }
  }
  }
}

I want my program to remove all spaces in the string and store the new string in the variable

store

but my program giving error when i am running it.


Test1.java:12: empty character literal
store = '';
^
Test1.java:12: unclosed character literal
store = '';
^
Test1.java:12: ';' expected
store = '';
^
Test1.java:17: ';' expected
Switch (ch)
^
Test1.java:19: orphaned case
case ' ':
^
5 errors

Recommended Answers

All 26 Replies

One problem I see is that you're trying to initialize a String with a char value in line 12. Single quotes ('') enclose a char, double quotes("") enclose a String.

The rest of your error messages aren't making immediate sense to me, but try fixing that and see what happens.

thanks....i sorted out second problem, its switch not Switch :)
but program is still not working..
here is the code.

import java.io.*;
class Test
{
  public static void main (String arge[]) throws IOException
  {
  InputStreamReader values = new InputStreamReader (System.in);
  BufferedReader br = new BufferedReader (values);
  String value,store;
  char ch;
  System.out.print ("Input String: ");
  value = br.readLine();
  store = "";
  int ln = value.length();
  for (int i=0; i<ln; i++)
  {
    ch=value.CharAt(i);
    switch (ch)
    {
      case '\u0020':
      break;
      default:
      store+=ch;
    }
  }
  }
}

and error is,

Test.java:16: cannot find symbol
symbol : method CharAt(int)
location: class java.lang.String
ch=value.CharAt(i);
^
1 error

Edit:


Problem Solved

The method name should always be starting with lower case conventionally speaking.
the code should be:

ch=value.charAt(i);

Yes..! Thank You :)

I am getting another error when i am trying to convert uppercase character to lower case in a string,

import java.io.*;
class Test
{
  public static void main (String arge[]) throws IOException
  {
  InputStreamReader values = new InputStreamReader (System.in);
  BufferedReader br = new BufferedReader (values);
  String value,store;
  char ch;
  System.out.print ("Input String: ");
  value = br.readLine();
  store = "";
  int ln = value.length();
  for (int i=0; i<ln; i++)
  {
    ch=value.charAt(i);
    switch (ch)
    {
      case isUpperCase():
      toLowerCase();
      break;
      default:
      store+=ch;
    }
  }
  System.out.print (store);
  }
}

Error :

Test.java:19: cannot find symbol
symbol : method isUpperCase()
location: class Test
case isUpperCase():
^
Test.java:20: cannot find symbol
symbol : method toLowerCase()
location: class Test
toLowerCase();
^
2 errors

and when i am using if-else instead of switch , my program is not working.

for (int i=0; i<ln; i++)
  {
    ch=value.charAt(i);
    if (ch=='\u0020' || ch=='\u0009')
    break;
    else
    store+=ch;
  }

The job to convert uppercase character to lower case should be done after the confirmation of the non-space character(i.e. it is not a space). Therefore the switch code should be:

switch (ch)
    {
      case ' ':
      break;
      default:
      if (Character.isUpperCase(ch))
      ch = Character.toLowerCase(ch);
      store+=ch;
    }

Since the wrapper class Character is used one has to import the package: java.lang.*;
Attached please find the code modified.

Accodring to your for loop body, if a space has been found, the “break" is executed so that the loop operation terminates. It will not continue to do scanning. You should replace "break" by "continue".

for (int i=0; i<ln; i++)
  {
    ch=value.charAt(i);
    if (ch==' ')
    continue;
    else
    store+=ch;
  }

i want to do this without using if statement in a Switch,

see this code,

switch (ch)
    {
      case '\u0020':
      break;
      case '\u0009':
      break;
      case Character.isUpperCase(ch):
      {
      ch=Character.toLowerCase(ch);
      store+=ch;  
      }

this is not working , WHY ??

line 7 : case Character.isUpperCase(ch): is wrong. The reason is:

Only constant values may define each case. That is, Following each "case" must be a constant rather than variable/expression. The Character.isUpperCase(ch) is not a constant, thus it is not working.
If you don't like "if... " you may delete "if... ", which also works well. So the correct code for the switch could be:

switch (ch)    {     
      case '\u0020':
      break;
      case '\u0009':
      break;
      default:
         ch=Character.toLowerCase(ch);
         store+=ch;  
  	}
commented: +++ :) +0

thanks, I've understand what you've said but if i want to use it in a case instead of default then what will i do ???

if its not a constant then how will i use it in a case instead of default ??

Suppose , i want to add two case, upper to lower and lower to upper so do i have to use two defaults ???

i want to add two case

You may have one choice only for the output: either all lower case or all upper one unless you want to have two kinds of output: Upper and Lower. If you want both you have to do twice.

i use it in a case instead of default

I have no solution. We need help.

if its not a constant then how will i use it in a case

I don't think you can use a variable with case.
You'll have to use if() in the default: section.

Can you define what the code is supposed to do?

i want my program to convert upper case letters to lower case and lower case letters to upper case and remove all spaces and tabs between the characters.
i have to do this using switch statement.
here is the code.

import java.io.*;
class Test1
{
  public static void main (String arge[]) throws IOException
  {
  InputStreamReader values = new InputStreamReader (System.in);
  BufferedReader br = new BufferedReader (values);
  String value,store;
  char ch;
  System.out.print ("Input String: ");
  value = br.readLine();
  store = "";
  int ln = value.length();
  for (int i=0; i<ln; i++)
  {
    ch=value.charAt(i);
    switch (ch)
    {
      case '\u0020':
      break;
      case '\u0009':
      break;
      case:
      ch=Character.toLowerCase(ch);
      store+=ch;
      break;
      default:
      ch=Character.toUpperCase(ch);
      store+=ch; 
    }
  }
  System.out.print (store);
  }
}

what case should i use to convert upper case to lower case??

If you absolutely must use a switch then you can code this with 26 cases each for upper & lower case chars:
case 'a': case 'b': ... case 'z': // convert to upper case; break;
case 'A': case 'B': ... case 'Z': // convert to lowercase; break;

Xufyan - I like your switch, it's probably the best approach to a poorly-designed problem, but the only way I can see to make it work is to compare the result of the toLower in line 24 to the input char - and that's going to require an if, or else a bunch of code that simply obfuscates an if.*

If the use of 'if' is forbidden, then you're stuck with James' brute-force solution, which may in fact be what your professor intended. What you can learn from this is that switch statements are generally not efficient or readable solutions to regular transformations or mappings. (Personally, I've only found one problem for which switches are a good answer, and that's processing menu input for a CLI-driven program. Even then, it's only good because I haven't found a good way to put method calls into an array or a map, as I'd do in perl)


* For an example of obfuscation, you could div the input char by 'a' and use a while loop on the result to get an "if this is lowercase". A labeled break takes you around the following toLower, and you're good. Probably not allowed, though.

ch=value.charAt(i);
SWITCH:
    switch (ch)
    {
      case '\u0020':
      break;
      case '\u0009':
      break;
      default:
      while (ch/('a') >0){
        ch=Character.toUpperCase(ch);
        store+=ch;
        break SWITCH;
      }
      ch=Character.toLowerCase(ch);
      store+=ch;

      break; 
    }
commented: Great :) +0

Thanks BRO ..! :)

Using the condition in a while amd then breaking is exactly the same as using an if/else statement.
That's a lawyer's solution. It follows the exact wording but not the intent.

i kn0w but there is no other way to do this.

The cases shown by James.

Using the condition in a while amd then breaking is exactly the same as using an if/else statement.
That's a lawyer's solution. It follows the exact wording but not the intent.

I agree. As I said, this is an obfuscation, and it's probably not allowed. However, if you wanteed to write the program using a switch statement and also be reasonable, an if is required. The 54-case solution is probably what the teacher wants, and it's a terrible solution. The only place you're likely to see code like that is here.

Come to think of it, it would probably be less trouble and more fun to generate that switch statement programmatically. Xufyan, you might consider writing the code to generate that 54-case solution, and turn that in with the assignment. You might even get some extra credit out of it!

I took that approach some times when a prof gave a stupid assignment. Write code to generate the code.

commented: u're a born programmer :) XuFyan :) +0

using 54 cases looks very Cheap...!!!!
can't i use a loop in case to loop through each character ??
i mean....

for (i='a'; i<='z'; i++)
case 'i':

isn't it a better way if it is possible ??

if it is possible

Ask the compiler.

I don't think so, but that's a lovely inversion of the switch. I like your creativity, unfortunately I don't think it'll get you anywhere in this case.

Why don't you try doing it mechanically? Then you can use your for loop...

import java.io.*;
class Test1
{
  public static void main (String arge[]) throws IOException
  {
  InputStreamReader values = new InputStreamReader (System.in);
  BufferedReader br = new BufferedReader (values);
  String value,store;
  char ch;
  System.out.print ("Input String: ");
  value = br.readLine();
  store = "";
  int ln = value.length();
  for (int i=0; i<ln; i++)
  {
    ch=value.charAt(i);
	for (char j='a'; j<='z'; j++)
    switch (ch)
    {
      case j:
      ch=Character.toUpperCase(ch);
      store+=ch;
    }
  }
  System.out.print (store);
  }
}

Error:
C:\Program Files\Java\jdk1.6.0_20\bin>javac Test1.java
Test1.java:20: constant expression required
case j:
^
1 error

:(

commented: Creative thinking, nice. +1
commented: yes +0

Nice try, though.

Thnanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.