danmaker
asked on
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,differen ce,e,z;
String a;
System.out.println("Welcom e to Guessing Game please enter your first name (lower case)");
a = Keyboard.readString ();
string f;
f = char charAt(z 0);
b = toUpperCase (f);
String c;
System.out.println("Please Enter Second Name (lower case)");
c = Keyboard.readString ();
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");
num2 = Keyboard.readInt();
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("Sorry Your guess wasn't correct");
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");
}
}
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,differen
String a;
System.out.println("Welcom
a = Keyboard.readString ();
string f;
f = char charAt(z 0);
b = toUpperCase (f);
String c;
System.out.println("Please
c = Keyboard.readString ();
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");
num2 = Keyboard.readInt();
if (num1 = num2)
System.out.println(+b(".")
else
difference = num1 - num2;
e = Intabs(difference);
System.out.println("Sorry Your guess wasn't correct");
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");
}
}
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;
System.out.println("Welcom e to Guessing Game please enter your first name (lower case)");
a = Keyboard.readString();
String b = a.toUpperCase();
String c;
System.out.println("Please Enter Second Name (lower case)");
c = Keyboard.readString();
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 num2 = Keyboard.readInt();
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("Sorry Your guess wasn't correct");
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");
}
}
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;
System.out.println("Welcom
a = Keyboard.readString();
String b = a.toUpperCase();
String c;
System.out.println("Please
c = Keyboard.readString();
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 num2 = Keyboard.readInt();
int difference, e;
if (num1 == num2)
System.out.println(+b(".")
else
difference = num1 - num2;
e = Math.abs(difference);
System.out.println("Sorry Your guess wasn't correct");
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");
}
}
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.
>> 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.
ASKER
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("Sorry Your guess wasn't correct");
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");
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("Sorry Your guess wasn't correct");
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");
>> difference = num1 - num2;
>> e = Math.abs(k);
e = num1 - num2 ; // since they atr integers anyway
>> e = Math.abs(k);
e = num1 - num2 ; // since they atr integers anyway
Put:
else
{
e = num1 - num2 ; // since they atr integers anyway
System.out.println("Sorry Your guess wasn't correct");
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");
}
with braces { } as shown.
Mayank.
else
{
e = num1 - num2 ; // since they atr integers anyway
System.out.println("Sorry Your guess wasn't correct");
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");
}
with braces { } as shown.
Mayank.
ASKER
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,
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,
e = Math.abs ( num1 - num2 ) ;
I hope that you've out the indicated code block in curly braces {}.
Mayank.
I hope that you've out the indicated code block in curly braces {}.
Mayank.
>> out
I mean, put.
I mean, put.
ASKER
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;
System.out.println("Welcom e to Guessing Game please enter your first name (lower case)");
a = Keyboard.readString ();
char f;
f = a.charAt (0);
b = Character.toUpperCase (f) ;
int d;
String c;
System.out.println("Please Enter Second Name (lower case)");
c = Keyboard.readString ();
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");
num2 = Keyboard.readInt();
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("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");
}
}
}
and this is what it returns;
Welcome to Guessing Game please enter your first name (lower case)
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
Your guess was: 5
The Computer's Number was: 1
the difference was:4
better luck next time
what it should return is
Welcome to Guessing Game please enter your first name (lower case)
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
Your guess was: 5
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 ??
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;
System.out.println("Welcom
a = Keyboard.readString ();
char f;
f = a.charAt (0);
b = Character.toUpperCase (f) ;
int d;
String c;
System.out.println("Please
c = Keyboard.readString ();
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");
num2 = Keyboard.readInt();
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("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");
}
}
}
and this is what it returns;
Welcome to Guessing Game please enter your first name (lower case)
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
Your guess was: 5
The Computer's Number was: 1
the difference was:4
better luck next time
what it should return is
Welcome to Guessing Game please enter your first name (lower case)
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
Your guess was: 5
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 ??
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.
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.
ASKER
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
thanks for all the help its now working just as its sposed to
cheers
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you're satisfied with the answers, then please rate the question so that it can be PAQed.
danmaker:
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
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:
Accept mayankeagle's comment as answer.
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
jimmack
EE Cleanup Volunteer
I will leave a recommendation in the Cleanup topic area that this question is:
Accept mayankeagle's comment as answer.
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
jimmack
EE Cleanup Volunteer
Its high time.
Please make it:
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.