Results 1 to 2 of 2

Thread: System.in

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Does anyone know how i can get System.in to work.
    Ive done this before in C++ by just using cin <<
    but i cant seem to get it to work in Java.

    import java.io.*;

    class menu{
    public static void main(String args[]){

    int choice = 0;
    for(;{
    System.out.println(" ");
    System.out.println("Press 1 to say Hi");
    System.out.println("Press 2 to say Hello");
    System.out.println(" ");
    System.out.println("Enter a choice: ");
    System.in(choice); // dosent seem to work
    break;}



    switch(choice){
    case '1':
    System.out.println("Your choice is 1");
    break;
    case '2':
    System.out.println("Your choice is 2");
    break;
    default:
    System.out.println("You must enter in a choice");
    break;
    }



    }
    }


  2. #2
    Guest
    Check out my bolded type.

    import java.io.*;

    class menu{
    public static void main(String args[]){

    int choice = 0;
    for(;{
    System.out.println(" ");
    System.out.println("Press 1 to say Hi");
    System.out.println("Press 2 to say Hello");
    System.out.println(" ");
    System.out.println("Enter a choice: ");
    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));//The "standard" input stream System.in is represented by the variable stdIn
    try{
    choice = Integer.parseInt(stdIn.readLine());//readLine returns a String. Integer.parseInt returns an int.
    }catch(IOException e){System.out.println(e);
    }

    break;}



    switch(choice){
    case 1: //Java is Strongly typed. '1' is a character, 1 is an integer type. Notice that I removed the single quotes.
    System.out.println("Your choice is 1");
    break;
    case 2:
    System.out.println("Your choice is 2");
    break;
    default:
    System.out.println("You must enter in a choice");
    break;
    }

    }
    }

    [Edited by VirtuallyVB on 11-25-2000 at 12:46 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
  •  



Click Here to Expand Forum to Full Width