Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Java - Errata compilazione in Programma elementare
Forum - Java - Errata compilazione in Programma elementare

Avatar
Fen24 (Normal User)
Newbie


Messaggi: 2
Iscritto: 03/04/2011

Segnala al moderatore
Postato alle 20:30
Domenica, 03/04/2011
File con il main ;


Codice sorgente - presumibilmente Java

  1. import java.io.*;
  2. class M_shift_ver
  3. {
  4.         public static void main(String args[])
  5.         {
  6.                 InputStreamReader In = new InputStreamReader (System.in);
  7.             BufferedReader T = new BufferedReader (In);
  8.                 int maxel,i=-1;
  9.                 char risp;
  10.                
  11.                
  12.                 try
  13.                 {
  14.                         System.out.println("Inserire il numero massimo di elementi");
  15.                         maxel = Integer.parseInt(T.readLine());
  16.                         shift_ver v1 = new shift_ver(maxel);
  17.                         System.out.println("Scegli tra i/c/s/r/l/f");
  18.                         risp = T.readLine().charAt(0);
  19.                         while (risp != 'f')
  20.                         {
  21.                                 switch(risp)
  22.                                 {
  23.                                         case 'i':
  24.                                         {
  25.                                         System.out.println("Chiama l'inserimento");
  26.                                         if (i<maxel-1)
  27.                                         i= v1.carica(i);
  28.                                         } break;
  29.                                         case 'c':
  30.                                         {
  31.                                         if (i>=0) i=v1.cancella(i);
  32.                                         }break;
  33.                                         case 's':
  34.                                         {
  35.                                         if (i>=0) v1.stampa(i);
  36.                                         }break;
  37.                                         case 'r':
  38.                                         {  
  39.                                         if (i>=0) v1.ricerca(i);
  40.                                         } break;
  41.                                         case 'l' :
  42.                                         {
  43.                                         if (i>=0)
  44.                                         v1.cerca(i);
  45.                                         } break;
  46.                                         default: System.out.println("Inserimento nullo"); break;
  47.                                 }
  48.                                 System.out.println("Scegli tra i/c/s/r/l/f");
  49.                             risp = T.readLine().charAt(0);
  50.                         }
  51.                 }
  52.                 catch(Exception E)
  53.                 {
  54.                         System.out.println("errore :" + E.getMessage());
  55.                         System.exit(1);
  56.                 }
  57.         }D
  58. }



