The Artima Developer Community
Sponsored Link

Java Answers Forum
help!!

1 reply on 1 page. Most recent reply: May 10, 2003 9:30 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
displace

Posts: 6
Nickname: displace
Registered: May, 2003

help!! Posted: May 9, 2003 9:12 AM
Reply to this message Reply
Advertisement
can someone help me to correct the code? this is the first time i code java program. i've problem with the input for boolean data type, when i insert 'true' or 'false' and enter, it won't jump out. and everytime after i run a menu, it won't return to mainmenu and just jump out from the program.can someone tell me why? thanks a lot.
below are part of the code:
application.java:

System.out.println("Enter your Choice: "); //allow user to input choice
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
choice = br.readLine();

if(choice.equalsIgnoreCase("a")) {
patCount = addNew(patientList, patCount);
}


public static int addNew(Patient[] patientList, int count)throws IOException {
int index = 0;
String id = "";
boolean found = false;
boolean addMore = true;

Patient aPatient;
while(addMore){
aPatient = new Patient();
do{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter new patient Id: ");
id = br.readLine();
System.out.println();
found = search(patientList, id, count);
if (found)
{
System.out.println("Patient Id exists. Please re-enter!");
}
}while(found);
while(!found){
addMore = aPatient.insert(count, id);
if(addMore){
patientList[count] = aPatient;
count++;
}
}
}
return count;
}

patient.java
//insert a new patient record
public boolean insert(int c, String id) throws IOException {
boolean addMore = false;
String cont = "";
String str = "";
int inAge = 0;
int inWgt = 0;
boolean inSmk = false;

do{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
setId(id);
System.out.print("Enter patient name: ");
str = readString(br);
setName(str);
System.out.println();

System.out.print("Enter patient age: ");
inAge = readInt(br);
setAge(inAge);
System.out.println();

System.out.print("Enter patient weight: ");
inWgt = readInt(br);
setWgt(inWgt);
System.out.println();

System.out.print("Is patient a smoker? (True/False): ");
inSmk = readBoolean(br);
System.out.println();

System.out.println("Do you wish to continue? (Y/N): ");
cont = readString(br);
if(cont.equalsIgnoreCase("y"))
addMore = true;
}while(cont.equalsIgnoreCase("y")==true);
return addMore;
}

public boolean readBoolean(BufferedReader br) throws IOException{
boolean input;
while(true){
try{
String smoker = br.readLine();
if(smoker.equals("true")){
setSmk(true);
}else{
setSmk(false);
}
}catch(IOException ioex){
System.out.println("Input error!");
}
}


}


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: help!! Posted: May 10, 2003 9:30 AM
Reply to this message Reply
Here is one problem.
If the variable found is false, program execution will never reach your inner while loop which reads: while(!found){

            }while(found);
                while(!found){
                    addMore = aPatient.insert(count, id);
                    if(addMore){
                        patientList[count] = aPatient;
                        count++;
                    }
                } 
            }
 


In your method readBoolean, it looks like execution will get stuck in an infinite loop. So try the following:

    public boolean readBoolean(BufferedReader br) throws IOException{
        boolean status = false;
            try{
                String smoker = br.readLine();
                if (smoker.equals("true"){
                    status = true;
                }else{
                    status = false;
                }
            }catch(IOException ioex){
                System.out.println("Input error!");
            }
        }
        return status;        
    
    } 

Flat View: This topic has 1 reply on 1 page
Topic: constructors Previous Topic   Next Topic Topic: getpixel and getcolor I need some help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use