Java - Zahlen aus String?

3 Antworten

Wenn du nur das Fragezeichen ersetzen willst:

for (int i = 0; i < test.length(); i++) {
    if (Character.isDigit(test.charAt(i))){
        count ++;
    }
}

Mit dem "enhanced Statement":

 for (char c : test.toCharArray()) {
    if (Character.isDigit(c)){
        count ++;
    }
}

Mit Streams (ab Java 8):

long count = test.chars().mapToObj(i -> (char) i).filter(Character::isDigit).count();
Woher ich das weiß:Berufserfahrung

Wenn du keine zweistelligen Zahlen zählen möchtest (was schwieriger wird), sondern wirklich nur die "Digits", kannst du es mit Regex versuchen:

int zahlen_count = DeinString.replaceAll("\\D", "").length()

Oder du machst es mit einem for-loop:

int zahlen_count = 0;
for (int i = 0; i < DeinString.length(); i++) {
    if (Character.isDigit(DeinString.charAt(i))) {
        zahlen_count++;
    }
}

Wenn du die Schleife nutzen willst, dann kannst du folgende Bedingung verwenden:

if (test.charAt(i) >= '0' && test.charAt(i) <= '9')

Es prüft, ob der char eines der Zeichen 0-9 ist, also "zahlen".

Woher ich das weiß:Studium / Ausbildung – Informatik-Studium / Mathematik-Studium / ITK-Ausbildung
BBEad 
Fragesteller
 20.09.2018, 09:34
package testTests;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class countTest {
	public WebDriver driver;
	public String baseUrl;
	public String test;
	public int count;
	
	@BeforeClass
	public void setUp() {
		driver = new ChromeDriver()https://www.youtube.com/watch?v=5FUdrBq-WFoh?v=5FUdrBq-WFo";
		driver.get(baseUrl);
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.manage().window().maximize();
	}

	
	@Test
	public void getInt(char in, char c) throws InterruptedException {
		test = driver.findElement(By.xpath("//span[@class='view-count style-scope yt-view-count-renderer']")).getText();
		Thread.sleep(2000);
		count = 0;
		for(int i = 0; i <= test.length(); i++) {
			if(test.charAt(i) >= '0' && test.charAt(i) <= '9') {
				count ++;
			}
			
		}

		}
	
	@AfterClass
	public void tearDown() {
		System.out.println(count);
		driver.close();		
	}

}

0
BBEad 
Fragesteller
 20.09.2018, 09:35

im allgemeinen gehts um Selenium. Wenn ich diese Funktion jetzt ausführe kommt Folgendes:

[TestNG] No tests found. Nothing was run

Usage: <main class> [options] The XML suite files to run

 Options:

  -configfailurepolicy

   Configuration failure policy (skip or continue)

  -d

   Output directory

  -dataproviderthreadcount

   Number of threads to use when running data providers

  -excludegroups

   Comma-separated list of group names to exclude

  -groups

   Comma-separated list of group names to be run

  -junit

   JUnit mode

   Default: false

  -listener

   List of .class files or list of class names implementing ITestListener 

   or ISuiteListener

  -methods

   Comma separated of test methods

   Default: []

  -methodselectors

   List of .class files or list of class names implementing IMethodSelector

  -mixed

   Mixed mode - autodetect the type of current test and run it with 

   appropriate runner

   Default: false

  -objectfactory

   List of .class files or list of class names implementing 

   ITestRunnerFactory 

  -parallel

   Parallel mode (methods, tests or classes)

   Possible Values: [tests, methods, classes, instances, none, true, false]

  -port

   The port

  -reporter

   Extended configuration for custom report listener

  -suitename

   Default name of test suite, if not specified in suite definition file or 

   source code

  -suitethreadpoolsize

   Size of the thread pool to use to run suites

   Default: 1

  -testclass

   The list of test classes

  -testjar

   A jar file containing the tests

  -testname

   Default name of test, if not specified in suitedefinition file or source 

   code 

  -testnames

   The list of test names to run

  -testrunfactory, -testRunFactory

   The factory used to create tests

  -threadcount

   Number of threads to use when running tests in parallel

  -usedefaultlisteners

   Whether to use the default listeners

   Default: true

  -log, -verbose

   Level of verbosity

  -xmlpathinjar

   The full path to the xml file inside the jar file (only valid if 

   -testjar was specified)

   Default: testng.xml

0
SirNik  20.09.2018, 18:28
@BBEad

Hi; mit selenium kenne eich mich leider nicht aus.
Aber laut der Ausgabe sollte in einer xml datei irgendwo drin stehen, welche Klassen Testklassen sind und diese werden dann bestimmt ausgeführt; Inwiefern du diese nun einträgst weiß ich nicht (gibt bestimmt ein automatismus).

Anderer Punkt; Deine testmethode ist so aktuell ja noch sinnlos, ein assert statement wäre bestimmt noch cool, damit der test auch sinn macht :D

0