PDA

View Full Version : Java Looping and decision making question


a4udi
12-10-2006, 12:55 AM
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.

Here is the code... 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. I'm extremely new at this, so any help is appreciated.

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");
}

}
}

Mink
12-10-2006, 10:34 AM
Lets take a look at what your loop is doing.

First you have the user input a course to search for. Lets say the user selects "COM101". So you drop into the body of the loop and it finds it right off the bat, it outputs the time that it is scheduled, and then what does it do? It loops again. It looks like you need an exit case to stop the loop once a course has been located.

Also, you are checking if a course is invalid upon each iteration. A course is invalid if you have looped through the entire array without finding a match.

a4udi
12-10-2006, 11:00 PM
It looks like you need an exit case to stop the loop once a course has been located
Which is exactly what I don't know how to do...

Aradon
12-11-2006, 01:58 AM
Which is exactly what I don't know how to do...

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 get's to the statement. So if we were to do


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


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");


Hope that helps!

a4udi
12-11-2006, 02:44 AM
thanks very much!
I had tried the method using a boolean statement, but I must have done it wrong. I'll give it another go with those tips. Thanks a lot!

Gox
12-11-2006, 08:33 AM
My suggestion is to go with Aradon's second example.

Using break statements (as well as goto's) is considered to be bad style.

Aradon
12-11-2006, 05:23 PM
Very true. Break and Continue statements are indeed in bad style and can lead to some rather questionable code. So the second one would probably be better for aesthetics (my word of the day) as well as future knowledge of boolean representation.

Thanks for pointing that out Gox :thumbsup:

Brandoe85
12-11-2006, 05:42 PM
I don't know that I'd go as far as saying the break statement is bad practice, in fact, I don't agree with it at all. This really comes down to personal preference in the long run if you ask me.

rpgfan3233
12-11-2006, 06:19 PM
I agree that, much like the goto statement, break and continue have their place. In fact, I did something a few weeks ago just to brush up on my skills in C and C++. It turned out that I needed continue, which I had never previously used. As for break, if you think it is considered bad practice, then you don't use switch-case/select-case blocks, do you?

goto, though old and able to be circumvented, is still useful. goto has been abused quite badly, and still is abused badly by newbie coders when they learn it, if they haven't learned loops first. goto is useful in situations you might be wanting to continue through a program or go to a later part. Of course, some practical uses of goto could be set up so that if-then-else statements or ternary conditionals ( (condition) ? (value if condition is true) : (value if condition is false) ) or occasionally looping. However, it tends to create exact duplicate statements in blocks, which can also be considered poor practice since such statements often mean long code getting longer unnecessarily. While goto is generally used badly, it still has its uses. I don't advocate the use of it, but I certainly don't remove it from my toolbox and forget about it as if it never existed.

Aradon
12-11-2006, 09:10 PM
I agree that, much like the goto statement, break and continue have their place. In fact, I did something a few weeks ago just to brush up on my skills in C and C++. It turned out that I needed continue, which I had never previously used. As for break, if you think it is considered bad practice, then you don't use switch-case/select-case blocks, do you?

goto, though old and able to be circumvented, is still useful. goto has been abused quite badly, and still is abused badly by newbie coders when they learn it, if they haven't learned loops first. goto is useful in situations you might be wanting to continue through a program or go to a later part. Of course, some practical uses of goto could be set up so that if-then-else statements or ternary conditionals ( (condition) ? (value if condition is true) : (value if condition is false) ) or occasionally looping. However, it tends to create exact duplicate statements in blocks, which can also be considered poor practice since such statements often mean long code getting longer unnecessarily. While goto is generally used badly, it still has its uses. I don't advocate the use of it, but I certainly don't remove it from my toolbox and forget about it as if it never existed.

Granted, and I have also used break statements and continue statements. Mostly it is discouraged so frequently because of new coders. Just in this example it would be more beneficial to do it the boolean way rather then the break statement way.

Even in undergraduate work most courses will completely discourage use of the break statement in order to cement fundamental coding practices and usage.

I see break statements just as you do for the most part, however if there is a clean way around using a break statement then I'd much rather do it that way rather then use the break statement. Granted, in my last program I did have a break statement nested inside of it. But it was used as an exit point to a condition that would only show up once in every thousand executions.

I want to add that I used this break statement in order to increase efficiency in a program. I had already found a statement and so I wanted to break out of a loop in order to decrease the number of memory reads.

This debate by the way has been going on for decades. Even the Java Sun forums debate over this issue consistently.

Brandoe85
12-11-2006, 09:46 PM
That's fine, if you'd prefer to create a bool var and flip it on condition, great, personal preference. I'd rather break out of the loop instead. Bad practice, I wouldn't say so.

Not using break is apart of fundamental coding practice? hrmm...I find that hard to believe as well.

AnaUhibbuki
01-04-2007, 07:44 PM
hi i'm new to this forum, and new to programming.

from the source code :

String courseList[][]={{"COM101","Mon 08:00"},
{"COM201","Tue 09:00"},
{"COM301","Wed 11:00"},
{"COM315","Thu 14:00"},
{"COM401","Fri 16:00"},};

what does this statement "courseList.length" give ?
is it the length of the array or how much character in the string, i'm a little confused..

rpgfan3233
01-05-2007, 08:45 PM
courseList.length gives the length of the array. courseList[j].length(), where [i]i and j are integers representing the indexes of the array, gives the length of the string at [i, j].