Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    JAVA - Stampare solo array selezionata.

    Ciao a tutti,
    vorrei che alla fine se scrivo per esempio charmander mi stampi solo la stringa dove si trova la parola charmander e invece mi stampa:

    Sicuro?
    Sicuro?
    Nome: charmander
    Pokedex: 04
    Specie: Lucertola
    Evoluzione: Charmeleon\Charizard

    e quando invece scrivo qualcosa che non si trova in nessuna stringa dell'array mi restituisca 1 solo: Sicuro?
    Invece che:
    Sicuro?
    Sicuro?
    Sicuro?

    Io so perche fa cosi perche lui cicla indipendentemente da quello che trova...come posso risolvere il problema?

    Scusate la niubbagine...rimango a disposizione per le delucidazioni, vi ringrazio anticipatamente e vi allego il codice.

    codice:
    import java.util.Scanner;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Chiamata {
    
    
    	public static void main(String[] args) {
    
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Inserire il nome del pkm: ");
    		String testocap = scan.nextLine();
    		String testi = testocap.substring(0, 1).toLowerCase() + testocap.substring(1);
    
    		String pikachu = "Nome: pikachu \nPokedex: 025 \nSpecie: Topo \nEvoluzione: Pichu";
    		String squirtle = "Nome: squirtle \nPokedex: 02 \nSpecie: Scoiattolo \nEvoluzione: Wartholt\\Blastoise";
    		String charmander = "Nome: charmander \nPokedex: 04 \nSpecie: Lucertola \nEvoluzione: Charmeleon\\Charizard";
    
    		String[] array = new String [3];
    
    		array[0] =  pikachu;
    		array[1] =  squirtle;
    		array[2] =  charmander;
    
    		for (int i = 0; i < array.length; i++){
    
    			boolean found = array[i].indexOf (testi) != -1;
    
    			Pattern pattern = Pattern.compile("[a-z]+");
    			Matcher matcher = pattern.matcher(testi); 
    
    			if (found && matcher.matches()) {
    				System.out.println(array[i]);
    
    			}else{
    				System.out.println("Sicuro?");
    				
    			}
    		}
    	}
    }

  2. #2

    RISOLTO

    Ho risolto da solo cosi:

    codice:
    import java.util.Scanner;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Chiamata {
    
    
    	public static void main(String[] args) {
    
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Inserire il nome del pkm: ");
    		String testocap = scan.nextLine();
    		String testi = testocap.substring(0, 1).toLowerCase() + testocap.substring(1);
    
    		String pikachu = "Nome: pikachu \nPokedex: 025 \nSpecie: Topo \nEvoluzione: Pichu\n";
    		String squirtle = "Nome: squirtle \nPokedex: 02 \nSpecie: Scoiattolo \nEvoluzione: Wartholt\\Blastoise\n";
    		String charmander = "Nome: charmander \nPokedex: 04 \nSpecie: Lucertola \nEvoluzione: Charmeleon\\Charizard\n";
    
    		String[] array = new String [3];
    
    		array[0] =  pikachu;
    		array[1] =  squirtle;
    		array[2] =  charmander;
    
    		for (int i = 0; i < array.length; i++){
    
    			boolean found = array[i].indexOf (testi) != -1;
    
    			Pattern pattern = Pattern.compile("[a-z]+");
    			Matcher matcher = pattern.matcher(testi); 
    
    			if (found && matcher.matches() == false) {
    				System.out.println("Sicuro?");
    				break;
    
    			}else{
    				if(found && matcher.matches()){
    					System.out.println(array[i]);
    				}
    			}
    		}
    	}
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.