Java Could someone please help me out with this code I have

Java

Could someone please help me out with this code, I have no idea what to do.:

As a warmup, we are going to read input from a String. Look up the StringReader class in the API. Create a StringReader object using the string \"EECS 132\". Then, use the read method to read each character, one at a time, from the string. Finally, state how you can tell when the last character of the String is read. You may give an English description or you can enter Java code that does the test.

Solution


/**
* The java demo program that reads a string using
* StringReader and prints the last character read
* from the string and print the last character to console
* */
//StringReaderTester.java
import java.io.IOException;
import java.io.StringReader;
public class StringReaderTester {
  
   public static void main(String[] args) throws IOException {
      
       String str=\"EECS 132\";
       //Create an instance of StringReader with string , str
       StringReader sr=new StringReader(str);
       int lastCharacter=-1;      
       int data = sr.read();
       lastCharacter=data;
       //read until data is not -1 or end of the string
       while(data!=-1)
       {
           //print data to console
           System.out.println((char)data);
           lastCharacter=data;
           //read next data
           data = sr.read();
          
       }      
       //print lastCharacter to console
       System.out.println(\"Last character from the string is \"+(char)lastCharacter);
       //close the string reader object,sr
       sr.close();
   }

}


Sample Output:

E
E
C
S

1
3
2
Last character from the string is 2

Java Could someone please help me out with this code, I have no idea what to do.: As a warmup, we are going to read input from a String. Look up the StringReade

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site