String source = name1;
mut = source.charAt (0);
{
mut1 =mut.toLowerCase();
}
fixing int can not be dereferenced.
Page 1 of 19 Replies - 172 Views - Last Post: 10 October 2012 - 12:49 PM
#1
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.
Replies To: fixing int can not be dereferenced.
#2
Re: fixing int can not be dereferenced.
Posted 10 October 2012 - 11:44 AM
Quote
mut1 =mut.toLowerCase();
should be
mut1 = Character.toLowerCase(mut);
#3
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);
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);
#4
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");
}
}
#5
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?
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?
#6
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
mut is a String
String.charAt() retuns a char
can't put a char in a String
#7
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
#8
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?
#9
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
I betcha if you read that, you'll find all of the methods of the String class, including toLowerCase
#10
Re: fixing int can not be dereferenced.
Posted 10 October 2012 - 12:49 PM
Amadrid064, 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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|