Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to split parts of a String but ignoring certain parts?

  1. #1
    Join Date
    Aug 2020
    Thanked 0 Times in 0 Posts

    Question How to split parts of a String but ignoring certain parts?

    Hello, I've decided to try and mess around with a dataset I've found online and when it came to writing the code for it, my typical method of using a comma to split between each segment doesn't work for this dataset.

    An example of a line within the dataset:
    The Legend of Zelda: Ocarina of Time, Nintendo 64,"November 23, 1998","As a young boy, Link is tricked by Ganondorf, the King of the Gerudo Thieves. The evil human uses Link to gain access to the Sacred Realm, where he places his tainted hands on Triforce and transforms the beautiful Hyrulean landscape into a barren wasteland. Link is determined to fix the problems he helped to create, so with the help of Rauru he travels through time gathering the powers of the Seven Sages.",99,9.1

    The data is organized as such:
    name,platform,release_date,summary,meta_score,user _review

    I've tried finding a way to hopefully have it ignore commas within quotations as it seems the commas are within quotations. But no luck.

    So the first thing I noticed was that there's a comma within the date, and the program more than likely wouldn't ignore it how I have it set up. Does anyone know a way around this? Here's my code below:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    public class GameDataTests {
        public static void main(String[] args) {
            File file = new File("all_games.csv");
            Scanner input = null;
            GameEntry[] gameEntries = new GameEntry[231];//Array to hold the objects
            try {
                input = new Scanner(file);
            } catch (FileNotFoundException e) {
                System.out.println("File not found.");
            input.nextLine();//to read in the header of the text file
            int index = 0;
            while(input.hasNext()) {
                String line = input.nextLine();//each line in the file is read in as a single String segment here
                String[] fields = line.split(",");//the string segment is then 'split' at every ',' char (see .cvs file)
                gameEntries[index++] = new GameEntry(fields[0], fields[1], fields[2], fields[3], Integer.parseInt(fields[4]), Double.parseDouble(fields[5]));
            //At this point, there should be an array of GameEntry objects

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    On vacation
    Thanked 2,678 Times in 2,628 Posts

    Default Re: How to split parts of a String but ignoring certain parts?

    Is the problem due to the separator characters are being used in the data?
    Can you change the separator characters to something that is not in the data?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: October 26th, 2012, 02:19 PM
  2. Replacing parts of a String using line.replaceAll
    By brandonSMC in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 27th, 2012, 04:07 PM
  3. Beginner having some trouble with two parts in my code
    By csprung in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 25th, 2012, 04:23 AM
  4. How do I parse an input file in two parts?
    By bryanboy in forum Java Theory & Questions
    Replies: 4
    Last Post: November 12th, 2011, 02:20 AM
  5. Repeating different parts of a program
    By swiftxjames in forum Loops & Control Statements
    Replies: 3
    Last Post: November 17th, 2010, 04:29 PM

Tags for this Thread