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;
}
}
}
Re: help me with my buffered reader please! whats wrong?
Quote:
Originally Posted by
jeremanalaotao
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;
}
}
}
Re: help me with my buffered reader please! whats wrong?
Quote:
Originally Posted by
pushpendra.saraswat@gmail
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
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?
Re: help me with my buffered reader please! whats wrong?
i really dont understand it .. T__T
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?
Re: help me with my buffered reader please! whats wrong?
variable b..yes i guess it needs to be converted?
Re: help me with my buffered reader please! whats wrong?
Quote:
i guess it needs to be converted?
See the Integer class for a method to convert a String to an int
Re: help me with my buffered reader please! whats wrong?
im really a beginner here..can u teach me?i cant find it
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
Re: help me with my buffered reader please! whats wrong?