Results 1 to 15 of 15
  1. #1
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default My code is not working properly ..modify it

    problems-->

    1. not taking any input inside Case 1 & Case 2.
    2. System.exit() is not working .
    3. is their any problem in do-while loop ?



    import java.io.*;

    public class shyamin {

    public static void main ( String []args) throws IOException {

    char input=' ';

    do {
    System.out.println("--------------- MENU ----------------");
    System.out.println("1. Enter the name ");
    System.out.println("2.Enter Roll ");
    System.out.println("0.Exit");


    System.out.println("Enter the Choice");

    input=(char)System.in.read();

    byte b[]=new byte[10];

    switch(input) {

    case'1':
    System.out.println("Enter the Name ");

    int l=System.in.read(b);
    System.out.write(b,0,l-1);

    break;

    case'2':
    System.out.println("Enter the Roll ");
    byte c[]=new byte[4];
    int len=System.in.read(c);
    System.out.write(c,0,len-1);

    break;

    case '0':
    System.out.println(" Invalid ");
    System.exit(5);
    //break;
    }//switch

    } while(input >2);

    }
    }

    help pls.:::mad:

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default Two problems ..

    1. when i m tring to take user input in Case 1 , it is onle printing, not taking the input from the keyboard . it should take the name & roll no. as input.

    2. it is not comming out of the prog. after System.exit();

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    the while test compares the value of input(a char) to the int value of 2. It is very difficult to enter an int of two from the keyboard. You usually only enter characters from a keyboard and their int values start at 32 for a space.

    Read the doc for the in.read() method. What does it return?

    Could you copy and paste here the console when you run your tests. Add comments to it to describe what is wrong.

  6. #6
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default

    thank norm

    but i m giving as case '2' this means it will read it as char .

    the prog. printing the statment in side the case '1' but not promting for user input in the same case means it is going in side the case .

  7. #7
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    You have to use either scanner or a BufferedReader for this.
    thanks Eranga.

    could u pls post me a simlpe examlpe of the any of two.
    i m sending another prog. to u gays .. give a look to that also.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    Add some more println() statements to your program so you can see what the values are that the program is seeing and where the code is executing.
    case '2' this means it will read it as char
    Not sure what you mean.
    while(input >2);
    If input is defined as char, it will NOT have a value of 2. Its value could be '2' which is not the same as 2. Use type casting with println() to see the int value of '2' : println("'2'=" + (int)'2');

  9. #9
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    the while test compares the value of input(a char) to the int value of 2. It is very difficult to enter an int of two from the keyboard. You usually only enter characters from a keyboard and their int values start at 32 for a space.

    Read the doc for the in.read() method. What does it return?

    Could you copy and paste here the console when you run your tests. Add comments to it to describe what is wrong.
    --------------- MENU ----------------
    1. Enter the name
    2.Enter Roll
    0.Exit
    Enter the Choice
    1
    Enter the Name
    --------------- MENU ----------------
    1. Enter the name
    2.Enter Roll
    0.Exit
    Enter the Choice
    2
    Enter the Roll
    --------------- MENU ----------------
    1. Enter the name
    2.Enter Roll
    0.Exit
    Enter the Choice
    0
    Invalid

  10. #10
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default

    Quote Originally Posted by Shyam Singh View Post
    --------------- MENU ----------------
    1. Enter the name
    2.Enter Roll
    0.Exit
    Enter the Choice
    1
    Enter the Name
    --------------- MENU ----------------
    1. Enter the name
    2.Enter Roll
    0.Exit
    Enter the Choice
    2
    Enter the Roll
    --------------- MENU ----------------
    1. Enter the name
    2.Enter Roll
    0.Exit
    Enter the Choice
    0
    Invalid
    --------------- MENU ----------------
    1. Enter the name
    2.Enter Roll
    0.Exit
    Enter the Choice
    1
    Enter the Name this messg. is in side case 1
    --------------- MENU ----------------
    1. Enter the name
    2.Enter Roll
    0.Exit
    Enter the Choice
    2
    Enter the Roll this messg. is in side case 2
    --------------- MENU ----------------
    1. Enter the name
    2.Enter Roll
    0.Exit
    Enter the Choice
    0

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    I see the output.
    what is the problem??? Is it working?
    Could you copy and paste here the console when you run your tests. Add comments to it to describe what is wrong.

  12. #12
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default

    Norm it is going in the cases but not prompting for user i/p .

    console -->

    --------------- MENU ----------------
    1. Enter the name
    2.Enter Roll
    0.Exit
    Enter the Choice
    1
    --------------- MENU ----------------
    1. Enter the name
    2.Enter Roll
    0.Exit
    Enter the Choice
    Enter the Name this messg. is in side case 1
    --------------- MENU ----------------
    1. Enter the name
    2.Enter Roll
    0.Exit
    Enter the Choice
    2
    Enter the Roll this messg. is in side case 2
    --------------- MENU ----------------

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    not prompting for user i/p .
    Where is this supposed to happen?
    I don't see any reference to i/p in the program.

  14. #14
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default Scanner

    Quote Originally Posted by Norm View Post
    .....If input is defined as char, it will NOT have a value of 2. Its value could be '2' which is not the same as 2. Use type casting with println() to see the int value of '2' : println("'2'=" + (int)'2');
    Norm, readline ( or whatever it is ) for System.in ( the keyboard ) returns an int. This should ( I think ) be as you would expect it. Java has a class for reading such things from the keyboard: Scanner (Java 2 Platform SE 5.0) which I would expect does the type conversions by convention. I have not figured out any nomenclature for my own use nor do I know what is accepted practice, the Integer class has a toString method that will take the keyboard input and do a conversion to integer as it's binary form as used by the processor and so on.

    The Scanner class will show accepted practice in Java. A cast is likely ineffective, as it wil give ( for example ) 0x20 for a spacebar. ( I think )
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  15. #15
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default

    after entering into case1 & 2 -->


    byte b[]=new byte[10];

    switch(input) {

    case'1':
    System.out.println("Enter the Name ");

    int l=System.in.read(b); // Here
    System.out.write(b,0,l-1);

    break;




    case'2':
    System.out.println("Enter the Roll ");
    byte c[]=new byte[4];
    int len=System.in.read(c); // Here
    System.out.write(c,0,len-1);

Similar Threads

  1. Image not showed properly in struts
    By ganesan in forum Web Frameworks
    Replies: 4
    Last Post: 01-13-2009, 09:21 AM
  2. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  3. Log4j not working properly....
    By prakash_dev in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 12:13 PM
  4. Modify A* Algorithm
    By prakharbirla in forum Advanced Java
    Replies: 1
    Last Post: 02-13-2008, 06:25 PM
  5. how to properly express adding...
    By paul in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •