Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 684

# CharAt command

G'day All
Im only just learning java and part of my course is to develop a programme that;
* asks for users first name in lower case
* asks for users second name in lower case
* generates a random number (between 1 and 10)
* asks for users guess at number
* the announces succsess or failure and identifies user by thier first and last inital

now when i run the compiler it announces i need a ")" on the line   "f = char charAt(z 0);" and i hav no idea why below is the entire code, any and all help would be greatly appreaciated

//
//*******************************************
//** Guessing Game                         **
//** Version 1.0                           **
//** Last Updated 21/03/2003               **
//** By Dan Maker                          **
//*******************************************

import cs1.Keyboard;
import java.util.Random;
import java.lang.Math;

public class Guess
{
//*******************************************
//** Simple Guessing Number Game           **
//*******************************************
public static void main (String[]args)
{
int f,b,g,d,num1,num2,difference,e,z;

String a;
string f;
f = char charAt(z 0);
b = toUpperCase (f);

String c;
System.out.println("Please Enter Second Name (lower case)");
g = Char.charAt(c 0);
d = toUpperCase(g);

Random generator = new Random();
num1 = generator.nextInt(10);

System.out.println("I am thinking of a number between 1 and 10"
+ "(1 to 9 inclusive) can you guess what it is?");
System.out.println("Enter Guess");

if (num1 = num2)
System.out.println(+b(".")+d (" You're Correct the number was:") +num1);
else
difference = num1 - num2;
e = Intabs(difference);
System.out.println("The Computer's Number was: " +num1);
System.out.println("the difference was:" +e);
System.out.println(" better luck next time");
}
}
0
danmaker
1 Solution

Associate Director - Product EngineeringCommented:
>> string f;

char f ; // since it is supposed to hold only one character

>> f = char charAt(z 0);
>> b = toUpperCase (f);

f = a.charAt (0) ;
b = Character.toLowerCase (f) ;

>> if (num1 = num2)

if ( num1 == num2 )

That much should do it.

Mayank.
0

Commented:
Try this:-

import cs1.Keyboard;
import java.util.Random;
import java.lang.Math;

public class Guess {
//*******************************************
//** Simple Guessing Number Game           **
//*******************************************
public static void main(String[] args) {
String a;
String b = a.toUpperCase();

String c;
System.out.println("Please Enter Second Name (lower case)");
String d = c.toUpperCase();

Random generator = new Random();
int num1 = generator.nextInt(10);

System.out.println("I am thinking of a number between 1 and 10" + "(1 to 9 inclusive) can you guess what it is?");
System.out.println("Enter Guess");

int difference, e;
if (num1 == num2)
System.out.println(+b(".") + d(" You're Correct the number was:") + num1);
else
difference = num1 - num2;
e = Math.abs(difference);
System.out.println("Your guess was: " + num2);
System.out.println("The Computer's Number was: " + num1);
System.out.println("the difference was:" + e);
System.out.println(" better luck next time");
}
}
0

Associate Director - Product EngineeringCommented:
In continuity with my last comment:

>>  g = Char.charAt(c 0);
>>  d = toUpperCase(g);

char g, d ;
g = c.charAt (0) ;
d = Character.toLowerCase (g) ;

>> System.out.println ( + b (".") + d ( "You're Correct the number was: " ) + num1 ) ;

System.out.println ( b + a.substring ( 1 ) + " " + d + c.substring ( 1 ) + ". You're correct. The number was: " + num1 ) ;

Mayank.
0

Author Commented:
cheers thanks for all that but for some reason im still haing problems with the code in the last third, my compiler seems to think i havnt intitalised the int"difference"
any suggestions as to why it happens to say that????

if (num1 == num2)
System.out.println ( b + a.substring ( 1 ) + " " + d + c.substring ( 1 ) + ". You're correct. The number was: " + num1 ) ;
else

difference = num1 - num2;
e = Math.abs(k);
System.out.println("The Computer's Number was: " +num1);
System.out.println("the difference was:" +e);
System.out.println(" better luck next time");
0

Associate Director - Product EngineeringCommented:
>> difference = num1 - num2;
>> e = Math.abs(k);

