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

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




starting another set of instructions

 
Reply to this topicStart new topic

starting another set of instructions, how to I get my code to do one thing then start another?

Mastergeek666
16 Aug, 2007 - 02:44 PM
Post #1

D.I.C Head
Group Icon

Joined: 10 Aug, 2007
Posts: 120


Dream Kudos: 75
My Contributions
My question is how can I ask if you want to play the game then start my other code that tells whether the letter is a vowel or not. Basically start doing one thing and when its done start the other. The problem isn't that the code is bad because my program runs and asks if I want to play the game, its just that isn't running then going to my other code. Also if anyone could give tips on how to put all of this in a little window or GUI.


import cs1.Keyboard;
public class anotherTest {

public static void main(String[] args)
{
String[] Yes = {"Yes", "yes"};
String[] No = {"No", "no"};

String start;
boolean found = false;
System.out.println ("Would you like to play a game? Yes/No");
start = Keyboard.readString();

for (int index = 0; index < Yes.length; index++)
if (start.equals(Yes[index]))
found = true;
else (start.equals(No[index]))
found = false
if (found)
System.out.println (start + ", Then shall we begin.");
else
System.out.println (start + ", Your loss.");
}

{

String[] begin = {"shall we begin."};
String[] letters = {"A", "a", "E", "e", "I", "i", "O", "o", "U", "u"};

for (int index = 0; index < letters.length; index++)
if (after.equals(begin[index]))

String after;
boolean found1 = false;
System.out.println ("Please enter a letter from A-Z: ");
after = Keyboard.readString();

for (int index = 0; index < letters.length; index++)
if (after.equals(letters[index]))
found1 = true;
if (found)
System.out.println (letters + " is a vowel.");
else
System.out.println (letters + " is a constant.");

}
}

This post has been edited by Mastergeek666: 16 Aug, 2007 - 02:55 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Starting Another Set Of Instructions
16 Aug, 2007 - 07:24 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



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

My Contributions
There appears to be a few syntax issues with your code. I noticed that a few of your "for loops" don't have opening curly braces and one of your statements found = false is missing the semicolon. Your printlns also need to be changed from letters to something like letters[index]. That way it will display each character at a time like I am sure you meant for it to do.

But as far as your first question it really depends what you want it to do. Normally when you ask for input from the user it will cause the program to pause execution until they enter a value, then it will process that value and call other parts of the program to execute.

Other times you require the program to continue in an endless loop until the user does something to break the loop or change how the loop behaves (this is how some games work).

I think you will need to clarify further what you mean by "..going to my other code". What is that other code? A function? A separate file?

As for making GUI interfaces, you can go two different routes. You can use AWT which is for the more simple components without all the flashiness or you can go with Swing which gives you those pretty buttons and stuff.

I recommend swing because everything looks nice when you are done. Below is a nice site to showing you the start of swing. You can find tons of more articles on the net by typing "java swing tutorial" into Google.

Java GUI tutorial - Swing basics

Hope you find it useful. If you get stuck, feel free to come back here and we can work out any other errors you get.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Starting Another Set Of Instructions
16 Aug, 2007 - 07:57 PM
Post #3

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,121



Thanked: 76 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE

String[] letters = {"A", "a", "E", "e", "I", "i", "O", "o", "U", "u"};


You can use toupper to keep the size of the array down.
User is online!Profile CardPM
+Quote Post

alpha02
RE: Starting Another Set Of Instructions
16 Aug, 2007 - 09:12 PM
Post #4

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687


Dream Kudos: 850
My Contributions
I'd say:

-Use swing for your GUI (see my tutorial in the Java Tutorials section)
-Always put opening curly braces in your "if", "for", "while", ... blocks, even if the block is only a single line. This is a good habit to take.
User is offlineProfile CardPM
+Quote Post

Mastergeek666
RE: Starting Another Set Of Instructions
17 Aug, 2007 - 10:10 AM
Post #5

D.I.C Head
Group Icon

Joined: 10 Aug, 2007
Posts: 120


Dream Kudos: 75
My Contributions
QUOTE(Martyr2 @ 16 Aug, 2007 - 08:24 PM) *

There appears to be a few syntax issues with your code. I noticed that a few of your "for loops" don't have opening curly braces and one of your statements found = false is missing the semicolon. Your printlns also need to be changed from letters to something like letters[index]. That way it will display each character at a time like I am sure you meant for it to do.

But as far as your first question it really depends what you want it to do. Normally when you ask for input from the user it will cause the program to pause execution until they enter a value, then it will process that value and call other parts of the program to execute.

Other times you require the program to continue in an endless loop until the user does something to break the loop or change how the loop behaves (this is how some games work).

I think you will need to clarify further what you mean by "..going to my other code". What is that other code? A function? A separate file?

As for making GUI interfaces, you can go two different routes. You can use AWT which is for the more simple components without all the flashiness or you can go with Swing which gives you those pretty buttons and stuff.

I recommend swing because everything looks nice when you are done. Below is a nice site to showing you the start of swing. You can find tons of more articles on the net by typing "java swing tutorial" into Google.

Java GUI tutorial - Swing basics

Hope you find it useful. If you get stuck, feel free to come back here and we can work out any other errors you get.

//-------------------------------------------------------------------------------------------------------------------------------
thx for the help what I'm trying to get it to do is to ask if you want to play the game and then if you type yes then it will ask you to enter a letter from a-z.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 07:40PM

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