Why is the problem? String Manipulation. Help Please.
Ok I have two while loops that run with two different String arrays. The problem that I have is that I have a String LeadPI that looks like this: "Dr. Ayusman Sen, Pennsylvania State University".
I would like to separate the String where the first half "Dr. Ayusman Sen" = String investigator; and "Pennsylvania State University" = String institution;
basically separate the string into two separate String variables.
can I do this in 1 while loop, and why is my output not functioning correctly?
Lead PI: Dr. Ayusman Sen, Pennsylvania State University //*** String to manipulate***//
// what the output should look like.
Institution: Pennsylvania State University //*** this should be institution ***//
Investigator: Dr. Ayusman Sen //*** this should be investigator ***//
// This is the current output!
Institution: Dr. Ayusman Sen
Institution: Pennsylvania State University //*** this should be institution ***//
Investigator: Dr. Ayusman Sen //*** this should be investigator. ***//
Investigator: Pennsylvania State University
Investigator: Dr. Ayusman Sen
Investigator: Pennsylvania State University
//********************** institution **************************//
int count= 0;
char separator =',';
int index =0;
do
++count;
++index;
index = LeadPI.indexOf(separator, index);
while (index != -1);
String[] insti = new String[count];
index=0;
int endIndex = 0;
for(int i = 0; i < count; i++)
endIndex = LeadPI.indexOf(separator, index);
if(endIndex == -1)
insti= LeadPI.substring(index);
else
insti[i] = LeadPI.substring(index, endIndex);
index = endIndex + 1;
for(int i = 0; i < insti.length; i++)
System.out.println("\tInvestigator: "+insti[i]);
//********************** investigator **************************//
index =0;
do
++count;
++index;
index = LeadPI.indexOf(separator, index);
while (index != -1);
String[] investi = new String[count];
index=0;
endIndex = 0;
for(int i = 0; i < count; i++)
endIndex = LeadPI.indexOf(separator, index);
if(endIndex == -1)
investi[i]= LeadPI.substring(index);
else
investi[i] = LeadPI.substring(index, endIndex);
index = endIndex + 1;
for(int i = 0; i < investi.length; i++)
System.out.println("\tInstitution: "+investi[i]);
Cheers!
One of the problems with the String's split method is that it will nto handle the spaces properly. You will still need to trim it.
In the class, below, I have coded for you two methods that will do what you are asking for.
The first method (split) uses the String's split method, but then has to go through and trim down the strings to remove the space after the comma.
The second method, tokenize, does the same thing, but uses a string tokenizer.
Both work - I was just trying to illustrate both methods. The tokenizer method is about three times faster than the split method (in my tests) - but the split method is a little cleaner (IMHO) - but we're talking sub-millisecond differences in one execution.
You would get the name from the [0] index of the returned String array and the institution from the [1] index, like so:
System.out.println( "Name: " + info[ 0 ] );
System.out.println( "Institution: " + info[ 1 ] );There are some issues with this code... if there is a comma in the institution name or the person's name, if messes things up - if, for example, the persons' name is "Paul Leska, Jr." - the routine splits the string in between Leska and Jr.
The same thing happens in the institution name - if, for example, the name of the place is "University of Minnesota, Duluth" - we tokenize or split between Minnesota and Duluth.
You'll either need to (1) watch your data, (2) make these routines smarter or (3) Change your input to already be split (from the source, perhaps).
Here's the code:
import java.util.StringTokenizer;
public class Splitter {
public static String[] split( String s, char c ) {
String[] chopped = s.split(String.valueOf( c ));
for( int idx = 0; idx < chopped.length; idx++ ) {
chopped[ idx ] = chopped[ idx ].trim();
return chopped;
public static String[] tokenize( String s, char c ) {
StringTokenizer st = new StringTokenizer( s, String.valueOf( c ) );
int count = st.countTokens();
String[] chopped = new String[ count ];
for( int idx = 0; idx < count; idx++ ) {
chopped[ idx ] = st.nextToken().trim();
return chopped;
public static void main( String[] args ) {
String[] info = tokenize( "Michael A. Riecken, Jumping Mouse Software", ',' );
for( int idx = 0; idx < info.length; idx++ ) {
System.out.println( info[idx] );
info = split( "Michael A. Riecken, Jumping Mouse Software", ',' );
for( int idx = 0; idx < info.length; idx++ ) {
System.out.println( info[idx] );
}
Similar Messages
-
String manipulation help please
I need to write a Java program that will find the sum of the digits in a 7-digit integer. The input has to be taken by JOptionPane in 1 single integer variable.
e.g if the input value was 4209632, my output should look like:
Value = 4209632
and the sum of each digits in 1 variable.
Sum of digits = 26
Please help. I need to use modulus function to do this but i dont know how to get all digits individually out of the single variable and add them together.Oh, i didn't notice that you want to use the modulus function...it's not necessary, but if you have to use it for some reason, than use the Integer class to help.
Use the static methord parseInt(String s) of the Integer class to get an int value of a string. parseInt return an int with the integer value of the string s.
Here is the syntax:
int intValue = Integer.parseInt(numericString); -
I have had my mac since December of this year. Suddenly it will not play video with stopping every few seconds to load, and my internet is running slower even though the connection is fine and everythiny else in the house on wifi is working well. Whats the issue?
Run this web based browser scan, it will tell you in red and provide a link to the download page.
https://www.mozilla.com/en-US/plugincheck/
Also another great free program is OnyX, just run everything, cleans your machine out on a system level.
You can cancel the reboot to run another cleaning section, just make sure you reboot at the end before using the computer.
http://www.macupdate.com/app/mac/11582/onyx
Also use the Firefox web browser, it's insanely faster, the fasest of all browsers. The choice of Add-ons is numerous, personalization is just awesome. Everything can be changed, moved around etc. Persona's, themes etc. Just insane.
Not only that, Firefox is for Windows, OS X and Linux, the same browser on all.
https://www.mozilla.com/en-US/firefox/fx/
Do a bandwidth test after you fixed Flash.
http://www.speedtest.net/ -
HT1926 i've tried so many times but the problem still exists! please help!!
i've tried so many times but the problem still exists! please help!!
Could you describe what your problem is, please?
-
Why can't i save my WiFi password in my IPad? Every time i turne my iPad off the password disappear! Help please!!
Create a new SSID and password for your router.
-
i am currently using final cut pro 6 and i have a problem that i can not to do a play out to the dvcam but i can record from the dvcam, can you help please?
I believe there is no "Export to Tape" feature as appeared in legacy FCP.
SORRY! Just noticed you are using FCP 6.
You need to ask this question in the Final Cut Studio forum.
Message was edited by: Ian R. Brown -
Hi, problem occurs when I'm clicking a button or a new url on the current site. For instance, when i clicked that "Ask This" button on this web site, It redirects me to the default homepage. I cant go back and I'm losing the previous web site HELP PLEASE!
Try the Firefox SafeMode. <br />
''A troubleshooting mode, which disables most Add-ons.'' <br />
''(If you're not using it, switch to the Default Theme.)''
# You can open the Firefox 4.0 SafeMode by holding the '''Shft''' key when you use the Firefox desktop or Start menu shortcut.
# Or use the Help menu item, click on '''Restart with Add-ons Disabled...''' while Firefox is running. <br />
''To exit the Firefox Safe Mode, just close Firefox and wait a few seconds before using the Firefox shortcut (without the Shft key) to open it again.''
If it is good in the Firefox SafeMode, your problem is probably caused by an extension, and you need to figure out which one. <br />
http://support.mozilla.com/en-US/kb/troubleshooting+extensions+and+themes -
I have problem with recently with playing DVDs on my iMac. the problem only with DVDs, please anybody can help me solve this problem?
Get a lens cleaner disc. One with brushes and try that. CDs and DVDs use different lenses, so since CDs load and play, I strongly suspect a dirty lens.
-
Hello, I'm new in Apple Support Communities
Most of my e-mail goes to Junk e-mail..! I have tried the rules suggestion as soluiton of the problem, but I don't see any rules to be removed. I went to the Apple store and they couldn't resolve the problem ...Help!!! Any other suggestion?Is it being marked by your email provider? Check the raw headers and see if there is an x-spam header.
If so, you can disable Mail trusting spam headers in the Junk preferences. -
My ipod touch is stuck on a white screen and i did everything hold the home botton and sleep botton togher, but it wont respnd it is just stuck on the white flash scree HELP PLEASE!
- Let the battery fully drain. The charge for an hour and see what happens, Try resetting too:
Reset iPod touch: Hold down the On/Off button and the Home button at the same time for at
least ten seconds, until the Apple logo appears.
- Connct to computer and restore if it shows in iTunes.
- You can try placing the iPod in recovery mode and then trying to restore. See:
iPhone and iPod touch: Unable to update or restore
- Also see:
iPod touch: Hardware troubleshooting
Frequenly the white scree is due to a hardware problem. -
My Mac Book Pro was really slow at everything and would kick me out of my photos every time i got on them. now it won't even allow me to log on. i put my password in and the screen will flash white and go back to the login page. HELP please...
There is nothing wrong with your Dell, it will work fine with any MacbookPro. I have been using Dell displays for over 12 years with many different Mac models. I have two 21" Ultra Sharp displays working side by side to design a Keynote presentation right now.
The issue your having is with the way Keynote takes control of the video output to both displays, it sends the presentation signal to one and the presenter display to the other, this is set up in;
Keynote preferences > Presenter display.
If you want to show a wesite or another app on either display, use application switcher:
press the the command key on the keyboard, then the tab key; a row of applications will show what applications are running, choose which one you want to show. Use command > tab to return to Keynote. -
I can't figure out how to get the music on my iPhone5s to shuffle. If I use the arrows to skip a song, it shuffles through a handful of the same songs then goes back to the song menu. Help please!
Un-sync all your music to remove it from the device, then restart (hold down the home button along with the sleep/wake button until you see the apple, then let go). Now re-sync your music.
-
I need to export a photo at around 1meg file size,and dont seem to be able to do it in the presets can anyone help please ,I shoot on a canon 5d
I believe Frank is referring to the end use (e.g., for print or on-screen use).
Until he returns, I can tell you that I ran a quick test using an image from the Aperture 3 training book library which was as follows:
Camera = Canon EOS 5D Mark II
Master resolution = 5616 x 3744 (21 MP)
I was able to export to a JPEG with end file size of 1 MB as follows:
Export Presets drop-down > select 'Edit' option
Selected preset name 'JPEG - 50% of Original Size' > change percentage value to 40% > change DPI value to '300' > click OK to set as current default > Export version to desktop.
The exported JPEG has a resolution of 2246 x 1498 and appears on screen to have the same image quality of the original in Aperture.
Note - I did not change the default Image Quality slider in the Export Preset dialog (which left it at 10).
Basically, you will need to play around with the settings until you find the appropriate values to arrive at an image version you want. -
Just upgraded my iPad to IOS5 and when the download was completed, a window said that there was an error and it could not restore. Now I have a black screen with the Apple logo and the circle lines. None of the buttons respond. Help, please!!
Reset the iPad by holding down on the sleep and home buttons at the same time for about 10-15 seconds until the Apple Logo appears - ignore the red slider - let go of the buttons. Maybe this will let the iPad reboot and then you may need to restore again but see if this helps to start the iPad first.
-
My music seems to be frozen - it's trying to play something but just keeps scrolling my library. I've tried resetting but the problem persists. Help !!
Make sure that all of your music is in iTunes. Once you have made sure that all of the music that is on your phone is on the computer that you sync with, go to Settings>General>Usage, slide your finger from left to right across the music portion, delete the library, restart the phone, then sync the library back to your phone via iTunes.
-
I have bought and downloaded the new iphoto but it is not installing. I have restarted my computer and the problem continues. Help!
can you describe exactly what is happening? And give exact error messages
LN
Maybe you are looking for
-
Imac as a monitor for a MacBook Pro
I would like to use my 6 year old Imac as a monitor for my 1 yr old MacBook Pro. Any suggestions as to how to hook this up?
-
Hi, I want to get the report in which i can get exact release date of PR. In ME5A / ME5j release date is not exact date at which the PR was released. Regards Kalpesh
-
Oracle 9i patches for linux 4.0 Advance server
how do i download the required patches of oracle 9i for linux 4.0 Advanced server. I am 9i oracle certified professional. Currently I am not working. how it is possible for me to dowload pathces through www.metalink.oracle.com or is there any other w
-
Hi all, I have problem uninstalling the SAP BPC 7.0 on Microsoft Platform. When I uninstall through Windows Add/Remove Component, the wizard stuck at the Removing Sample OLAP database? Any idea what am I missing out here? Is there a uninstall guide I
-
i havent had any problems with my camera up until now. i have had the Curve 8310 for 3 months and now all of a sudden the camera wont zoom. can anyone help me out with that. Solved! Go to Solution.