aspose file tools*
The moose likes Beginning Java and the fly likes Converting a Split String into an Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting a Split String into an Array " Watch "Converting a Split String into an Array " New topic
Author

Converting a Split String into an Array

Bobby Bushell
Greenhorn

Joined: Mar 12, 2013
Posts: 4
Hi,

I'm trying to take an input from the user in the form of a string variable. This variable is spilt by the "." in the string given. Now here is where I'm having the trouble I'm not sure on how to take a spilt string like this and convert it to and array that I can use for output to the user where I could report single sections of the string. Any advice would help

Thanks

This is my code so far I have tried doing it a couple different ways not sure this one is the best way to go our not

import java.util.Scanner;
import java.util.*;
public class Extensions {
public static void main(String [] args){
Scanner string = new Scanner(System.in);
System.out.println("Please enter your file name to have it tested");
String answer = string.nextLine();
List<String> list = new ArrayList<String>(Arrays.asList(answer.split(" . ")));

}

}
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Look at this line and think carefully about what you are doing in each of its steps.


Look at the docs for String.split(). Compare that to what you stated you're trying to do.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Welcome to the Ranch

I suggest you read this tutorial, where youfind that . means something different in regular expressions. The chance of a file name splitting on " . " is negligible.
Bobby Bushell
Greenhorn

Joined: Mar 12, 2013
Posts: 4
I see well im fairly new to using split ill read up on the tutorial link you posted. But im trying to read the file extension at the end of the input and give that extension back to the user and obviuosly they will be split into periods. Maybe I just need to think of a different way of doing it I just thought I could split the input into an array and output the last part of the filename back to the user printing that partucular element.
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
String.split() takes a regular expression as input, not a string. In regular expressions, dots are not literal - they have special meaning. If you really want to split on a dot, you have to escape it.
Bobby Bushell
Greenhorn

Joined: Mar 12, 2013
Posts: 4
I finally got it to work using String [] arr = answer.split("\\."); and reporting the last element in the array to the user. Spilt just didn't like periods
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2310
    
  49
I just thought I could split the input into an array and output the last part of the filename back to the user printing that partucular element.

You probably can although unless you can guarantee the user will always or won't ever use a full stop at the end of their input you may need to do something a little more complex than simply splitting at a full stop.
The point Campbell was making is if you want to split on a full stop you can't just use ".". There is a way around this which you will see if you read the regex tutorial he supplied a link to and in particular the String Literals page.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Bobby Bushell wrote:I finally got it to work using String [] arr = answer.split("\\."); . . .
Well done That is a correct approach.

You could also use the lastIndexOf method, and then a substring.
Spilt just didn't like periods
That is not the correct explanation at all. It is, as at least two people have told you, that . means something different in regular expressions.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Bobby Bushell wrote:I finally got it to work using String [] arr = answer.split("\\.")...

As Campbell says: well done.

Just FYI: if you don't like all those darn backslashes (and I hate 'em)
answer.split("[.]")
will also work.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
Bobby Bushell wrote:I finally got it to work using String [] arr = answer.split("\\.")...

As Campbell says: well done.

Just FYI: if you don't like all those darn backslashes (and I hate 'em)
answer.split("[.]")
will also work.

Winston


And if you have a lot of characters that are normally special to regex that you want to be treated literally without having to backslash each one, you can enclose them in \Q and \E to tell the regex engine to take them all literally.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting a Split String into an Array