Advertisement
MOISES_QUISPE_ROJAS

Estructura Datos 2021 - TP01 Helper

Sep 15th, 2021
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.09 KB | None | 0 0
  1. /* Estructura de Datos - Facultad de Ingenieria - Universidad Nacional de Jujuy
  2.  *
  3.  * @Autor: Equipo 4.1
  4.  */
  5. /*      @integrantes:                  |    @Carrera:             |   @LU:
  6.                                        |                          |
  7.   Flores ,Cesar Ismael                 | Lic. en Sistemas         | 1782
  8.   Llampa, Ariel Angel Gabriel          | Ing. Informatica         | 8445
  9.   Machaca, Rodrigo Agustin             | Ing. Informatica         | 8512
  10.   Perez, Emanuel Ismael                | Ing. Informatica         | 8393
  11.   Quispe Rojas, Moises Esteban Nicolas | Ing. Informatica         | 7286
  12.  
  13.     Clase Helper
  14.  
  15. */
  16. package Array;
  17.  
  18. import java.text.ParseException;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Date;
  21. import java.util.Random;
  22. import java.util.Scanner;
  23.  
  24. public class Helper {
  25.     private static Scanner input = new Scanner(System.in);
  26.     public static Random random=new Random();
  27.    
  28.      public static float getFloat(){
  29.         float numberfloat = 0;
  30.         boolean band = true;
  31.         do {
  32.             try {
  33.                 numberfloat = Float.parseFloat(input.nextLine());
  34.                 band = false;
  35.             } catch (NumberFormatException e) {
  36.                 System.out.println("Error...debe ingresar un numero");
  37.                 System.out.print("Ingrese nuevamente: ");
  38.             }
  39.         } while (band);
  40.         return numberfloat;
  41.     }
  42.    
  43.     public static String getString(){
  44.         String string;
  45.         do {
  46.             string = input.nextLine();
  47.             if (string.isBlank()) {
  48.                 System.out.println("Debe ingresar una cadena de texto");
  49.                 System.out.print("Ingrese nuevamente: ");
  50.             }
  51.         } while (string.isBlank());
  52.         return string;
  53.     }
  54.      
  55.     public static void showArray(Object array[]) {
  56.         for(int i=0;i<array.length;i++){
  57.             System.out.println("v[" + i + "]: " + array[i]);
  58.         }
  59.     }
  60.      
  61.     public static float getFloatPositive(){
  62.         float numberfloat = 0;
  63.         boolean band = true;
  64.         do {
  65.             try {
  66.                 numberfloat = Float.parseFloat(input.nextLine());
  67.                 if(numberfloat<=0){
  68.                     System.out.println("Debe ingresar un numero positivo");
  69.                     System.out.print("Intentelo de nuevo: ");
  70.                 }else{
  71.                     band = false;
  72.                 }
  73.             } catch (NumberFormatException e) {
  74.                 System.out.println("Error...debe ingresar un numero");
  75.                 System.out.print("Ingrese nuevamente: ");
  76.             }
  77.         } while (band);
  78.         return numberfloat;
  79.     }
  80.  
  81.     public static Date getDate(){
  82.         SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
  83.         boolean band = true;
  84.         Date testDate = null;
  85.         do{                    
  86.             String fecha = input.nextLine();  
  87.             try{
  88.                testDate = sdf.parse(fecha);
  89.                 band = false;
  90.             } catch (ParseException e){
  91.             System.out.print("Error... ");  
  92.                 System.out.print("Intentelo nuevamente:");
  93.             }
  94.         }while(band);
  95.         return testDate;
  96.     }
  97.    
  98.     public static int getIntegerPositive(){
  99.         int numberint = 0;
  100.         boolean band=true;
  101.         do{
  102.             try{
  103.                 numberint=Integer.parseInt(input.nextLine());
  104.                 if (numberint<=0){
  105.                     System.out.println("Debe ingresar un numero positivo");
  106.                     System.out.print("Intentelo de nuevo: ");
  107.                 }else{
  108.                     band=false;
  109.                 }
  110.             }catch(Exception e){
  111.                 System.out.println("Error...");
  112.                 System.out.print("Intentelo de nuevo: ");
  113.             }
  114.         }while(band);
  115.         return numberint;
  116.     }
  117.    
  118.     public static int getInteger(){
  119.         int numberint = 0;
  120.         boolean band=true;
  121.         do{
  122.             try{
  123.                 numberint=Integer.parseInt(input.nextLine());
  124.                 band=false;
  125.             }catch(Exception e){
  126.                 System.out.println("Error...");
  127.                 System.out.print("Intentelo de nuevo: ");
  128.             }
  129.         }while(band);
  130.         return numberint;
  131.     }
  132.    
  133.     public static char getChar(){
  134.         String character;
  135.         char singleCharacter;
  136.         boolean band;
  137.         do {
  138.             character = input.nextLine();
  139.             if(character.isBlank()){
  140.                 System.out.println("No ha ingresado un valor");
  141.                 System.out.print("Ingrese nuevamente: ");
  142.                 band = true;
  143.             }else{
  144.                 if(character.length()>1){
  145.                     System.out.println("Ingreso invalido");
  146.                     System.out.print("Ingrese nuevamente: ");
  147.                     band=true;
  148.                 }else{
  149.                  band = false;  
  150.                 }
  151.             }
  152.         } while (band!=false);
  153.         singleCharacter = character.charAt(0);
  154.         return singleCharacter;
  155.     }
  156.    
  157. }
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement