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
  • Last Modified:

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;
        System.out.println("Welcome 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");
    }
}
0
danmaker
Asked:
danmaker
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
>> string f;

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.
0
 
Ajay-SinghCommented:
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("Welcome 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");
     }
}
0
 
Mayank SAssociate 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
danmakerAuthor 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("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");
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> difference = num1 - num2;
>> e = Math.abs(k);

e = num1 - num2 ; // since they atr integers anyway
   
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
danmakerAuthor 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
 
Mayank SAssociate Director - Product EngineeringCommented:
e = Math.abs ( num1 - num2 ) ;

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

Mayank.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> out

I mean, put.
0
 
danmakerAuthor 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;
        System.out.println("Welcome 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 ??

0
 
Mayank SAssociate 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
 
danmakerAuthor 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
 
Mayank SAssociate 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
 
Mayank SAssociate Director - Product EngineeringCommented:
If you're satisfied with the answers, then please rate the question so that it can be PAQed.
0
 
CleanupPingCommented:
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.
0
 
jimmackCommented:
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
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Its high time.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now