6 Replies - 818 Views - Last Post: 03 January 2013 - 07:00 AM Rate Topic: -----

#1 Amokachi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-November 12

char cannot be dereferenced

Posted 03 January 2013 - 03:21 AM

Hello all,

I was trying to write a program which will print the even indexes of the given sentence.The program supposed to make
them upper case if they are lower case and vice-versa. But I got the following errors:

error: cannot find symbol line 26
error: char cannot be dereferenced line 28

Can you please help me with that?

Thank you in advance


import java.util.Scanner;
/**
 * @(#)E03012013.java
 *
 * E03012013 application
 *
 * @author 
 * @version 1.00 2013/1/3
 */
 
public class E03012013 {
    
    public static void main(String[] args) {
    	
    	Scanner scan = new Scanner(System.in);
    	
    	System.out.print("Please enter your name: ");
		String name = scan.nextLine();
		int in;
		
		
		for(int index1 = 0; index1 <= name.length()-1; index1 = index1 +2)
		{	
			in =(int) name.charAt(index1);
			if(in <= 90 && in >= 65)
				name.charAr(index1).toLowerCase();
			if(in <= 122 && in >=97)
				name.charAt(index1).toUpperCase();
			System.out.print(name.charAt(index1) + " "); 
			 	
		}   
    
    }
}

This post has been edited by GregBrannon: 03 January 2013 - 03:34 AM
Reason for edit:: Fixed code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: char cannot be dereferenced

#2 Amokachi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-November 12

Re: char cannot be dereferenced

Posted 03 January 2013 - 03:33 AM

the "code" tags didn't work how will I erase this post?
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: char cannot be dereferenced

Posted 03 January 2013 - 03:43 AM

For the code tags, the closing tag begins with the other slash, '/'. You can also highlight the code and use the tool button in the editor's tool area to indicate that the highlighted text is code. Don't worry about it, not a big deal, no need to erase and start over.

As for your Java questions:

There is a typo at line 26: charAt vs charAr.

The variable 'name' is a String object. The method charAt() on the String name returns a primitive char value which knows neither upper or lower case. It's kind of like asking a primitive integer to declare whether it's greater than 50 or less than 50. It can't.

This post has been edited by GregBrannon: 03 January 2013 - 03:44 AM

Was This Post Helpful? 2
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: char cannot be dereferenced

Posted 03 January 2013 - 04:49 AM

name.charAr(index1).toLowerCase();

you probably want to refer to the toLowerCase() method of the Character class. But the String.charAt() method returns a char not a Character. You probably want

Character.toLowerCase(name.charAt(index1));


Also don't use hardcode ASCII value as you do. You want everybody to know that you know your ASCII table by heart ? Using <= 'Z' and >= 'A' is a lot clearer than <= 90 && >= 65

Happy coding
Was This Post Helpful? 2
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2783
  • View blog
  • Posts: 11,775
  • Joined: 20-September 08

Re: char cannot be dereferenced

Posted 03 January 2013 - 05:18 AM

Quote

Using <= 'Z' and >= 'A' is a lot clearer than <= 90 && >= 65
that's true, but you don't even want to do that as it simply limits your code to a specific alphabet. Use the following, together with the isUpperCase method and that should work for any alphabet that has the concepts of upper and lower case:

http://docs.oracle.c...LowerCase(char)
Was This Post Helpful? 2
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: char cannot be dereferenced

Posted 03 January 2013 - 05:57 AM

I think the purpose of this program is to convert to lowerCase what is upperCase and vice-versa so should be (for all alphabets)

    char[] digit = name.toCharArray();
    for(int i = 0; i < digit.length; ++i) {
        if(Character.isLowerCase(digit[i]))
           digit[i] = Character.toUpperCase(digit[i]);
        else if(Character.isUpperCase(digit[i]))
           digit[i] = Character.toLowererCase(digit[i]);
    }
    name = new String(digit);


Was This Post Helpful? 1
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2134
  • View blog
  • Posts: 3,272
  • Joined: 21-June 11

Re: char cannot be dereferenced

Posted 03 January 2013 - 07:00 AM

To be entirely on the safe side, you should use the String methods toLowerCase and toUpperCase (after converting the character to a String) - not the Character methods. There are letters in some languages which can only be converted to upper case by replacing them with multiple other letters. For those Character.toUpperCase won't produce a meaningful result.

For example in German the upper case version of the letter "" is "SS". "".toUpperCase() will correctly return "SS", Character.toUpperCase('') will return '' unchanged (because obviously 'SS' isn't a valid character).
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1