Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Jbuilder main methods  RSS feed

 
Wayne Styles
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have the following code, but the task asks for me to limit the input to 1-3, and display a message

But as far as I can see it already does this or am I missing something?


Added Code tags and formatted for easy reading - Carl
[ August 20, 2006: Message edited by: Carl Trusiak ]
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You always display the message and continue processing even if the input doesn't meet the requirements.

Add some tests to only display the message if the input is outside the required values and only process if correct.

Another thing, you need to add protection if the user would enter something like "Fred" Hint: try Catch block.
 
Wayne Styles
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thanks Carl
But I am new and dont know what you mean here sorry.
 
Brian Mozhdehi
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Wayne,

What Carl is saying is that your code always shows the error message regardless of whether or not the user types in a valid value (e.g. 1 through 3) or an invalid value, presumably 4 and up.

Try doing:

if(numberOfStaff < 1 || numberOfStaff > 3)
System.out.println("Sorry, only numbers from 1 - 3");

You might also structure it to prompt until the correct input is entered, like this:

int numberOfStaff = 0; //note, I did not see where you are initializing the variable here, so I added the declaration

while(numberOfStaff < 1 || numberOfStaff > 3)
{
String userln=JOptionPane.showInputDialog("Enter the number of Nurses presently employed.");
numberOfStaff=Integer.parseInt(userln);

if(numberOfStaff < 1 || numberOfStaff > 3)
{
System.out.println("Sorry, only numbers from 1 - 3");
continue;
}

staffNames = new String[numberOfStaff];
//this added to initialize the array

for (int count = 0 ; count < numberOfStaff; count=count + 1)
{
staffNames[count]=JOptionPane.showInputDialog("Enter the name of the next Nurse.");
}

}

you also may want to consider using a JOPtionPane to show the error message,
instead of printing it to the console, but of course, I dont know the actual business
requirements.

Finally, Carl also said that you are not checking to see if they have even entered
an int before using numberOfStaff=Integer.parseInt(userln);

you might do:

try{numberOfStaff=Integer.parseInt(userln);}
catch(Exception e){//some error message indicating that the user typed in something invalid}
 
Brian Mozhdehi
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot, you can also ensure the user enters an int by doing this (plagarized from JavaDoc on JOptionPane.....an excellent place to start learning about how to use these classes )

Object[] possibleValues = { "1", "2", "3" };
Object selectedValue = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);

Hope that helps
 
Wayne Styles
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Brian
I will have a go at this. Many, many thanks for your time on this its much appreciated.
 
Wayne Styles
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK
I got this bit working, but I now need to add a new class called nurse, then 2 new attributes.
I have got so far, but its not asking for the input?
anyone help
package Admin;

import javax.swing.*;

public class Admin{

static int numberOfStaff;
static String[]staffNames;
public Admin(){
}
public static void main(String[]args){
Admin.setThingsUp();
System.out.println("All Data Entered Goodbye");
System.exit(0);
}
private static void setThingsUp(){
while(numberOfStaff < 1 || numberOfStaff > 3)
{
String userln=JOptionPane.showInputDialog("Enter the number of Nurses presently employed. 3 Maximum");
numberOfStaff=Integer.parseInt(userln);
{
}
staffNames = new String[numberOfStaff];//this added to initialize the array
NurseID= new string [NurseID];
NatIns= new NatIns [NatIns];
for (int count = 0 ; count < numberOfStaff; count=count + 1)
for (int count = 0 ; count < NurseID; count=count + 1)
for (int count = 0 ; count < NatIns; count=count + 1)
{
staffNames[count]=JOptionPane.showInputDialog("Enter the name of the next Nurse.");
NurseID[count]=JOptionPane.showInputDialog("Enter the Nurse ID Number.");
NatIns[count]=JOptionPane.showInputDialog("Enter the National Insurance Number.");
continue;
}
}
numberOfStaff = integer.parseInt(userIn);
staffNames=new Sring[numberOfStaff];
}
}
 
Brian Mozhdehi
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one question....you do have a JFrame around this right? Is your code calling this class directly from the main method or have you built some sort of UI?
 
Wayne Styles
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Well no it does not have a frame, its from the JBuilder design pane.

It may help to look at my other post as this is now cleaner code I hope with specific problems.

Can I thank you for taking the time to help, its driving me made and I have been reading all day as well and its still not sinking in. I am hoping for a ureka moment soon!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm closing this thread to prevent duplicated effort.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!