|
5 Dec 2006, 17:57
|
#1
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Java Project
This is procedural before you ask. We're starting object orientated next term.
The problem I'm having with this is that when you answer yes to "do you have a headache?" and then yes to the next "do you have a rash"- you get given "ring 999" message. But then it goes back and asks "do you have a rash?" - i don't want it to ask that, I just want it to go back to the start of the program.
I've tried adding breaks in two of the questions but this doesn't seem to work.
Any help is greatly appreciated - thanks.
Here is the coding:
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
Last edited by Androme; 6 Dec 2006 at 18:30.
|
|
|
6 Dec 2006, 00:19
|
#2
|
Warden
Join Date: Jul 2005
Location: The Far Side
Posts: 137
|
Re: Java Project
The trouble is the condition on your loops in the questions:
while (!answer[1].equalsIgnoreCase("yes") || !answer[1].equalsIgnoreCase("no"))
it should be && because:
!answer[1].equalsIgnoreCase("yes") == false
!answer[1].equalsIgnoreCase("no") == true
false || true == true
but
false && true == false
alternatively you could use:
while (! (answer[1].equalsIgnoreCase("yes") || answer[1].equalsIgnoreCase("no")) )
This might help or just be confusing, I don't know, but it's because of De Morgans Law:
!(a || b) === !a && !b
|
|
|
6 Dec 2006, 18:23
|
#3
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: Java Project
Cheers SpaceMonkey - that helped solve the problem!!!
However, I noticed something else just during testing. When I type in "exit" as players name to stop the program going through all the questions it adds "exit" to the names arraylist - I don't want exit to appear in the arraylist. I realise I have to 'remove' exit from the arraylist but I'm not sure how to search an arraylist for an item and then remove it.
Do I have to go through "i<names.length" and do "if names[i] = "exit",
names.remove[i]" kind of thing?
Also, this doesn't seem to work as I get these 2 errors:
>> "cannot find symbol variable length"
>> "array required, but java.util.ArrayList found"
Code:
for (int i=1; i<names.length; i++)
{
System.out.println("Name: " + names[i] + "\nAnswer: " + ans);
}
Does this mean I cannot look at the length of the arraylist because in theory it is infinate? Does that mean .length can only be used on arrays? :|
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
Last edited by Androme; 6 Dec 2006 at 18:29.
|
|
|
6 Dec 2006, 23:33
|
#4
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: Java Project
Also, how can I link 2 arraylists? Is a LinkedList the solution?
I have a names arraylist and an answers arraylist (one answer per person). I'm going to run an algorithm using the comparator to sort the names arraylist into alphabetical order but that means the arraylist will no longer be in the same position as the players name.
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
|
|
|
7 Dec 2006, 00:10
|
#5
|
[Vision]
Join Date: Jul 2001
Location: The Netherlands
Posts: 897
|
Re: Java Project
Although i have no idea what your actual code looks like and it has been a while since i fooled around in Java, i'll give it a go...
Quote:
Originally Posted by Androme2
Cheers SpaceMonkey - that helped solve the problem!!!
However, I noticed something else just during testing. When I type in "exit" as players name to stop the program going through all the questions it adds "exit" to the names arraylist - I don't want exit to appear in the arraylist. I realise I have to 'remove' exit from the arraylist but I'm not sure how to search an arraylist for an item and then remove it.
Do I have to go through "i<names.length" and do "if names[i] = "exit",
names.remove[i]" kind of thing?
|
This doesn't make a whole lot of sense in a program. How about instead of adding every player name submitted to the names list by default, you first check for the values you don't want to be added and simply don't allow them to be added at all (and immediatly start the desired procedure on the exception instead of doing 'extra' work).
i.e.
Code:
if( entered_name == 'exit' ) {
this.startExitProcedure();
} else {
namesList.Add( New name());
}
Quote:
Also, this doesn't seem to work as I get these 2 errors:
>> "cannot find symbol variable length"
>> "array required, but java.util.ArrayList found"
Code:
for (int i=1; i<names.length; i++)
{
System.out.println("Name: " + names[i] + "\nAnswer: " + ans);
}
Does this mean I cannot look at the length of the arraylist because in theory it is infinate? Does that mean .length can only be used on arrays? :|
|
In Java there is a difference between Array and Arraylist. Going by those error messages your names list is an Arraylist, which is resizable array/list of objects rather than a normal static array of values. Therefor you have to use a different technique to access the values. Meaning you have to use names.size() to get the amount of elements in the nameslist and names.get(i) to get the object from a specific index and then use.
Quote:
I have a names arraylist and an answers arraylist (one answer per person). I'm going to run an algorithm using the comparator to sort the names arraylist into alphabetical order but that means the arraylist will no longer be in the same position as the players name.
|
Java has been a while for me this is quite hard for me to remember. But, since Java is a OO language another solution would be to make a Player class which holds the name of the player (string value) and the list of questions for that player. When a new name is entered a new Player class is initiated with a new Question list, the player object is added to the names arraylist and you can access the question list through the selected player object (advantage being that in such a solution the question lists can 'never' be detached from the player/name or be mixed up with a list from another player/name).
__________________
[Vision] in a lost dream, contributing to The 5th Element at present
|
|
|
7 Dec 2006, 00:30
|
#6
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: Java Project
cheers for the help dude
why is it this works:
Code:
for (int i=0; i<nameslist.size(); i++)
{
System.out.println("Name: " + nameslist.get(i) + "\nAnswer: " + anslist.get(i) + "\n");
}
but the following will not and gives me this error: "cannot find symbol method showMessageDialog(java.lang.String)"
Code:
for (int i=0; i<nameslist.size(); i++)
{
JOptionPane.showMessageDialog("Name: " + nameslist.get(i) + "\nAnswer: " + anslist.get(i) + "\n");
}
I want to display the 2 lists in a message box but it will not allow it - seems to only let me do so in the command line.
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
Last edited by Androme; 7 Dec 2006 at 00:49.
|
|
|
7 Dec 2006, 02:37
|
#7
|
Insomniac
Join Date: May 2003
Posts: 3,583
|
Re: Java Project
dont forget the null before the message in showMessageDialog.
JOptionPane.showMessageDialog(null,message); etc
|
|
|
7 Dec 2006, 02:53
|
#8
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: Java Project
Quote:
Originally Posted by Phil^
dont forget the null before the message in showMessageDialog.
JOptionPane.showMessageDialog(null,message); etc
|
that gives me this error message for the coding i gave above your post
cannot find symbol method println(<nulltype>,java.lang.String)
thanks though!
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
|
|
|
7 Dec 2006, 03:01
|
#9
|
Insomniac
Join Date: May 2003
Posts: 3,583
|
Re: Java Project
You only want the null param in the JOptionpane line, Not the system.out.println line.
This is becase the showmessagedialog function can take a componant as a parameter. Since you have no others - substituting it with null gets around it
|
|
|
7 Dec 2006, 03:34
|
#10
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: Java Project
LOL my stupid! Thanks.
Just an update on the coding btw - I'm combining the two arraylists into a new arraylist called "cbal". I haven't touched multiple classes and object orientated stuff yet - I don't want to confuse myself right now heh.
First part combines the two arraylists into one. Second part displays a message. ** PROBLEM** - it displays each element of my new combined arraylist with a seperate message box - i.e.
say I have the elements mark, paul, jack - it'll display mark in one message dialog, paul in another etc. - I want them to be displayed in one - in a list - any idea how?
Code:
for (int i=0; i<nameslist.size(); i++)
{
cbal.add(nameslist.get(i) + " : " + anslist.get(i));
}
for (int i=0; i<cbal.size(); i++)
{
JOptionPane.showMessageDialog(null, cbal.get(i));
}
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
Last edited by Androme; 7 Dec 2006 at 03:48.
|
|
|
7 Dec 2006, 04:50
|
#11
|
Insomniac
Join Date: May 2003
Posts: 3,583
|
Re: Java Project
put them all into a single string first., then use joptionpane to display the string
|
|
|
7 Dec 2006, 11:22
|
#12
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: Java Project
thanks annoying questions should now stop
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
|
|
|
7 Dec 2006, 11:39
|
#13
|
Insomniac
Join Date: May 2003
Posts: 3,583
|
Re: Java Project
a little trick you can use if you want it to be 'pretty' when it appears in the joptionpane - use html coding for it
Code:
String out = "<html><ul>";
for (int i=0; i<cbal.size(); i++)
{
out += "<li>" + cbal.get(i) + "</li>";
}
out += "</ul></html>";
JOptionPane.showMessageDialog(null,out);
Now, assuming ive got the html tags correct that should put it in a nice bullet point'd list.
|
|
|
7 Dec 2006, 11:49
|
#14
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: Java Project
Wow! But it gives the same result as what I've done:
Code:
for (int i = 0; i<xd.size(); i++)
{ stringans += xd.get(i); }
JOptionPane.showMessageDialog(null, xd);
Thanks though
**UPDATE**
Why is it combining example elements of two arraylists into one to give something like ---> "Mark - Has The Flu" but then when these 2 arraylists are combined into one AND THEN displayed in a list give an output like this?
[Mark - Has the flu
, paul - not ill
, test - diahorrea
]
My coding for combining the two lists is below and my coding for displaying the combined list is at the top of thisg msg
Code:
for (int i=0; i<nameslist.size(); i++)
{
xd.add(nameslist.get(i) + " --- " + anslist.get(i) + "\n");
}
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
Last edited by Androme; 7 Dec 2006 at 11:56.
|
|
|
7 Dec 2006, 11:59
|
#15
|
Insomniac
Join Date: May 2003
Posts: 3,583
|
Re: Java Project
Its displaying the arraylist like that because thats how its tostring method formats the data inside of it. When you use
JOptionPane.showMessageDialog(null, xd); , you are indirectly calling the tostring method for the arraylist object. I suspect you meant to use xd.get(i) , or stringans instead
Last edited by Phil^; 7 Dec 2006 at 12:05.
|
|
|
7 Dec 2006, 12:49
|
#16
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: Java Project
Thanks for all those who have helped me put my project together.
Especially for that book Phil, nice algorithms in it. I might not understand that book yet but it doesn't seem out of reach which is nice. Thanks
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
|
|
|
|
All times are GMT +1. The time now is 07:27.
| |