Hi All,

I need help with try catch statement at the statement. for code

myChar = s.readLine().charAt(0);

I keep getting error: can't find variable myCar and s if te remove the whole try catch statment I get error: unreported exeception java.io.IOException; must be caught or declared to be thrown on the line where this code ismyChar = s.readLine().charAt(0);

Please point me in the right direction

import java.io.*;
import java.util.*;



public class Shorthand
{
//TODO Complete the isVowel method
public boolean isVowel(char c)
{
char myChar;
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
myChar = s.readLine().charAt(0);
if (myChar == 'a' || myChar == 'e' || myChar == 'i' || myChar == 'o' || myChar == 'u')
if (myChar == 'A'|| myChar == 'E'|| myChar == 'I'|| myChar == 'O'|| myChar == 'U')
{
return true;
}
else
{
return false;
}
}


//TODO Complete the shorthand method
public String shorthand(String in)
{
StringBuilder vowel = new StringBuilder();
StringBuilder result = new StringBuilder(in);
result = null;
for(int i = 0;i < in.length(); i++)
{
char myChar = in.charAt(i);
if (isVowel(myChar))
{
vowel.append(myChar);
}
result.append(vowel);
return result.toString().trim();
}
}


//TODO Complete the run method
public void run() throws IOException
{
String yourLine;
Scanner sc = new Scanner(System.in);
yourLine = sc.nextLine();
while(!yourLine.equals("*"));
{
System.out.println("Enter your line of text");
}
yourLine = sc.nextLine();
try
{
System.out.println("Entered try statement");
myChar = s.readLine().charAt(0);
}
catch (Exception ex)
{
System.out.println("Caught excecption " + ex.getMessage());
}
}
}

Edited 3 Years Ago by happygeek: fixed formatting

as far as i can see from the code...

try making these two statements global

char myChar;
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));

as you define them only locally to the isVowel method block

Hi Pheonix,

I didn't get you tey already have a public access how can I make them global.

as far as i can see from the code...

try making these two statements global

char myChar;
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));

as you define them only locally to the isVowel method block

Edited 3 Years Ago by mike_2000_17: Fixed formatting

public class Shorthand
{
//TODO Complete the isVowel method
public boolean isVowel(char c)
{
[B]char myChar;
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));[/B]
myChar = s.readLine().charAt(0);
if (myChar == 'a' || myChar == 'e' || myChar == 'i' || myChar == 'o' || myChar == 'u')

change your code... so that it looks like this below

public class Shorthand
{
//TODO Complete the isVowel method
[B]BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
char myChar;[/B]

public boolean isVowel(char c)
{

myChar = s.readLine().charAt(0);

if (myChar == 'a' || myChar == 'e' || myChar == 'i' || myChar == 'o' || myChar == 'u')

Edited 6 Years Ago by phoenix911: n/a

Tried it but still getting error at line where code [myChar = s.readLine().charAt(0);] is . the error is unreported exeception java.io.IOException; must be caught or declared to be thrown

just add a try catch to the statement... hover your mouse over it, and add the try catch...

that should fix it

Below is the code edited from what you suggested it does have a try catch statement in the last code

import java.io.*;
import java.util.*;


public class Shorthand
{    
    //TODO Complete the isVowel method
    
    BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
    char myChar;
    
    
    public boolean isVowel(char c)
   {
myChar = s.readLine().charAt(0);
       if (myChar == 'a' || myChar == 'e' || myChar == 'i' || myChar == 'o' || myChar == 'u')
       if (myChar == 'A'|| myChar == 'E'|| myChar == 'I'|| myChar == 'O'|| myChar == 'U')
       {
           return true;
       }
       else
       {
           return false;
       }
   }
    
    //TODO Complete the shorthand method
    public String shorthand(String in)
    {
      StringBuilder vowel = new StringBuilder();
      StringBuilder result = new StringBuilder(in);
      result = null;
      for(int i = 0;i < in.length(); i++) 
      { 
          char myChar = in.charAt(i); 
          if (isVowel(myChar)) 
          { 
              vowel.append(myChar); 
          } 
          result.append(vowel); 
          return result.toString().trim(); 
      }
    }
    
    //TODO Complete the run method
    public void run() throws IOException
    {
       String yourLine; 
       Scanner sc = new Scanner(System.in); 
       yourLine = sc.nextLine(); 
       while(!yourLine.equals("*"));
       {
            System.out.println("Enter your line of text");
       }
       yourLine = sc.nextLine(); 
        try
        {
           myChar = s.readLine().charAt(0);  
        }  
      catch (Exception ex)
      {
         System.out.println("Caught excecption " + ex.getMessage());
      }
    }
}
This article has been dead for over six months. Start a new discussion instead.