File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting a Split String into an Array

 
Bobby Bushell
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 2878
59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9461
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic