Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: help me with my buffered reader please! whats wrong?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help me with my buffered reader please! whats wrong?

    import.java.io*;


    public class Ship
    {
    public static void main(String[]args){
    BufferedReader dataIn= new BufferedReader ( new InputStreamReader(System.in));

    String name=" ";
    int b;
    try{
    name=dataIn.readline();
    }catch(IOException e){
    System.out.println("ERROR");}

    switch(b){
    case 1:
    System.out.println("Battleship");
    break;
    case 2:
    System.out.println("Cruiser");
    break;
    default;
    System.out.println("wrong");
    break;
    }
    }
    }


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Re: help me with my buffered reader please! whats wrong?

    Quote Originally Posted by jeremanalaotao View Post
    import.java.io*;


    public class Ship
    {
    public static void main(String[]args){
    BufferedReader dataIn= new BufferedReader ( new InputStreamReader(System.in));

    String name=" ";
    int b;
    try{
    name=dataIn.readline();
    }catch(IOException e){
    System.out.println("ERROR");}

    switch(b){
    case 1:
    System.out.println("Battleship");
    break;
    case 2:
    System.out.println("Cruiser");
    break;
    default;
    System.out.println("wrong");
    break;
    }
    }
    }


    Try this code this will work
    ===============================

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;


    public class Ship
    {
    public static void main(String[]args){
    BufferedReader dataIn= new BufferedReader ( new InputStreamReader(System.in));
    String name="";
    int b=0;
    try{
    name=dataIn.readLine();
    }catch(IOException e){
    System.out.println("ERROR");}
    switch(b){
    case 1: System.out.println("Battleship");
    break;
    case 2:System.out.println("Cruiser");
    break;
    default:System.out.println("wrong");
    break;
    }
    }
    }

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me with my buffered reader please! whats wrong?

    Quote Originally Posted by pushpendra.saraswat@gmail View Post
    Try this code this will work
    ===============================

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;


    public class Ship
    {
    public static void main(String[]args){
    BufferedReader dataIn= new BufferedReader ( new InputStreamReader(System.in));
    String name="";
    int b=0;
    try{
    name=dataIn.readLine();
    }catch(IOException e){
    System.out.println("ERROR");}
    switch(b){
    case 1: System.out.println("Battleship");
    break;
    case 2:System.out.println("Cruiser");
    break;
    default:System.out.println("wrong");
    break;
    }
    }
    }
    when i type 1 it should be "Battleship" instead "wrong" appears..any number i type "wrong" appears

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: help me with my buffered reader please! whats wrong?

    So you type 1 at the prompt, and 'name' ends up referring to a String object with the value "1". And then you switch on an int variable b. How does the value "1" referred to by 'name' get into the variable 'b' so that you can switch on it?
    Last edited by Sean4u; September 6th, 2011 at 08:47 AM. Reason: typo

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me with my buffered reader please! whats wrong?

    i really dont understand it .. T__T

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,107
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: help me with my buffered reader please! whats wrong?

    What variable holds what the user typed in?

    What variable is used in the switch statement?

    Are they different variables?
    How are they related?
    Do you need to convert the user's input so it can be used in the switch statement?

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me with my buffered reader please! whats wrong?

    variable b..yes i guess it needs to be converted?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,107
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: help me with my buffered reader please! whats wrong?

    i guess it needs to be converted?
    See the Integer class for a method to convert a String to an int

  9. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me with my buffered reader please! whats wrong?

    im really a beginner here..can u teach me?i cant find it

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,107
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: help me with my buffered reader please! whats wrong?

    Yes, beginners need to learn how to read the API doc.
    Go to this Site: Java Platform SE 6
    In the left hand column are listed the names of the classes.
    Scan down or Find Integer and click on it.
    The right hand section will display the API doc for the class. It has several sections.
    General discussion.
    Summaries of the following:
    Fields
    Constructors
    Methods
    Details for the above

  11. #11
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me with my buffered reader please! whats wrong?

    thanks man!

Similar Threads

  1. Whats wrong here?
    By Java Sucks in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 8th, 2011, 08:33 PM
  2. whats wrong?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2011, 02:35 PM
  3. Buffered Reader to J Option
    By jk_0821 in forum Collections and Generics
    Replies: 13
    Last Post: July 19th, 2010, 03:14 PM
  4. Buffered Reader is not reading my file properly... HELP!
    By mannyT in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: November 8th, 2009, 08:14 PM
  5. help whats wrong
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 3rd, 2009, 01:41 AM