• We've upgraded our forums. Please post any issues/requests in this thread.

Split String - Java

Joined
Jun 30, 2008
Messages
1,135
Likes
67
Location
England
System Name Wasleys PC
Processor Intel i5 2400 3.10GHz
Motherboard Asus P8z68-V
Cooling AC Freezer Pro
Memory Kingston 4GB (2x2GB) DDR3 Hyperx Memory
Video Card(s) HIS ATi 6850 1GB DDR3
Storage Seagate ST3500320AS 500GB Hard Drive SATA II 7200rpm *32MB Cache*
Case Antec 900 with mods
Audio Device(s) On Board
Power Supply OCZ Stealth Xtream 500W
Software Windows 7
#1
Hi all,

I've created a program to split a string which contains latitude coordinates.

Code:
 String[] temp;
        
        String delimiter = "°";
        
        temp = MetaData.latitude.split(delimiter);
 
        for (int i = 0; i < temp.length; i++) {
            System.out.println(temp[i]);
        }
For arguments sake, here is a latitude string: 52°97'0.6822"

The program above will only do up to 97. I was thinking of using a loop, but unsure how to go about doing it.

Any input would be helpful.

Cheers

:toast:
 

Kreij

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#2
You could try using the regex or operator.
Code:
String[] temp = MetaData.latitude.split("°|\\'|\\"")
I'm not sure if I got the regex syntax correct. lol
 

W1zzard

Administrator
Staff member
Joined
May 14, 2004
Messages
17,021
Likes
17,872
Processor Core i7-4790K
Memory 16 GB
Video Card(s) GTX 1080
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 7
#3
you definitely want to use a regular expression for this. to parse the whole thing, dont split
 
Joined
Sep 10, 2008
Messages
532
Likes
157
Location
VA/PA
System Name Freyja
Processor Core i7 3770K
Motherboard AsRock Z77 Extreme4
Cooling Cooler Master Hyper 212 EVO
Memory 16 GB GSkill Sniper
Video Card(s) Diamond Radeon HD 7970
Storage Kingston HyperX 240 GB SSD + Seagate 2 TB HD
Display(s) Dell U2410
Case NZXT Tempest 210
Audio Device(s) Asus Xonar Essence STX
Power Supply Seasonic X-Series 750W
Software Ubuntu 13.04 64 bit
#4
You could try using the regex or operator.
Code:
String[] temp = MetaData.latitude.split("°|\\'|\\"")
I'm not sure if I got the regex syntax correct. lol
What you wrote throws an error when compiling. Should be:
Code:
String[] temp = MetaData.latitude.split("°|\'|\"")
The regex stuff does work though (Thanks for showing that Kreij! Didn't know about that):

 

Kreij

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#5
What you wrote throws an error when compiling. Should be:
Code:
String[] temp = MetaData.latitude.split("°|\'|\"")
The regex stuff does work though (Thanks for showing that Kreij! Didn't know about that):

http://img.techpowerup.org/121120/javatest.png
Yeah, that why I said I wasn't sure about the syntax and if it needed to be double backslashed.
I'm also not sure what he plans on doing with the coordinates once they are split out.
He should put them in a float array if he will need to do calculation with them.
 
Joined
Jun 30, 2008
Messages
1,135
Likes
67
Location
England
System Name Wasleys PC
Processor Intel i5 2400 3.10GHz
Motherboard Asus P8z68-V
Cooling AC Freezer Pro
Memory Kingston 4GB (2x2GB) DDR3 Hyperx Memory
Video Card(s) HIS ATi 6850 1GB DDR3
Storage Seagate ST3500320AS 500GB Hard Drive SATA II 7200rpm *32MB Cache*
Case Antec 900 with mods
Audio Device(s) On Board
Power Supply OCZ Stealth Xtream 500W
Software Windows 7
#6
He should put them in a float array if he will need to do calculation with them.
The answer to my next question :rolleyes:

I'm converting decimal degrees into degrees, minutes, seconds.

Thanks Kreij