Dsy Network www | forum | my | didattica | howto | wiki | el goog | stats | blog | dona | rappresentanti
Homepage
 Register   Calendar   Members  Faq   Search  Logout 
.dsy:it. : Powered by vBulletin version 2.3.1 .dsy:it. > Didattica > Corsi N - Z > Programmazione > aiuto Esercizio!!!!!!
  Last Thread   Next Thread
Author
Thread    Expand all | Contract all    Post New Thread    Post A Reply
Collapse
Cri
.primate.

User info:
Registered: Oct 2007
Posts: 67 (0.01 al dì)
Location: Milano
Corso: Informatica
Anno: Primo
Time Online: 1 Day, 12:00:16 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
aiuto Esercizio!!!!!!

Ciao ragazzi, qualcuno mi puo' dire dove sbaglio????
Se inserisco una stringa composta non solo di cifre e spazi il mio codice entra nel ciclo e non esce piu!!!!!!!!!! dov'e' l'errore????

grazie in anticipo
/*
Es 4.23 pag 153

Modificate l'applicazione SommaNumeriDaStringa in modo che venga all'utente di reinserire la
sequenza se essa non e' costituita esclusivamente da cifre e spazi. Per verificare se un carattere
e' un cifra decimale si puo' utilizzare il metodo statico isDigit della classe java.lang.Character:

- public static boolean isDigit(char ch)

*/

import prog.io.*;
import java.util.StringTokenizer;

class Es4_23
{
public static void main(String[] args)
{
//predisposizione dei canali di comunicazione
ConsoleInputManager in = new ConsoleInputManager();
ConsoleOutputManager out = new ConsoleOutputManager();

//lettura della sequenza
//String sequenza = in.readLine("Inserisci una sequenza di numeri interi separati da spazi: ");

boolean continua = false;
String sequenza = "";

do
{
sequenza = in.readLine("Inserisci una sequenza d numeri: ");

for (int i = 0 ; i < sequenza.length(); i++)
{
char c = sequenza.charAt(i);
if(!Character.isDigit(c) && c != ' ')
{
out.println("ripeti stringa ");
continua = true;
break;
}
}
} while(continua);


//costruzione dell'estrattore di token
StringTokenizer stk = new StringTokenizer(sequenza, " ");

int somma = 0;

while (stk.hasMoreTokens())
{
String token = stk.nextToken();



somma = somma + Integer.parseInt(token);

}

//comunicazione del risultato
out.println("La somma dei numeri nella sequenza e' : " + somma);

}
}

25-11-2011 15:30
Click Here to See the Profile for Cri Click here to Send Cri a Private Message Find more posts by Cri Add Cri to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
CowBoy
.arcimaestro.

User info:
Registered: May 2006
Posts: 294 (0.04 al dì)
Location: Milano
Corso: F49 - Informatica
Anno: Laureato F49
Time Online: 3 Days, 13:40:27 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

entra nel ciclo e non esce più... dov'è l'errore?!
La risposta è sottintesa, mancava l'else:

sostituisci il ciclo do-while con questo:

code:
do { sequenza = in.readLine("Inserisci una sequenza di numeri: "); for (int i = 0 ; i < sequenza.length(); i++) { char c = sequenza.charAt(i); if(!Character.isDigit(c) && c != ' ') { out.println("ripeti stringa "); continua = true; break; } else { continua = false; } } } while(continua);

__________________
.. ±·ø·±-`` MuSiC iS My LanGuAGe ´´-±·ø·± ..

Last edited by CowBoy on 25-11-2011 at 22:00

25-11-2011 21:52
Click Here to See the Profile for CowBoy Click here to Send CowBoy a Private Message Find more posts by CowBoy Add CowBoy to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Cri
.primate.

User info:
Registered: Oct 2007
Posts: 67 (0.01 al dì)
Location: Milano
Corso: Informatica
Anno: Primo
Time Online: 1 Day, 12:00:16 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

che stupida!!!
grazie mille!!!!

25-11-2011 23:29
Click Here to See the Profile for Cri Click here to Send Cri a Private Message Find more posts by Cri Add Cri to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
All times are GMT. The time now is 11:35.    Post New Thread    Post A Reply
  Last Thread   Next Thread
Show Printable Version | Email this Page | Subscribe to this Thread | Add to Bookmarks

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is ON
 

Powered by: vBulletin v2.3.1 - Copyright ©2000 - 2002, Jelsoft Enterprises Limited
Mantained by dsy crew (email) | Collabora con noi | Segnalaci un bug | Archive | Regolamento | Licenze | Thanks | Syndacate
Pagina generata in 0.031 seconds (77.05% PHP - 22.95% MySQL) con 28 query.