Codice sorgente - presumibilmente Java

  1. File senza main
  2.  
  3. /*realizzare le seguenti funzioni
  4. memorizzare nome in ordine alfabetico
  5. cancellare nome
  6. cercare tutti i nomi che iniziano con x e terminano con y
  7. main con possibilita' di iterare */
  8.  
  9. import java.io.*;
  10. public class shift_ver
  11. {
  12.        
  13.         private String nomi[];
  14.         private  int maxel;
  15.  
  16.     //metodo costruttore
  17.         public shift_ver(int N)
  18.         {
  19.                 maxel = N;
  20.                 nomi = new String[N];
  21.         }
  22.     //carica
  23.         public  int carica(int i)
  24.         {
  25.                 InputStreamReader In = new InputStreamReader(System.in);
  26.                 BufferedReader T = new BufferedReader(In);
  27.                 String nome = " ";
  28.                 int j,k;
  29.                 try
  30.                 {
  31.                         System.out.println("Inserisci il nome");
  32.                         nome = T.readLine();
  33.                         j=0;
  34.                         while((j<=i) && (nome.compareTo(nomi[j])>0))
  35.                          {
  36.                                  System.out.println("sono in while");
  37.                          j++;
  38.                      }
  39.                         if(j>i)
  40.                         {
  41.                                 nomi[j] = nome;
  42.                         }
  43.                         else
  44.                         {
  45.                                 for (k=i;k>=j;k--)
  46.                                 nomi[k+1] = nomi[k];
  47.                                 nomi[j] = nome;
  48.                         }
  49.                 i++;
  50.                 return(i);
  51.             }
  52.            
  53.           catch(Exception E)
  54.           {
  55.                   int errore;
  56.                   System.out.println("errore: " + E.getMessage());
  57.                   errore = -1;
  58.                   return(errore);
  59.           }
  60.   }
  61.  
  62.  
  63.  
  64.         public  int cancella(int i)
  65.         {
  66.                 InputStreamReader In = new InputStreamReader(System.in);
  67.                 BufferedReader T = new BufferedReader(In);
  68.                 String  nome;
  69.                 int j,k;
  70.                 try
  71.                 {
  72.                         System.out.println("Inserisci il nome da cercare e cancellare");
  73.                         nome = T.readLine();
  74.                         j=0;
  75.                         while ((j<=i) && (nome.compareTo(nomi[j])>0))
  76.                            j++;
  77.                         if((j>i)||(nomi[j].compareTo(nome)>0))
  78.                                 System.out.println("Non trovato");
  79.                                 else
  80.                                 if(j==i)
  81.                                 i--;
  82.                                 else
  83.                                 {
  84.                                         for(k=j;k<=i;k++)
  85.                                         nomi[k] = nomi[k+1];
  86.                                         i--;
  87.                                 }
  88.                        
  89.                         return(i);
  90.                 }
  91.         catch(Exception E)
  92.                 {
  93.                         int errore;
  94.                         System.out.println("errore " + E.getMessage());
  95.                         System.exit(1);
  96.                         errore = -1;
  97.                         return(errore);
  98.                 }
  99.         }
  100.        
  101.        
  102.         public  int stampa(int i)
  103.         {
  104.                 int j;
  105.                 for(j=0;j<=i;j++)
  106.                 System.out.println(nomi[j]);
  107.                 return(j);
  108.         }
  109.        
  110.         public  void ricerca(int i)
  111.         {
  112.                 InputStreamReader In = new InputStreamReader(System.in);
  113.                 BufferedReader T = new BufferedReader(In);
  114.                 boolean trovato;
  115.                 String ric;
  116.                 int sup,inf,centro=0;
  117.                 try
  118.                 {
  119.                         System.out.println("Inserisci il nome che si desidera ricercare");
  120.                         ric = T.readLine();
  121.                         trovato = false;
  122.                         inf = 0;
  123.                         sup = i;
  124.                         while((inf<=sup) && (trovato == false))
  125.                         {
  126.                                 centro = (sup+inf) / 2;
  127.                                 if(ric.compareTo(nomi[centro]) == 0)
  128.                                 trovato = true;
  129.                                 else
  130.                                 if (ric.compareTo(nomi[centro])>0)
  131.                                 inf = centro+1;
  132.                                 else
  133.                                 sup = centro-1;
  134.                                         }
  135.                         if (inf > sup)
  136.                         System.out.println("Nome non trovato");
  137.                         else System.out.println("Nome " + ric + " trovato nella posizione " + centro);
  138.                 }
  139.                 catch(Exception E)
  140.                 {
  141.                         System.out.println("errore " + E.getMessage());
  142.                         System.exit(1);
  143.                 }
  144.         }  
  145.        
  146.         public void cerca(int i)
  147.         {
  148.                 InputStreamReader In = new InputStreamReader(System.in);
  149.                 BufferedReader T = new BufferedReader(In);
  150.                 char x, y;
  151.                
  152.                 try
  153.                 {
  154.                         System.out.println("Inserisci la lettera iniziale di controllo");
  155.                         x = T.readLine().charAt(0);
  156.                         System.out.println("Inserisci la lettera finale di controllo");
  157.                         y = T.readLine().charAt(0);
  158.                         for (int j=0;j<=i;j++)
  159.                         {
  160.                         if ((nomi[j].charAt(0) == x) && (nomi[j].charAt(nomi[j].length()-1) == y))
  161.                         System.out.println("Il nome soddisfa i parametri impostati");
  162.                     }                  
  163.             }
  164.         catch(Exception E)
  165.           {
  166.                   int errore;
  167.                   System.out.println("errore: " + E.getMessage());
  168.                   errore = -1;
  169.                  
  170.           }
  171.         }
  172. }


Salve sono nuovo del forum spero di non aver sbagliato niente nel postare.
Veniamo al problema;
Nel main non riesco ad implementare il caso 'l' dello swicht, quando compilo mi da errore e non capisco il motivo.

Inoltre non so se possibile ma non ho capito altre cose e avrei bisogno di una spiegazione.
Grazie :)

Ultima modifica effettuata da Fen24 il 03/04/2011 alle 20:32
PM Quote
Avatar
Fen24 (Normal User)
Newbie


Messaggi: 2
Iscritto: 03/04/2011

Segnala al moderatore
Postato alle 14:27
Lunedì, 04/04/2011
Nessuno mi può dare una mano?
Mi server per domani .-.

PM Quote