Tuesday 15 February 2011

java - How do i input a string from a buffered reader? -


I also used the scanner and wanted to try to use a buffer reader: I still have to Have done something

import java.util. *; Import java.io * *; Public square ice cream cone (// variable string taste; int numScoops; scanner flavorIceCream = new scanner (System.in); // constructor public ice cream cone () {} // methods public string getFlavour () throws IOexception {try {BufferedReader KeyboardInput = New BufferedReader (New InputStreamReader (System.in)); System.out.println ("Please enter your taste ice cream"); ​​taste = keyboardInput.readLine (); Return keyboard input .readLine ();} Hold ( IOexception e) {e.printStackTrace ();}}

im to get an integer By the way, you can say that

  integer. ParseInt (keyboardInput.readLine ());  

But what should I do if I want the string < / P>

keyboardInput.readLine () already gives a string so that you can

  return keyboard input .readLine ();  

(update)

ReadLine throws a IOException method. You throw either an exception:

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

or you handle it in your own way.

  public stable string getFlavour () {BufferedReader keyboardInput = null; Try {keyboardInput = new BufferedReader (New InputStreamReader (System.in)); System.out.println ("Please enter your taste ice cream"); // In this case, you do not need to define this extra variable / string taste. KeyboardInput.readLine (); // return taste; Return keyboard input .readLine (); } Hold (IOException e) {// handle this e.Pintstaxress; } Return tap; }  

No comments:

Post a Comment