Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-13-2007, 11:48 PM
Member
 
Join Date: Jul 2007
Posts: 44
susan is on a distinguished road
Java Looping and decision
I'm trying to do a simple program where a user inputs a String "course code" (predefined in an array) and then a loop searches to see what time the input course is at and displays the date/time for that particular course. I also want it to display an error if they input anything that doesn't match.

The problem is that it just loops through the whole array every time and isn't really dependent on the 'if else' for some reason I can't figure out.

Code:
import javax.swing.*; public class ScheduleJ2 { public static void main(String[] args) { String courseList[][]={{"COM101","Mon 08:00"}, {"COM201","Tue 09:00"}, {"COM301","Wed 11:00"}, {"COM315","Thu 14:00"}, {"COM401","Fri 16:00"},}; String courseSelect = JOptionPane.showInputDialog(null, "Enter the Course Code to view the course date and time:\nCOM101\nCOM201\nCOM301\nCOM315\nCOM401"); for(int i = 0; i<courseList.length;i++) { if(courseList[i][0].equalsIgnoreCase(courseSelect)) JOptionPane.showMessageDialog(null, "The course you selected is scheduled for: " + courseList[i][1]); else courseSelect = JOptionPane.showInputDialog(null, "Error - Course does not exist.\nPlease re-enter your course code:\nCOM101\nCOM201\nCOM301\nCOM315\nCOM401"); } } }
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 06:29 AM
Member
 
Join Date: Jul 2007
Posts: 40
cachi is on a distinguished road
You can do this in one of two ways in Java. There are break statements and using boolean statements you can take advantage of.

Break statements will break out of a loop right when it gets to the statement. So if we were to do
Code:
int i = 0; while(true) { if(i > 3) break; i++ }
This loop would break when I becomes greater then three.

That is one way. The other way to separate execution would be to use a boolean variable to keep track of what is going on when. For example

Code:
boolean found = false; for(int i = 0; i < somelist.length() && !found; i++) { if(something.equals(somethingelse)) found = true; } if(!found) System.out.println("Not Found"); else System.out.println("Found");
Greetings.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
looping a function Username New To Java 2 07-30-2007 07:37 PM


All times are GMT +3. The time now is 12:20 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org