Java - Zahlen aus String?
Wie kann ich aus einem String die Zahlen zählen?
Hier ist was ich habe:
Was muss ins if rein oder sollte man das anders lösen?
for(int i = 0; i < test.length(); i++) {
if( ? ){
count ++;
}
}
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();
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".
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
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();
}
}
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