Searching a string in java - java

I am a bit lost here, i have been searching this for sometime.
I have a string $#897950%-1. Now i need to see if it contains a -1 in it. I tried
str.contains("-1") but it did not work. Can someone guide me to solve this ?

I tried it out as:
public static void main(String ar[]){
String str="$#897950%-1";
System.out.println(str.contains("-1"));
}
And See, what eclipse saw me, (It just work fine and gave me true as output):

Check whether you really have this string. May be debugger or output system replaces something.
I have the following code printing 'true'
public static void main(String[] args) {
String str = "$#897950%-1";
System.out.println(str.contains("-1"));
}

Related

Using Selenium with Java to Automate, how to I make an object oriented part of a script "actionable?"

I have automated a new customer form for work, but there are a lot of options and based on how questions are answered , different fields need to be filled out. Rather than just make a copy of the code and make a different script for each option, I'd like to do this by passing values to a class that determines what options are chosen based on what is passed in. I'm trying to figure most of this out myself and I'm somewhat of a n00b, but if someone can get me past the first hurdle, I'd like to tackle the rest of the hurdles myself.
So I want to start by just doing one line of the script this way, and eventually I will do more. Up front, it is going to seem like a lot of code just to do this, but here is the line:
driver.findElement(By.id("OrganizationName")).sendKeys("The Rolling Stones");
Here is what I have so far:
ncformPage1 skifootz = new ncformPage1("Rolling Stones");
skifootz.getOrgname();
That is the part that is in the script. Here is the class I wrote:
public class ncformPage1 {
private String orgName;
public ncformPage1(String on) {
orgName = on;
}
public String getOrgname() { return "driver.findElement(By.id(\"OrganizationName\")).sendKeys(\""
+ orgName + "\");";
}
}
So when I run this, it goes right past that organizationName element and leaves it blank, does all the other elements, and then fails because organization name is a required field. So I added this bit of code here to see what it prints out to the console:
System.out.println( skifootz.getOrgname());
Sure enough, it prints out
driver.findElement(By.id("OrganizationName")).sendKeys("Rolling Stones");
Which is exactly what I want returned. (I think the last semicolon is extraneous in this case, but at least it returned what I wanted!) But it doesn't execute that. I've tried all kinds of stuff to get it to execute, such as removing driver from what is returned and appending it here instead:
driver.skifootz.getOrgname();
but that gives me skifootz cannot be resolved or is not a field. I tried this:
String a = skifootz.getOrgname();
driver.a();
But that just made a get underlined in red saying method a() is undefined for the type Webdriver. So then I changed String a to Webdriver a:
WebDriver a = skifootz.getOrgname();
driver.a();
But now skifootz.getOrgname(); is underlined saying "type mismatch: cannot convert from String to WebDriver." I've been messing around with it for a few days now, and I haven't gotten any closer. Maybe this is an easy solution, but if I can just get this part working then perhaps I can move on to the next phase? This n00b thanks everyone in advance for any help anyone can give.
The method is returning a String type and you expect it to act like a driver object. That part is incorrect.
I think you can write methods to be more like
public WebElement getOrgname(WebDriver driver, String OrganizationName) {
return driver.findElement(By.id(OrganizationName));
}
WebElement a = skifootz.getOrgname(driver);
a.sendKeys("Rolling Stones");
OR
public void TypeText(WebDriver driver, String OrganizationName, String TextToType) {
driver.findElement(By.id(OrganizationName)).sendKeys(TextToType);;
}
in your context this should probably work.
ncformPage1 skifootz = new ncformPage1();
skifootz.getOrgname(skifootz.driver, "OrganizationName");
skifootz.sendKeys("Rolling Stones");
public class ncformPage1 {
private String orgName;
public WebDriver driver = new ChromeDriver(); // I'm assuming this part.
public ncformPage1(String on) {
orgName = on;
}
public WebElement getOrgname(WebDriver driver, String OrganizationName) {
return driver.findElement(By.id(OrganizationName));
}
}

I want to implement this format 12345-1234567-1 in regular expression in java

I want to implement a pakistan's standard format of cnic number which is like this:12345-1234567-1.
But I don't know anything about this. I found the following code for this purpose but it also giving errors in NetBeans.
private void idSearchKeyPressed(java.awt.event.KeyEvent evt) {
String cnicValidator = idSearch.getText();
if (cnicValidator.matches("^[0-9+]{5}-[0-9+]{7}-[0-9]{1}$")) {
idSearch.setEditable(true);
}
else {
idSearch.setEditable(false);
}
}
The pattern is correct. But it can be condensed to this:
^[\\d]{5}-[\\d]{7}-\\d$
Where does idSearch come from? If its not a final member of the class you can't access it in that way. So make sure idSearch is available inside idSearchKeyPressed. Also make sure that there are no trailing spaces or something like that. You can do this by calling
cnicValidator = cnicValidator.trim();
The following example returns true for both regex versions.
public static void main(String... args){
String id = "35241-7236284-4";
System.out.println(id.matches("^[\\d]{5}-[\\d]{7}-\\d$"));
System.out.println(id.matches("^[0-9+]{5}-[0-9+]{7}-[0-9]{1}$"));
}

Dropbox java api - can't use hebrew string

I try to to create DbxEntry object with path that include hebrew character and it's doesn't work.
It's gives me "nullPointerException", Here is example of the code i try to run with main function:
public static void example(String s) throws IOException, DbxException{
auth();
DbxEntry a = client.getMetadata(s);
System.out.println(a.name);
}
public static void main(String [] args) throws IOException, DbxException{
System.out.println("First example: ");
example("/Public");
System.out.println("Working..");
System.out.println("Second example: ");
example("/לשום");
}
The output is:
First example:
Linked account: Itay Velner
Public
Working..
Second example:
Linked account: Itay Velner
Exception in thread "main" java.lang.NullPointerException
at node.Main.example(Main.java:137)
at node.Main.main(Main.java:145)
I tried to look for help in the web and i doesn't find anything yet. if i convert the hebrew String into unicode it's still doesn't work.
I think that there is some problem with the api,
any thoughts?
Thanks,
Itay.

Java (Eclim + Vim) "system.out.print" not working

I am new to Java programming, and today while messing with eclim and vim, I discovered that the System.out.println(); function is not working.
class apples{
public static void main(String args[]){
double tuna = 5.28;
System.out.print(tuna);
}
}
This does not give me a result.
But when I do:
class apples{
public static void main(String args[]){
double tuna = 5.28;
System.out.println(tuna);
}
}
(The only difference is the "println")
I get 5.28, the correct behavior.
Anyone know why this would happen, or is this the way it should be happening?
.println() automatically appends a newline, .print() does not.
System.out is a buffered stream; you need to .flush() for the result of .print() to appear (do it after you print, obviously). The newline in .println() causes the output to be flushed, which is why you don't need it there.

Using a String in run configurations as an argument and using it in an if-statement

The code below works as long as the argument in the run configuration equals "-output". But when the arguments are empty the compiler throws and ArrayOutOfBoundsException.
The point of this piece of code would eventually be to;
- Perform an action when -output is written in the run configurations arguments
- Perform something else if the arguments are empty or different from -output
I found many problems that looked like this one. But I've been working on a solutions for far to long, so I started a new post. Help is very much appreciated.
...
public static void main(String[] args) {
Version_5 v5 = new Version_5("Test");
{
if(args[0].equals("-output")){
System.out.println("It works");
}
}
}
...
You need to check if you have arguments first, that's all.
if ((args.length > 0) && (args[0].equals("-output")) {
...
You might also consider using an argument-parsing library, of which there are several.
What are the extra brackets for?
As you can see String args[] is an array with a specific size. If you don't pass an argument the size is zero. Before you check what is at args[0] check if args has a size with args.length.
if (args.length>0){
//do something
}
else if (args[0].equals("-output")){
System.out.println("It works");
}

Categories