Solved

SIMPLE ROUTINE

Posted on 1998-02-05
2
226 Views
Last Modified: 2010-04-16
I have written a simple java program which does not appear to work, can anybody help...

class cat {
  public static void main(String args[])
     {
     char a,b;
     int d,e;
     d =(int)(Math.random()*3+65);
     e =(int)(Math.random()*3+65);
     a=(char)(d);
     b=(char)(e);
     System.out.println(a);
     System.out.println(b);
     if ((a=='c') && (b=='a'))
     System.out.println("OK");
     else
     System.out.println("NOT OK");
     }
  }

This generates two random characters and then prints a message accordingly, however if the characters are 'c' and 'a' then the display does not show the 'OK' message as expected.

Any suggestions.
0
Comment
Question by:RICHARDH
[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
2 Comments
 
LVL 4

Accepted Solution

by:
russgold earned 20 total points
ID: 1232886
Your code generates upper case letters, but you are testing for lower case letters.

Instead of:

if ((a=='c') && (b=='a'))

you need:

if ((a=='C') && (b=='A'))
0
 

Author Comment

by:RICHARDH
ID: 1232887
Thanks, it was so obvious.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

691 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