Welcome to Dream.In.Code
Become a Java Expert!

Join 150,385 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,139 people online right now. Registration is fast and FREE... Join Now!




help building if else

 
Reply to this topicStart new topic

help building if else

billjava
6 Feb, 2008 - 10:48 AM
Post #1

New D.I.C Head
*

Joined: 6 Feb, 2008
Posts: 1

• Next, the program should present the user with an input box requesting him to type a letter indicating the make (manufacturer) of the car for which the mats are being purchased. Mats-R-Us only sup¬plies mats for three different makes of cars, and those are the options that should be offered. The available options are to be: A = Acura, B = BMW, and M = Mercedes. The program should accept any of these single letters, whether typed in lowercase or uppercase. If the user enters any data other than these three letters, the program should display a message box indicating to the user that the data entered was invalid. Program execution should then end. (Hint: P. 92 in the textbook describes how a program that utilizes the JOptionPane class can be ended.) (Note: In a “real” application program, we would want to let the user try again to input the data correctly but, for now, we will accept this simple approach to dealing with incorrect data entry. There are other places in this program’s execution, described below, where we will make use of this simple program ending approach again.)
• Next, the program should present the user with an input box requesting the user to type a character indicating the model of the car for which the mats are being purchased. The options that are to be presented to the user in this dialog box will depend on the user’s selection of vehicle make in the previous dialog box. For Acura, the model options are to be: X = TSX, R = RL, and T = TL. For BMW, the model options are to be: 3 = 3-Series, 5 = 5-Series, 7 = 7-Series. For Mercedes, the model options are to be: C = C-Class, E = E-Class, and S = S-Class. The program should accept any of these characters, whether typed in lowercase or uppercase. Again, the user is to be presented only the options specific to the car make that he has already selected.

CODE

// Display prompt and get input for mat choice

  JOptionPane.showMessageDialog(null, "There are only three types of mats: A = Acura, B = BMW, and M = Mercedes");
      
   String mat;


    mat=          
JOptionPane.showInputDialog("A = Acura, B = BMW, and M = Mercedes.\n Enter your mat choice:");

         String input;     // To hold the user's input

String (char);
char A = Acura;
char B = BMW;
char M = Mercedes;


      input = JOptionPane.showInputDialog("A = Acura, B = BMW, and M = Mercedes.\n Enter your mat choice:");
  
    

if (A)

JOptionPane.showMessageDialog("You have choosen= Acura");
}}

*edit: Please use code tags in the future, thanks! code.gif


STUCK AT THIS POINT HELP ANYONE???? THANKS NEVER TOOK PROGRAMMING BEFORE!

This post has been edited by Martyr2: 6 Feb, 2008 - 12:10 PM
User is offlineProfile CardPM
+Quote Post

dontKnowJava
RE: Help Building If Else
6 Feb, 2008 - 11:23 AM
Post #2

D.I.C Head
**

Joined: 29 Sep, 2007
Posts: 213


My Contributions
not sure what String(char) means. a char cant hold more than a char so char b = bmw; is not valid. bmw is a string. if(a) is wrong too java cant think you have to tell it exactly what you mean. so if you try something if(input == 'a' || input == 'A') that should work better
User is offlineProfile CardPM
+Quote Post

DillonSalsman
RE: Help Building If Else
6 Feb, 2008 - 12:12 PM
Post #3

D.I.C Head
Group Icon

Joined: 30 Oct, 2007
Posts: 72


Dream Kudos: 50
My Contributions
I think that if you have never taken programming before you shouldn't be using graphics yet...
Did you start at the beggining of the book?
Most books teach you everything you need to do the exercises...

If not I would go through this tutorial:
http://www.myflex.org/books/JavaKid8x11.pdf
CODE

curtosy of:
http://www.faratasystems.com/


It is a really good tutorial that taught me alot.
Hope it helps you as much as it did me wink2.gif
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Help Building If Else
6 Feb, 2008 - 12:39 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Here is a great sample program for you to build off of. It shows you how to setup the collection of the data and validation. With this you can then build up the models choices in each if statement and move on from there.

CODE

import javax.swing.JOptionPane;

public class buymats {
    public static void main(String args[]) {

        // Introduce them to the program and give them the input dialog to ask for a choice.
        JOptionPane.showMessageDialog(null,"Welcome to the mat purchasing program.");

        // Their choice will be returned as a string, store it in "carmake"
        String carmake = JOptionPane.showInputDialog("Please enter a car make (A = Acura, B = BMW, and M = Mercedes)");

        // Lets capitalize it for better comparing.
        carmake = carmake.toUpperCase();
    
        // Evaluate the value and see if it is equal to the value
        if (carmake.equals("A")) {
            JOptionPane.showMessageDialog(null,"You have choosen= Acura");
        }
        else if (carmake.equals("B")) {
            JOptionPane.showMessageDialog(null,"You have choosen= BMW");
        }
        else if (carmake.equals("M")) {
            JOptionPane.showMessageDialog(null,"You have choosen= Mercedes");
        }
        else {
            JOptionPane.showMessageDialog(null,"Invalid make. Program exiting.");
        }
            
    }
}


Read the in code comments to see what I have done and how it works. Just by looking at it you should be able to see how to build your models choices for each choice they made.

Enjoy!

"At DIC we be make and model coding code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

DillonSalsman
RE: Help Building If Else
7 Feb, 2008 - 06:16 AM
Post #5

D.I.C Head
Group Icon

Joined: 30 Oct, 2007
Posts: 72


Dream Kudos: 50
My Contributions
You know.. I never thought of using .toUpperCase(); ... shame on me.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:51PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month