9 Replies - 275 Views - Last Post: 10 October 2012 - 12:49 PM Rate Topic: -----

#1 Amadrid064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-October 12

fixing int can not be dereferenced.

Posted 10 October 2012 - 11:20 AM

trying to get my charAt (0) from a string to ignore the input case but getting an int cannot be dereferenced error.

   String source = name1;
    mut = source.charAt (0);
    {
    mut1 =mut.toLowerCase();
    }

Is This A Good Question/Topic? 0
  • +

Replies To: fixing int can not be dereferenced.

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,181
  • Joined: 20-September 08

Re: fixing int can not be dereferenced.

Posted 10 October 2012 - 11:44 AM

Quote

 mut1 =mut.toLowerCase();

should be
 mut1 = Character.toLowerCase(mut);

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: fixing int can not be dereferenced.

Posted 10 October 2012 - 11:45 AM

You can't apply a method to a basic datatype

Lucky you are, most class that represent a basic datatype (Integer, Character, ...) have jacket static method to do the job so

mut.toLowerCase();

can be replace by

char xxx = Character.toLowerCase(mut);
Was This Post Helpful? 0
  • +
  • -

#4 Amadrid064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-October 12

Re: fixing int can not be dereferenced.

Posted 10 October 2012 - 11:59 AM

Ok I changed it to Character.toLowerCase(mut); but now I'm getting a incompatible type error with a required java.lang.String and java.lang.Character. Here's my entire code.

 import java.util.Random;
import java.util.Scanner;

public class Lab5_1
{
  public static void main (String[] args)
  {
    Scanner scan = new Scanner (System.in);
    Random generator = new Random();
   
    String name1, name2, mutation, mut, mut1, mut2;
    
    System.out.print ("Please enter your first name: ");
    name1 = scan.next();
   
    System.out.print ("Please enter your last name: ");
    name2 = scan.next();
   
    String source = name1;
    mut = source.charAt (0);
    {
    mut1 = Character.toLowerCase (mut);
    }
   
    mutation = name2.substring (0,6);
    {
    mut2 = mutation.toLowerCase();
    }
    
    
  
    int num1 = generator.nextInt(10);
    int num2 = generator.nextInt(10);
    int num3 = generator.nextInt(10);
   
    System.out.println (mut1 +mut2 +num1 +num2 +num3+"@student.highlands.edu");
  }
} 

Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,689
  • Joined: 19-March 11

Re: fixing int can not be dereferenced.

Posted 10 October 2012 - 12:11 PM

Mut1 is declared to be a String, and you're assigning a character to it. They're not the same thing.

Looking at this, it looks like you want to assemble a String by taking the first initial, first six characters of the last name, and three random numbers. Why not just assemble the String and call the toLowerCase method of the String class to lowercase the whole thing?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: fixing int can not be dereferenced.

Posted 10 October 2012 - 12:12 PM

mut = source.charAt (0);

mut is a String
String.charAt() retuns a char

can't put a char in a String
Was This Post Helpful? 1
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,181
  • Joined: 20-September 08

Re: fixing int can not be dereferenced.

Posted 10 October 2012 - 12:13 PM

Quote

mut1 = Character.toLowerCase (mut);


mut1 += Character.toLowerCase (mut);


But better to use correct data type in first place

Edit: don't do the above at all ;)

This post has been edited by g00se: 10 October 2012 - 12:19 PM

Was This Post Helpful? 0
  • +
  • -

#8 Amadrid064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-October 12

Re: fixing int can not be dereferenced.

Posted 10 October 2012 - 12:28 PM

What is the toLowerCase method for the String class? will I have to import anything for this method?
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,689
  • Joined: 19-March 11

Re: fixing int can not be dereferenced.

Posted 10 October 2012 - 12:39 PM

I betcha if you enter "java string" into any search engine you choose, the API for the String class will be the first thing that comes up.

I betcha if you read that, you'll find all of the methods of the String class, including toLowerCase
Was This Post Helpful? 1
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: fixing int can not be dereferenced.

Posted 10 October 2012 - 12:49 PM

View PostAmadrid064, on 10 October 2012 - 03:28 PM, said:

What is the toLowerCase method for the String class? will I have to import anything for this method?

Honnestly..... String class ? Look the API for the String class

http://docs.oracle.c...ang/String.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1