• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converting a Split String into an Array

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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(" . ")));

}

}
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 187
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic