Add exception Try and Catch

0

Create a program that requests a phrase by keyboard and displays on the screen the position of the character that has a higher ASCII value. The program must validate that the phrase written by the user does not have any of the following symbols: $, & or #. If any of these symbols are present, the program should show an error message and finish its execution.

It is the program to be developed and I have this done:

package errores2;

public class ascii {
  String cadena;
  private int i;

  public void splitcadena(){

    try{
      for(int i=0;i<cadena.length();i++){
        int as=cadena.charAt(i);
      }
    } catch(Exception a){
      // catch error a
    }
  }
}

package errores2;
import java.util.Scanner;

public class main {

    public static void main(String[] args) {
      Scanner read=new Scanner(System.in);
      ascii q=new ascii();
      System.out.println("Digite frase a convertir");
      q.cadena=read.nextLine();
      q.splitcadena();
    }
}
    
asked by Diego Alejandro 14.10.2016 в 02:08
source

5 answers

2

To see if the string contains any of those characters I would use the following regular expression: ".*[$&#].*" That regular expression means the following. The point represents any character, the asterisk represents 0 or N times, and the characters in brackets represent a position, so that regular expression is positive if it contains $ or & or # .

    String regExp=".*[hxx].*";
    if(cadena.matches(regExp)){
        System.out.println("Mensaje de error");
        System.exit(0);
    } 

Or maybe I would use a method within your class ascii type:      QuitSiContiene ("$");

 public void salirSiContiene(String caracter){ 
    if(cadena.contains(caracter)){
        System.out.println("La cadena contiene "+caracter);
        System.exit(0);
    } 
 }

To locate the major character if the string has not been discarded, I would do the following:

    char mayor = 0;
    for (int i = 0; i < cadena.length(); i++) {
        if ((int) cadena.charAt(i) > mayor) {
            mayor =  cadena.charAt(i);
        }
    }
    System.out.println (mayor); 

You do not need the try catch.

    
answered by 14.10.2016 в 03:58
1

I solved it with a cycle

If(la casilla del arreglo era igual a tal caracter){
  throw new Exception();
    
answered by 15.10.2016 в 02:32
0

Have you tried the indexof () function?

Check it here

    
answered by 14.10.2016 в 02:56
0

Well, what I've done is to do the exercise with the help of the idelcano methods and a structure complete would be:

public class LetraMayor {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        System.out.println("Escribe una frase...");
        String miCadena = scn.nextLine();

        if (contieneCaracteresNoPermitidos(miCadena)) {
            System.err.println("La frase contiene: $, & o #.");
        } else {
            System.out.println("La letra mayor es: '" + letraMayor(miCadena) + "'");
        }
        scn.close();
    }

    public static boolean contieneCaracteresNoPermitidos(String cadena) {
        String regExp=".*[$&#].*";
        if(cadena.matches(regExp)){
            return true;
        } else {
            return false;
        }
    }

    public static char letraMayor(String cadena) {
        int letraMayor = 0;// El valor mas bajo.

        for (int i = 0; i < cadena.length(); i++) {
            int letraActual = (int) cadena.charAt(i);
            if (letraActual > letraMayor) {
                letraMayor = cadena.charAt(i);
            }
        }

        return (char) letraMayor;
    }
}

It's just a complete structure, but the answer is already in idelcano , and as you can see in the code, no try / catch is used.

    
answered by 14.10.2016 в 12:05
0

I recommend you first filter that does not contain the symbols: $, & or # with the Contains () function Java.lang.String in a condition if(){} where you request the word.

The order would be:

  • Request the word in the function
  • Check with if(){} if it contains the symbols you mention ($, &, #)
  • If the condition is true, send the error message. Otherwise travel the entire word with the cycle for looking for the position of the largest ascii.

** To obtain the ascii of a letter you can do it in the following way:

Character.toString ((char) i);
    
answered by 14.10.2016 в 04:06