java program help - boolean


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: java program help - boolean

  1. #1
    Join Date
    Feb 2004
    Posts
    3

    java program help - boolean

    ive made a program so far, however when you select a member in the search i wanted it to print, "is this the correct member (y/n)" the problem is i dont know how to code it in java, ive looked through the forum and seen no example of y/n or true false from what i could find.

    PHP Code:
    public static void SearchMember()

     {
            
    String sName;
            
    int index;

            
    System.out.print("Enter Members Name >> ");
            
    sName Input.readString();
            
    System.out.println("");
            
    index SearchName(sName);
            if(
    index == -1)
            
    System.out.println(sName " - Member Not Found");
            else
      {
            
    s[index].displayMemberList();
      }
     } 

    if you press "Y" then it means that, that is the correct member and proceeds. if it is a 'N' then it returns to the public main.

    could some1 give me an example. on the above code say..

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    something like this method:

    Code:
    public boolean userIsSure(){
    
      System.out.print("Sure?(Y/n");
      String confirm = Input.readString();
      if(confirm.length == 0 || confirm.equalsIgnoreCase("y"))
        return true;
      else
        return false;
    }
    usage:

    if(userIsSure())
    //do whatever
    else
    //user is not sure, ask them again..


    you would typically use this inside an loop:

    Code:
    do{
      System.out.print("Member number:");
      memNumber = Input.readString()
    }while(!userIsSure())
    this reads as "do this (at least once) and repeat WHILE NOT user is sure (user is not sure, in english ).."

    see?
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Feb 2004
    Posts
    3
    nop. lol
    i hate java but my course makes me do it, im trying to learn it but seriously...i cant stand it.
    my program has to contain something like the above. here is my programs so far. it also has to save to file and read from file which i dont have a clue how to do.

    hehe. i was thinking on adding the above code or try to... to delete a member, u press the correct section on the program, "are you sure you wish to delete this member (y/n)"
    yes deletes
    no doesnt
    clever me

    saving to a file and reading im lost basically. the rest working fine i think
    Attached Files Attached Files

  4. #4
    Join Date
    Feb 2004
    Posts
    808
    you got the basic idea when you say:
    Code:
    		while(choice<1 || choice > 4)
      {
    		System.out.println("#################################");
    		System.out.print("Error - Enter From 1-4 >> ");
    		choice = Input.readInt();
      }
    your user cannot go on until he enters between 1 and 4 inclusive.. apply the same idea to your other methods.. but you should create a method like i did and ask the user if he is sure.. that way it saves pasting the same bit of code into every method.. just call the "askUserIfHeIsSure()" method... and test the response
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  5. #5
    Join Date
    Feb 2004
    Posts
    3
    and reading and writng to a file do you know how that is done?

    save member details to one file and game to a seperate one.

    im unsure how.

  6. #6
    Join Date
    Feb 2004
    Posts
    808
    Originally posted by m[o]
    and reading and writng to a file do you know how that is done?
    yes.

    Code:
    BufferedWriter bw_mem = new BufferedWriter(new FileWriter("C:\members.txt"));
    BufferedWriter bw_gam = new BufferedWriter(new FileWriter("C:\games.txt"));
    
    BufferedReader br_mem = new BufferedReader(new FileReader("C:\members.txt"));
    BufferedReader br_gam = new BufferedReader(new FileReader("C:\games.txt"));
    If you want to know how to use buffered reader and buffered writer:
    http://java.sun.com/j2se/1.3/docs/ap...redWriter.html
    http://java.sun.com/j2se/1.3/docs/ap...redReader.html
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center