views:

202

answers:

1

Im used too using Scanner mainly and want too try using a buffered reader: heres what i have so far

import java.util.*;
import java.io.*;
public class IceCreamCone 
{
// variables
String flavour;
int numScoops;
Scanner flavourIceCream = new Scanner(System.in);

// constructor
public IceCreamCone()
{

}
// methods
public String getFlavour() throws IOexception 
{
 try{

 BufferedReader keyboardInput;
 keyboardInput = new BufferedReader(new InputStreamReader(System.in));
 System.out.println(" please enter your flavour ice cream");
 flavour  =  keyboardInput.readLine();
 return keyboardInput.readLine();
 }
 catch (IOexception e)
 {
  e.printStackTrace();
 }
}

im fairly sure to get an int you can say

Integer.parseInt(keyboardInput.readLine());

but what do i do if i want a String

+3  A: 

keyboardInput.readLine() already returns a string so you should simply do:

return keyboardInput.readLine();

(update)

The readLine method throws an IOException. You either throw the exception:

public String getFlavour() throws IOException {
   ...
}

or you handle it in your method.

public static String getFlavour() {
 BufferedReader keyboardInput = null;
 try {
  keyboardInput = new BufferedReader(new InputStreamReader(System.in));
  System.out.println(" please enter your flavour ice cream");
  // in this case, you don't need to declare this extra variable
  // String flavour = keyboardInput.readLine();
  // return flavour;
  return keyboardInput.readLine();
 } catch (IOException e) {
  // handle this
  e.printStackTrace();
 }
 return null;
}
bruno conde
im still getting a compile error that way. i dont understand. your telling me not too have flavour defined as the string, just a line that will print?
OVERTONE
What's the error your getting?
bruno conde
unhandled io exception. i changed the code too what you gave me above.
OVERTONE
You don't have flavour defined as string. You have a meaningless construct flavour=string.(). He tells you to replace the last two lines with one.I must add that if you don't know how to declare a variable, you should not be writing any code.
yu_sha
@yu_sha there's no need to be mean. This is obviously a beginner.
itsadok
either return flavour or delete the flavour= line
Steve De Caux
@yu_sha i must add, im a student. so suck it. its my job too learn and make mistakes. dont like the question, then dont answer!
OVERTONE
i tried it that way. i can see what your doing now. i did have a custom exception class called iceCreamConeException but IOexception seems too make more sense. only now its telling me IOexception cannot be resolved too a type.
OVERTONE
It's not IOexception. It's IOException. Notice the capital E. See my updated answer.
bruno conde
ah, i missed that one. OK i got it now.thanks for the help bruno.
OVERTONE