Téléchargement
illégal
Posez votre question Signaler

Extraction d'un mot en java [Résolu]

hanaw 3Messages postés 24 juillet 2007Date d'inscription - Dernière réponse le 26 juil. 2007 à 14:29
Bonjour,
J'ai trouvé une difficulté à programmer une méthode en java permettant l'extraction du nombre qui suit "generation time" dans la ligne suivante:
At 5.0043376036883105 : MAC addr:1--- received DATA frame { sz570(MAC-802.11_Data_Frame)sz58--Data Frame--duration:218--Address1:1--address2:101--Address3:0--Address4:-2--forcedError:false--__<sz512(INET)sz20--src:0--dest:1--prot:17--TTL:6/255--ToS:#0--label:0--nexthop:1__<sz492(UDP)sz8--s:50--d:1050__<id:1 generation time:5.000372 delay bound:0.03 src: 0>__>__>__ }
prière de m'aider car j'en ai besoin
Merci d'avance
Lire la suite 

Extraction d'un mot en java »

2 réponses
Réponse
+2
moins plus
Salut!

Voici une classe qui te permet d'extraire une chaîne hors d'une autre chaîne.

Regarde la doc dans la calsse pour plus d'info.

Cette classe permet de transformer la valeur lue en int, long, double ou String.

A toi de l'adapter pour retourner d'autre types de valeur (boolean, date,...)

import java.util.StringTokenizer;

/**
 * 
 * Class: StringExtractor.java
 * 
 * Description: StringExtractor extract an substring from a given String and may
 * convert that String in a particular format.
 * 
 * @author HackTrack
 * 
 * Created on Jul 25, 2007
 * 
 */

public class StringExtractor {

	/**
	 * Extract a long from another String
	 * 
	 * @param text
	 *            The source String
	 * @param afterText
	 *            The String after which the value has to be retrieved
	 * @param untilText
	 *            The String where the extractor must stop for substring
	 *            retrieve
	 * @return The long extracted from String
	 * @throws NumberFormatException
	 *             If String 'afterstring' hasn't been found in the 'source
	 *             String' or if the returned String can't be converted into a
	 *             long
	 */
	public static long extractLong(String text, String afterText, String untilText) throws NumberFormatException {
		return Long.parseLong(extractString(text, afterText, untilText));
	}

	/**
	 * Extract a double from another String
	 * 
	 * @param text
	 *            The source String
	 * @param afterText
	 *            The String after which the value has to be retrieved
	 * @param untilText
	 *            The String where the extractor must stop for substring
	 *            retrieve
	 * @return The double extracted from String
	 * @throws NumberFormatException
	 *             If String 'afterstring' hasn't been found in the 'source
	 *             String' or if the returned String can't be converted into a
	 *             double
	 */
	public static double extractDouble(String text, String afterText, String untilText) throws NumberFormatException {
		return Double.parseDouble(extractString(text, afterText, untilText));
	}

	/**
	 * Extract a int from another String
	 * 
	 * @param text
	 *            The source String
	 * @param afterText
	 *            The String after which the value has to be retrieved
	 * @param untilText
	 *            The String where the extractor must stop for substring
	 *            retrieve
	 * @return The int extracted from String
	 * @throws NumberFormatException
	 *             If String 'afterstring' hasn't been found in the 'source
	 *             String' or if the returned String can't be converted into a
	 *             int
	 */
	public static int extractInt(String text, String afterText, String untilText) throws NumberFormatException {
		return Integer.parseInt(extractString(text, afterText, untilText));
	}

	/**
	 * Extract a String from another String
	 * 
	 * @param text
	 *            The source String
	 * @param afterText
	 *            The String after which the value has to be retrieved
	 * @param untilText
	 *            The String where the extractor must stop for substring
	 *            retrieve
	 * @return The found substring or null if not found
	 */
	public static String extractString(String text, String afterText, String untilText) {
		int afterTextIndex = text.indexOf(afterText);
		String tempStr = text.substring(afterTextIndex + afterText.length());
		int untilTextIndex = tempStr.indexOf(untilText);
		tempStr = tempStr.substring(0, untilTextIndex);
		return tempStr;
	}

	public static void main(String[] args) {
		String text = "At 5.0043376036883105 : MAC addr:1--- received DATA frame { sz570(MAC-802.11_Data_Frame)sz58--Data Frame--duration:218--Address1:1--address2:101--Address3:0--Address4:-2--forcedError:false--__<sz512(INET)sz20--src:0--dest:1--prot:17--TTL:6/255--ToS:#0--label:0--nexthop:1__<sz492(UDP)sz8--s:50--d:1050__<id:1 generation time:5.000372 delay bound:0.03 src: 0>__>__>__ } ";
		String afterText = "generation time:";
		String untilText = " ";
		System.out.println(StringExtractor.extractDouble(text, afterText, untilText));
	}
}



;-)
HackTrack
hanaw- 26 juil. 2007 à 14:29
Merci beaucoup, ça a bien marché
Ajouter un commentaire
Ce document intitulé « Extraction d'un mot en java » issu de CommentCaMarche (www.commentcamarche.net) est mis à disposition sous les termes de la licence Creative Commons. Vous pouvez copier, modifier des copies de cette page, dans les conditions fixées par la licence, tant que cette note apparaît clairement.
Dossier à la une
Passage au tout numérique : quel coût pour les particuliers ?