Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

search long string for several strings

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a lot of help.

I basically need to return defined strings based on string inputs seperated by ";". I have an excell sheet that has several people with some at same remote locations and other at different locations.

i.e.

"jones, carla h.; mackafee, stein j.; example, crazy i." - site ga030

"excuses, lame I; people, helpful u" - site al211

So I need to just copy and paste a much longer list of people into a program and have it provide the site codes/defined strings based on any name given it.

Preferred output sequence

...
"Please enter user names and press return: "

jones, carla i; rufus, borom o.; peoples, sandy k; jacobs, ron p.; lori, philip p.


\\jones, carla i; rufus, borom o.; ga030
\\peoples, sandy k; - al211
\\jacobs, ron p.; lori, philip p. - tx565

"The site codes are:
ga030
al211
tx565


"would you like to run the program again?"

Please helpppp. I have been trying this but I dont know how to search for substrings and only print a matching substring's defined return once - like I dont want:

ga030
ga030
al211
tx565
tx565

Please heeelp.

mo
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use String.contains(String substring)
to see if one string contains the substring specified.

and for the site 2343 or whatever, if there is alwasy a '-' before
the 'Site sitenumber' you can just get the substring like so.



be sure to import java.util.StringTokenizer

Justin Fox
 
Marshal
Posts: 70378
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Justin Fox:
be sure to import java.util.StringTokenizer

Justin!! You know StringTokenizer is classed as legacy code. You need regular expressions which are described in the Java Tutorial here, and probably the String#split method.

And, Maurice, welcome to JavaRanch. Find the thread about e-mail addresses and find Rob Prime's reply and the regular expression he quotes. What you need is probably less complicated than that.
 
maurice williams
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thansk for the suggestions but.... it's not quite what I need and I need to make my self more clear - this is something I am thinking about:


import java.util.Scanner;

public class EmailToSite
{
public static void main (String [] args)
{

Scanner scan = new Scanner(System.in);
String contacts;

System.out.println("Please enter the email addresses ");
contacts = scan.next();

\\i dont know the code to how to just search for just part of the string
\\to return the sitecode/predefined string literal

if(contacts.equals("jones, carla i;")|| contacts.equals("rufus, borom o."))
System.out.print("GA030);
if(contacts.equals("peoples, sandy k."))
System.out.print("AL211);


\\and so on and so on


}}

Help...
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the tutorial Campbell just cited. Searching N samples for M things can become very involved in some advanced literature and is not going to get you unstuck. The tutorial will, if you stay with it. There is code to get what was matched ( that means it found one ) as well as several methods that will be discussed in the tutorial that will do what you want.
 
maurice williams
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried running the code of the RegexTestHarness in my compiler and recieve:

----jGRASP exec: javac -g C:\Documents and Settings\mrwillia\My Documents\My Music\My Playlists\RegexTestHarness.java

RegexTestHarness.java:31: cannot find symbol
symbol : class Console
location: package java.io
import java.io.Console;
^
RegexTestHarness.java:38: cannot find symbol
symbol : class Console
location: class RegexTestHarness
Console console = System.console();
^
RegexTestHarness.java:38: cannot find symbol
symbol : method console()
location: class java.lang.System
Console console = System.console();
^
3 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.


How do I fix?
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by maurice williams:


How do I fix?



The Console class and console() method are new in JDK 1.6 (Java 6); your compiler must be Java 5 or older. You'll need to install the newer JDK to use this code.
 
Campbell Ritchie
Marshal
Posts: 70378
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whihc version of Java are you using? Console only works in Java6.
 
maurice williams
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks - it seems like I am making progress finally.

How do I go about removing the whitespace " " from a string?

Like if I put in "akldjf laksdjf laskdfj lalkjf"; how do I make a new string to only be"akldjflaksdjflaskdfjlalkjf"???
Thanks
 
maurice williams
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code:
import java.util.Scanner;

public class EmailToSite
{
public static void main (String [] args)
{

Scanner scan = new Scanner(System.in);

System.out.println("Please enter the email addresses ");
String after, contacts = scan.next();

//replace/delete whitespace
after = contacts.replace(" ","");
System.out.println(after);

int index = after.indexOf("asdf");
System.out.println(index);
if(index >= 0) System.out.println("AL006");



}
}

Here is my output:


----jGRASP exec: java EmailToSite

Please enter the email addresses
ajklsdfh ajkh dkj asdf
ajklsdfh
-1

----jGRASP: operation complete.



y???its maddening - do i need an upgrade? - does this code work on your compilers?
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is here:

What String exactly does contacts refer to? What do you think Scanner.next() returns? What do the javadocs say it returns?
 
maurice williams
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Contacts referes to the string literal that is given to the scanner via system.in and next (i guess) only executes on the next token or string not seperated by a space...

If this is true, How do i get it to read the entire string?

hmmm, got me thinking now...
 
maurice williams
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I got it:
import java.util.Scanner;

public class EmailToSite
{
public static void main (String [] args)
{

Scanner scan = new Scanner(System.in);

System.out.println("Please enter the email addresses ");
String after, contacts = scan.nextLine();

//replace/delete whitespace
after = contacts.replace(" ","");
System.out.println(after);

int index = after.indexOf("asdf");
System.out.println(index);
if(index >= 0) System.out.println("AL006");



}
}




----jGRASP exec: java EmailToSite

Please enter the email addresses
askldjfh jdkladhlfkajsfh asjkldfasdh fskldjhsf jsdkalfsdh fklsjadfhsd sjklfh jksdfh alskjfh asklfjhsdfkljhasfjkldhfjkasdfh asdkjfha sdfkjsdh fkjasdfh la jkd asdf
askldjfhjdkladhlfkajsfhasjkldfasdhfskldjhsfjsdkalfsdhfklsjadfhsdsjklfhjksdfhalskjfhasklfjhsdfkljhasfjkldhfjkasdfhasdkjfhasdfkjsdhfkjasdfhlajkdasdf
108
AL006

----jGRASP: operation complete.


whoo hoo - thanks would have not figured that for a long time
reply
    Bookmark Topic Watch Topic
  • New Topic