?
Solved

CharAt command

Posted on 2003-03-24
18
Medium Priority
?
682 Views
Last Modified: 2012-08-13
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
Comment
Question by:danmaker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
18 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 8193930
>> 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
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 8193944
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8193985
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!

 

Author Comment

by:danmaker
ID: 8194018
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8194034
>> difference = num1 - num2;
>> e = Math.abs(k);

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

Expert Comment

by:Mayank S
ID: 8194038
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
 

Author Comment

by:danmaker
ID: 8194068
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8194077
e = Math.abs ( num1 - num2 ) ;

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

Mayank.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8194079
>> out

I mean, put.
0
 

Author Comment

by:danmaker
ID: 8194117
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8194137
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 Comment

by:danmaker
ID: 8194143
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
 
LVL 30

Accepted Solution

by:
Mayank S earned 300 total points
ID: 8194155
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8194226
If you're satisfied with the answers, then please rate the question so that it can be PAQed.
0
 

Expert Comment

by:CleanupPing
ID: 9058523
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
 
LVL 15

Expert Comment

by:jimmack
ID: 9731912
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 9738219
Its high time.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question