Code bug help for a beginner!!

Ok, I've written this code so far. Everything was fine until I got to the IF condition. When I try compiling it it tells me that the 'a' variable in the IF condition expression may not have been initialised, which I don't really understand as it's been declared at the start of the program and even used fine beforehand in the switch statements.
Any ideas?
Thanks for any help. Here's the code.
import javax.swing.JOptionPane;
public class LetterProgram {
public static void main (String args[]) {
   String pickALet;
   String output;
   char letter;
   int guess;
   int a;
   pickALet = JOptionPane.showInputDialog ("please enter a lower case letter from (a to e)");
   output = "you selected a letter " + pickALet;
   letter = pickALet.charAt(0);
   switch (letter) {
   case 'a':
   a = 1;
   output = "you entered an a";
   break;
   case 'b':
   a = 2;
   output = "you entered a b";
   break;
   case 'c':
   a = 3;
   output = "you entered a c";
   break;
   case 'd':
   a = 4;
   output = "you entered a d";
   break;
   case 'e':
   a = 5;
   output = "you entered an e";
   break;
   default:
   output = "i did not recognise your entry";
   break;
   JOptionPane.showMessageDialog(null, output, "your letter", JOptionPane.INFORMATION_MESSAGE);
   guess =(int)(Math.random()*5 +1 ) ;
   System.out.println(guess);
   if (guess==a)
      System.out.println("The computer guessed the correct letter");
   else
      System.out.println("The computer guessed the letter incorrectly");
   System.exit(0);

It's better to add a default label to the switch; otherwise for most cases you will be setting the value twice, and it makes the intent of 'none of the above' clearer.
Pete

Similar Messages

  • CS3 - Classroom in a Book - Help for real beginner please!

    I'm literally only on lesson 4 of this course so please forgive my ignorance!  Using the Quick Selection Tool the course tells me to "  Choose Edit > Copy and Edit > Paste to paste the selection on a new layer."  I get a message which says "could not complete the Copy command because the selected area is empty".
    I'm sure there is a simple explanation but I just can't find it - please can anyone help this pure beginner from going entirely mad?
    Judy

    Is the layer that you want to copy from the active layer?  In the example below I used the quick selection tool to select some of the image (you can see the "marching ants" in the lower right corner), but since a new empty layer is active and not the layer I want to copy from, the error message you describe is displayed.

  • Help for a Beginner's Program

    Hello, I'm fairly new to the Java language. Recently I was writing a program revolving around the creation of classes and constructors...my code looks like this:
    import java.util.Scanner;
    class Money{
         private static int dollars, cents;
         Money(int dollars, int cents){
         Money(int dollars){
         Money(){
         static Money add(Money n,Money n2){
              return (n + n2);
         static Money minus(Money n,Money n2){
              return (n + n2);
         public static boolean equals(int m, int m2){
              if((m == dollars) && (m2 == cents));
                   return true;
         public String toString(){
              return ("Dollars = "+ getDollars() + " " + "Cents =" + getCents());
         public static int getDollars(){
              return (dollars);
         public static int getCents(){
              return (cents);
         public static void setDollars(int n){
              dollars = n;
         public static void setCents(int n2){
              cents = n2;
    class Program2_5{
         public static void main (String [] a) {
              Scanner kb= new Scanner (System.in);
              System.out.println ("Enter the amount of dollars");
              int d=kb.nextInt();
              System.out.println ("Enter the amount of cents");
              int c=kb.nextInt();
              Money.setDollars(d);
              Money.setCents(c);
              System.out.println (Money.toString());
    The main part of my code is just to simply make sure the methods actually work. When i go to compile my code I get three errors revolving around three specific lines.
    Error 1= {Operator + cannot be applied to Money, Money}
    *static Money add(Money n,Money n2){*+
    *     return (n n2);*+
    Error 2= {Same as Error 1}
    *static Money minus(Money n,Money n2){*+
    *     return (n n2);*+
    Error 3={non-static method toString() cannot be referenced from a static context}
    *public String toString(){*+
    *     return ("Dollars = " getDollars() + " " + "Cents =" + getCents());*+
    In order to fix Error 3 i tried to place static in front of String toString() but got an error about the Money String toString() cannot override the Object StringtoString()
    I have been working with this program for hours on and off and would really appreciate it if someone with more experience could help me...Thank You

    I think there are a few things that could help you out. Firstly, amounts are generally not looked at as dollars and cents separately. You can always go back and change it, but I'd code it a bit more like this:
    import java.util.Scanner;
    public class Money {
      private double amount;
      public Money() {
        this(0.0);
      public Money(double amount) {
        setAmount(amount);
      public void setAmount(double amount) {
        this.amount = amount;
      public double getAmount() {
        return(amount);
      public void add(double amount) {
        setAmount(getAmount()+amount);
      public void minus(double amount) {
        setAmount(getAmount()-amount);
      public boolean equals(double amount) {
        if (getAmount() == amount)
          return true;
        return false;
      public String toString() {
        return ("Amount = "+getAmount());
      public static void main (String [] argv) {
        Scanner kb= new Scanner (System.in);
        System.out.println("Enter initial amount");
        double a=kb.nextDouble();
        Money money = new Money(a);
        System.out.println(money.toString());
        System.out.println("Enter amount to add");
        a=kb.nextDouble();
        money.add(a);
        System.out.println(money.toString());
        System.out.println("Enter amount to subtract");
        a=kb.nextDouble();
        money.minus(a);
        System.out.println(money.toString());
        System.out.println("Enter amount to compare");
        a=kb.nextDouble();
        System.out.println(money.equals(a));
    }Hope it helps you out.

  • Help for a beginner in utl_file

    Hi,
    As I said I have just begun to learn pl/sql and looks like I am having a big trouble with it. I will attach the code I started with I know it does not do much and useful but can someone please help me run it? When I try to run it it gives the compilation error invalid directory path...
    CREATE OR REPLACE PROCEDURE encrypt_to_file IS
    /* Output variables to hold the result of the query: */
    a ALL_OBJECTS.OWNER%TYPE;
    b ALL_OBJECTS.OBJECT_NAME%TYPE;
    vlocation VARCHAR2(16) := 'OUTPUT';
    vopen_mode VARCHAR2(16) := 'w';
    F1 UTL_FILE.FILE_TYPE;
    /* Cursor declaration: */
    CURSOR TCursor IS
    SELECT OWNER, OBJECT_NAME
    FROM ALL_OBJECTS;
    BEGIN
    OPEN TCursor;
    F1 := utl_file.fopen( vlocation, 'DENEME.TXT', vopen_mode );
    LOOP
    /* Retrieve each row of the result of the above query
    into PL/SQL variables: */
    FETCH TCursor INTO a, b;
    /* If there are no more rows to fetch, exit the loop: */
    EXIT WHEN TCursor%NOTFOUND;
    END LOOP;
    UTL_FILE.FCLOSE(F1);
    /* Free cursor used by the query. */
    CLOSE TCursor;
    END encrypt_to_file;
    /

    OK, so someone has created a directory object for you.
    What do you mean by "I don t get the error with its last condition as procedure"? Are you saying that you no longer get the error when running your procedure?
    Are you looking in C:\Temp on the machine where Oracle is running for the file?
    In the code you posted, you are not writing anything to the file. Can you add a UTL_FILE.PUT (or PUT_LINE) to ensure that there is something in the file?
    Justin

  • Report Painter Help for a Beginner

    Hi All
    I am very new to FI and even SAP - can any one help with the tips for creating a report through Report Painter- I have gone through the online helps but not able to pick up, please help to create a simple G/L Report or some other reports for learning purpose
    Advance thanks for the help
    Thanks again
    Srini

    Hi all
    Please note I have a problem, my client want to create a same cost centers for all company codes (8 company codes) in one controlling area (all company codes linked with one controlling area), but the cost center master data is not accepting the same cost center for the different company code in the same controlling area- client don't want to create a new  cost centers since they are using the same cost centers for years in Peoplesoft, how to tackle this problem- anyway we can suppress the company code in the cost center master data?- &  if we supperss also the final report will be Ok?- Pls help
    Thanks
    Srini

  • Help for a beginner creating a 2d image-processing suite in LabVIEW.

    I am a final year student and currently seeking any advice from experienced engineers with regard to my project.
    The project is to create a LabVIEW based image-processing suite, which would be able to import 2d arrays of data from microscope, plus other information (scaling, date/time, sample info) and perform the following functions:
    Visualisation – 2d & 3d, Cross-section Profile Analysis, Histogram, Fourier Transform, Auto-Correlation, Copy, Print, Save & Export, Colour Palette Edit, Zoom, Manipulate.
    Having not used LabView previously, any examples or information you may have would be of extreme use,
    Thank you in advance for any help you can give me,

    Sam,
    Most of these tools are readily available in LabVIEW when you purchase the IMAQ Vision toolkit. You just need to learn how to use them and set them up. In your case, I would strongly recommend starting with Vision Builder. You can learn how to use a number of these tools, and it may actually be sufficient for your project without writing any code. It can generate LabVIEW code as well, so it would get you started on your program.
    Bruce
    Bruce Ammons
    Ammons Engineering

  • Need source code or help for COMM_FAILURE 209

    Hi,
    Anyone know where I can get the source code for the com.sun.jndi.cosnaming package? I'm trying to debug this message WARNING: "IOP00410209: (COMM_FAILURE) Connection rebind". Only get this problem when connecting to solaris, fine on windows.
    We run tnameserv and jdk1.4.2 on both. Also tried using jacorb, get same problem. The ORB's don't return any errors, error only on client side. Client is also jdk1.4.2 running on windows.
    Ted.

    Sorry,
    but nobody here will do your homework for you. If you have problems with your code let us know and we try to help, but never ask for source code.
    Phil

  • Match Codes / Search Helps for Duplicate Check Vendor are missing

    Hi Guys,
    I'm trying to enable the Duplicate Check for the Vendor solution we have here and I couldn't help, but notice that when I'm setting up the DB Search in "Define Search Application", the out of the box values for existing search helps are already preconfigured and the values are the following:
    BP MC_BP_ADDRESS Address Data
    BP MC_BP_BANK_DETAIL Bank Details
    BP MC_BP_GENERAL General Data
    BP MC_BP_ID_NUMBER Identification Numbers
    BP MC_BP_ROLES BP Roles
    Unfortunately, when I check in SE11 - those search helps do not exist in the system. How can I get them deployed? Are they part of a Business Set that I missed to install and activate?
    Thanks in advance,
    Boris

    Hi Boris
    Please check that the following is set in your system:
    Configure Duplicate check for entity types - BP need the following:
    Assign search object connector template to object types - BP need the following:
    Ensure that "Business Partner template for MDG" connector is created in ESH_COCKPIT
    Regards
    Danie

  • Help for a beginner

    I AM USING ORACLE DEVELOPER2000 R2.1 (STILL STUDYING IT)AND FIRST I INSTALLED ORACLE 8 PERSONAL EDITION AS A DATABASE ENGINE.
    THE PROPLEM IS THAT I HAD THE PROGRAM READ THE TABLES 'SUMMIT2' FOR THE USER : scott AND THE PASSWORD: tiger. THEN I MAKE IT READ THE TABLES ' SUMMIT2A ' FOR THE USER user1 AND THE PASSWORD : user1.
    THAT WAS USING system, manager. I CAN NOW LOG IN USING SCOTT BUT WHEN I LOG IN USING USER1 IT GIVES ME AN INVALID USER NAME AND PASSWORD.
    I AM USING WIN98. PLEASE HELP...

    you can also invoke any script with a ./<name_of_command>
    for example: ./orainst.sh
    There are also tons of manual pages on how the shell works
    just type: man<name of command you need help with>
    Kevin
    keith barron (guest) wrote:
    : stelar (guest) wrote:
    : : tanks for you help
    : : i have another question.
    : : why i login as root
    : : but cant run oratab.sh?????????????
    : : thanks your frined :stelar
    : : Jim Wartnick (guest) wrote:
    : : : stelar (guest) wrote:
    : : : : hi friends:
    : : : : i download oracle.but cant tar the 805ship.tar,
    : : : : but i cant find the install in orainst directory?
    : : : : why?
    : : : : help me . give me some advice.
    : : : : did i dont full download
    : : : : your friend:stelar
    : : : You should be looking for the file: "orainst" in the
    : : "orainst"
    : : : directory. This file is the executable to install Oracle
    : : 8.0.5.
    : : : It should be there.
    : You should include whatever messages you are getting whenever
    you
    : have a problem (or say there are no messages). My guess is that
    : you are not familiar w/ running a shell script. Are you
    : specifying "orainst.sh". If that doesn't work try (I'm not at
    my
    : linux machine so I'm going on memory) "/usr/bin/sh orainst.sh".
    : If it works, add "/usr/bin" to the path statement in your
    profile
    : (/root/.bash_profile).
    null

  • Selecting code bug/wishlist (for FlexBuilder 3 Dev team)

    When i try to select a word in the mxml code by double click
    on it, sometimes can't select the hole word. I'm talking about
    spanish words in a string or a comment line. by example this string
    below (paste it in flexbuilder 3 and try to select the
    "modificación" word by double click on it... i will be
    selected only before the "ó" character):
    I hope that can be fixed in the final release of Flex
    Builder

    Hi mxmotion!
    Please log this bug in the public Flex bugbase at
    http://bugs.adobe.com/flex/
    Thanks!
    Mac

  • Help for a beginner please -using iTunes

    Using iTunes for the first time, I buy and download an item. It`s a piece of music that lasts 55 mins.OK.The stuff comes down the line, and I can make it play on my scratchy screen-speakers.
    I then try to copy it to a DVD with my DVD writer. I`m warned it may take up to an hour. No indication of progress is given. After a few minutes there`s a xylophone-bong. Maybe it means the process failed. Maybe succeeded. I don`t know. No indication of either.The disc won`t play so I suppose I failed.
    A make a second try, on to a CD. This time no Burn - button appears on the screen, so I`m stuck straight away. Is Apple stopping me making a second copy, so I have to spend another ten quid downloading the thing again ? A pretty severe penalty for a mistake !
    Can anyone put me right, and maybe direct me to an elementary tutorial ?
    Eigenblog
    HP T3640 desktop   Windows XP  

    Making a DVD only creates a backup copy of a track. It does not create a disk that can be played in any other player. If you want to create a disk that can be played in other players, you need to burn an audio CD, so check your iTunes -> Advanced -> Burning preference and make sure it's set to Audio CD. Then you need to put the track into a playlist. It can be the only track in the playlist, but it must be in a playlist before it will burn.
    For more information, this tutorial may help:
    iTunes: Burn Your Music To CD
    Hope this helps.

  • A help for a beginner

    i just downloaded JMF on a windowxp OS ( i also downloaded java2sdkl1.4.2_02 ).
    I added the lib and bin directories to the PATH in the systems environment variables and i checked that the jmf.jar and sound.jar are created in the CLASSPATH user environment variables.
    My first question : should i add anything else to my environment variables ?
    i am trying to make an rtp session between two pcs and transmit audio files between the two terminals.
    i followed many methods and all of them failed, but i don't know the reason behind the failure .
    first, i opened an rtp session on the receiver side with the ip address of the transmitter and the port and was waiting for data.
    on the transmitter side , i have chosen an audio file to transmit by the transmit option at the ip address of the receiver and the same port .
    the result was :
    when i tried to transmit an mp3 file, the receiver side have this error :
    Controller error:
    Internal Module com.sun.BasicRendereModule@16aa42e: failed to handle a data format change.
    when i tried to transmit a midi file, the transmitter side have this error :
    Failed to create processor.
    Exception.
    Cannot find a processor for : com.sun.media.protocol.file
    DataSource@57e787
    second method, i transmit the audio file by the transmit option.
    on the receiver side, i opened the URL address : rtp:// 192.168.10.10/40000/audio/1/4
    192.168.10.10 is the ip address of the transmitter side
    40000 the port number same at both sides.
    audio 1 is the number of the track.
    4 is the value of ttl.
    the result is that the receiver opened a player but did not hear anything.
    i also tried the methods described at JMFsolutions : AVReceive2/3, AVTransmit2/3 aslo they failed.
    Last thing, when i try to add a plugin for the codec or renderer or anything else on the registry editor, i receiver the following error:
    Could not add item.
    Hope someone will help me to solve the problems.

    Any one found out the answer ? I get the same damn error ...
    RTP Handler internal error: javax.media.ControllerErrorEvent[s
    a.content.unknown.Handler@19616c7,message=Internal module com.
    dererModule@f0eed6: failed to handle a data format change!]
    thanks in advance for helping me out ...

  • Some array based help for a beginner needed.

    Hello there.
    I have an array, initialised like..
    int numbers[] = {12, 15, 67, 18, 29, 40, 23, 4, 59, 5 } ;
    and wish to print out a line, which has the sum of these numbers, and the average of them.
    any help will be greatly appreciated.
    Thanks

    So what's the problem?
    How would you do it manually? Write that down in very simple, precise, small steps. Then translate those steps into Java code. Post your best effort and specific questions when you get stuck.

  • Help for absolute beginner

    I am very new to computing and wish to learn more. Would like to learn Java and have access to net. Could download from site , but would then need my hand held for installation. If anybody is prepared to get me started please reply. I will not be offended by any answers (especially helpful ones!) Regards Steve

    I am very new to computing and wish to learn more.
    Would like to learn Java and have access to net. Could
    download from site , but would then need my hand held
    for installation. If anybody is prepared to get me
    started please reply. I will not be offended by any
    answers (especially helpful ones!) Regards SteveThis is latest on Sun site. might be helpful
    http://developer.java.sun.com/developer/onlineTraining/new2java/divelog/?frontpage-banner
    cheers!!

  • Printing from ipad - help for a beginner please

    I have downloaded the "Print to All.." app into my ipad 1.  I have an HP Photosmart Premium wireless printer. How do I print an e-mail?

    Have you consulted the Help pages?
    http://mobile.eurosmartz.com/help/help_index.html (assuming that's the correct app).
    However, if it is the correct app, the second line of the description in the AppStore says:
    "if you want to print email, docs, web pages or calendar etc, look at our other app "Print n Share""

Maybe you are looking for

  • E-MU USB 0404, what the hell is taking so long with Windows 7 drivers?

    Windows 7 beta was out ages ago, the RC has been out for some time. WTF is taking so long? Someone want to tell the developers I have a $200 USB audio device that I can't use with my operating system and to get off their lazy asses and put at least a

  • Radius of Apple TV 3 corners

    Can anybody help with the radius of Apple TV 3 corners in mm? Thanks David

  • Configuring OpenSSO Web Service Security on Tomcat

    Dear All, I want to configure a simple OpenSSO Web Service security for my application. My web service is on Tomcat and I have used JAX-WS. Please let me know if there is something we can develop just by configuring OpenSSO properties, instead of wri

  • Why TIF images loaded stay in DONE status??

    Hi: When I loaded images en IPM 11GR1, this images stay in DONE status.....and i can see public......the images not in RELEASE status please help me.

  • Safari 3.1 hanging shortly after 10.5.3 update

    Hi, Week old iMac, updated to 10.5.3 no problem, everything worked fine, but a day later and Safari is hanging on startup. I get the Safari window, but nothing loads and it just hangs the entire system. Have run Safari from a new account and it works