e = num1 - num2 ; // since they atr integers anyway

0

Associate Director - Product EngineeringCommented:
Put:

else
{
e = num1 - num2 ; // since they atr integers anyway
System.out.println("The Computer's Number was: " +num1);
System.out.println("the difference was:" +e);
System.out.println(" better luck next time");
}

with braces { } as shown.

Mayank.
0

Author Commented:
humm as part of my parameters sorry i didnt mention this was that the difference or (integer e) must be presented as an positive value

so thats why the abs command was in there so if the random number was 7 say and the user guessed 9 it would return 2 instead of -2

sorry i didnt mention that before,
0

Associate Director - Product EngineeringCommented:
e = Math.abs ( num1 - num2 ) ;

I hope that you've out the indicated code block in curly braces {}.

Mayank.
0

Associate Director - Product EngineeringCommented:
>> out

I mean, put.
0

Author Commented:
thanks for all the help but thers one final thing

this is my code as it stands now;

//
//*******************************************
//** Guessing Game                         **
//** Version 1.0                           **
//** Last Updated 21/03/2003               **
//** By Dan Maker                          **
//*******************************************

import cs1.Keyboard;
import java.util.Random;
import java.lang.Math;

public class Guess
{
//*******************************************
//** Simple Guessing Number Game           **
//*******************************************
public static void main (String[]args)
{
int b;
String a;
char f;
f = a.charAt (0);
b = Character.toUpperCase (f) ;

int d;
String c;
System.out.println("Please Enter Second Name (lower case)");
char g;
g = c.charAt(0);
d = Character.toUpperCase (f) ;

int num1,num2;
Random generator = new Random();
num1 = generator.nextInt(10);

System.out.println("I am thinking of a number between 1 and 10"
+ "(1 to 9 inclusive) can you guess what it is?");
System.out.println("Enter Guess");

int e,s;
if (num1 == num2)
System.out.println ( b + a.substring ( 1 ) + " " + d + c.substring ( 1 ) + ". You're correct. The number was: " + num1 ) ;
else
{
e = Math.abs ( num1 - num2 );
System.out.println( b + a.substring ( 1 ) + " " + d + c.substring ( 1 ) + "Sorry Your guess wasn't correct");
System.out.println("The Computer's Number was: " +num1);
System.out.println("the difference was:" +e);
System.out.println(" better luck next time");
}

}
}

and this is what it returns;

dan
Please Enter Second Name (lower case)

maker
I am thinking of a number between 1 and 10(1 to 9 inclusive) can you guess what it is?
Enter Guess
5
68an 68akerSorry Your guess wasn't correct
The Computer's Number was: 1
the difference was:4
better luck next time

what it should return is

dan
Please Enter Second Name (lower case)

maker
I am thinking of a number between 1 and 10(1 to 9 inclusive) can you guess what it is?
Enter Guess
5
D.M Sorry Your guess wasn't correct
The Computer's Number was: 1
the difference was:4
better luck next time
any idea why it replaces the first letters of  first and surname with 68 and returns the entire name????? instead of just the initals ??

0

Associate Director - Product EngineeringCommented:
Its printing the ASCII value of the characters. 68 is the ASCII value for 'D'. You have declared b and d as integers, though I'd mentioned that you should declare them as 'char's. Please do so, and your problem will be solved.

If you want to see only the initials, then replace the:

>>             System.out.println ( b + a.substring ( 1 ) + " " + d + c.substring ( 1 ) + ". You're correct. The number was: " + num1 ) ;

with:

System.out.println ( b + "." + d + ". You're correct. The number was: " + num1 ) ;

Mayank.

0

Author Commented:
ahh never mind on that last one i worked that part out for myself

thanks for all the help its now working just as its sposed to

cheers
0

Associate Director - Product EngineeringCommented:
Please change the data-type of the variables 'b' and 'd' to 'char' instead of 'int', and it will work fine. Also, if you're satisfied with the answers, then please rate the question so that it can be PAQed.

Mayank.
0

Associate Director - Product EngineeringCommented:
If you're satisfied with the answers, then please rate the question so that it can be PAQed.
0

Commented:
danmaker:
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is: