Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes If String Contains (symbols) then call main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "If String Contains (symbols) then call main method" Watch "If String Contains (symbols) then call main method" New topic
Author

If String Contains (symbols) then call main method

Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
Ok so basically I have an output from a file that needs shortening down and specifying as there is too much irrelevant data.

To do this I've done a bit of research and have come out with something along the following lines.

Please can someone help

kind regards S

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

?, /, =/ *, ", $," put these symbols into an array. and then check your source has the particular character or not using contains/indexOf method of String in a loop.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please don't bother posting code that won't even compile.
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
Hi All, the code wasn't supposed to compile, it was simply meant to give an idea of what i am trying to achieve.

I have a document that I am trying to extract a list of variables from, these variables are stored in instances of a string i have called resourceline. However i only want to add the variable to the array if it does not contain any symbols and also if it does not already exist.

the resourceline value will be different in most instances so i cannot pinpoint if it does or does not contain any symbols.

All i am asking is how I should correctly write if my resourceline variable contains any symbols.

regards S
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18511
    
  40

Nick Rowe wrote:
All i am asking is how I should correctly write if my resourceline variable contains any symbols.


What you are asking for, is pretty straightforward. Why not take an attempt at it first?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
Normally I would say yes if the string was constant. However the string changes continuously so i cannot say something simple along the lines of something like.



regards Nick
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

But you're not looking for equals, you're looking for a contains, or a regex. So is there anything else in the string api that might work?
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
A regex solution would make things far more difficult as i am already using a regex to capture the string that I want to investigate.

The code will need to be used within my find method in the forms of an if statement using something that searches for a character(S) then dependant on if that character exists within the variable resource line at that time and if it already exists within the array, a series of events will follow.

So would instances of below work
boolean checkContains1=resourceline.contains("$");
boolean checkContains2=resourceline.contains("=");
boolean checkContains3=resourceline.contains(""");
boolean checkContains4=resourceline.contains(">");
boolean checkContains5=resourceline.contains(":");


Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Why don't you try them, and see if what you were expecting is output.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
Ok so basically I've tried the code below and get one error from the compiler saying that != is an illegal start of an expression. This is on the
if (checkContains1 || checkContains2 || checkContains3|| checkContains4|| !=true) line



Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

This:


Needs to look like this in Java:


You have to specify a value for each part of your condition.

Hunter
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Don't use != true, use !:
This is identical to the following (De Morgan):


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
so could i also try if

if (true(checkContains1 || checkContains2 || checkContains3 || checkContains4)) ???
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Nick Rowe wrote:so could i also try if

if (true(checkContains1 || checkContains2 || checkContains3 || checkContains4)) ???


Just leaving the ! off would be checking if they were true.

Check True:


Check False:


Hunter
tushar bhasme
Ranch Hand

Joined: Feb 11, 2008
Posts: 50
something wrong with the logic...


first "if" checks if resourceline != null, and then, if its null, you are adding it to the array list???
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Nick Rowe wrote:A regex solution would make things far more difficult [...]

Not really.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617


Again: even if you're posting pseudo-code, please check that it makes sense. It throws people off to have to understand whatever notation it is you're trying to use *and* solve the problem at the same time. We don't know if you believe what you're writing will work or not if it won't even compile.

Stick to posting actual code. Please.
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
This is probably a stupid question but im wondering if there is a specific way of calling back a method from another method. My program only has one class to Im not calling it from anywhere else.

I've tried
public static void main(String[] args);
main();
main(String[] args);

and the compiler isn't having any of it.

regards S



I'll post my code below

 
Don't get me started about those stupid light bulbs.
 
subject: If String Contains (symbols) then call main method
 
Similar Threads
Calling the method from another method within the same class
Returning A List Of Variables From A Folder Of Documents And Returning Them Into A New Document
Why is my string still writing data to my file that i've asked it not to?
Dumping A string value???
Substring capture To Write A list Of instances in A New Document