I have the log file blackjacksample.txt

$10000  3
[2H, 4C, KC, 5D] $10 [QS, QD] $5 [AC, 3C, 4D] $25 [10H, 5C, 7H]
[7C, 5C, 10H] $20 [JH, 3C, 8H] $15 [3H, 6D, JC] $5 [AH, KH]
[9C, JS] $40 [5C, 7H, 3D, KS] $30 [9C, 10D] $50 [AD, 10C]
[10H, KS] $50 [2H, 2C, 6S, 5D, 7C] $5 [9D, 9H] $30 [3S, 7H, 7S]

^ not Java code, it just looked weird with (TEX) brackets, so I put in (CODE) brackets. It just a txt file.


Player 1: Hand is [2H, 4C, KC, 5D] and bet is  $10
Player 1: [QS, QD] and bet is  $5
Player 1: [AC, 3C, 4D] and bet is  $25
Dealer Hand: [10H, 5C, 7H]

And I need to get rid of the brackets, dollar sign commas. I was able to get rid of the first bracket. But I cant get rid of everything else, I have tried other delimiters but didnt work at all.

I think I need multiple loops to get rid of the commas, dollar sign, and end bracket separately.

Heres the code I have so far.

package dsa.blackjack;

import java.util.*;
import java.io.*;

public class Blackjack {

    /* public Blackjack(){

    } */

    public static void main(String args[]){

    public static void readFile(){
        File file;
        file = new File("C:\\DSA\\BlackjackProject\\src\\dsa\\blackjack\\blackjacksample.txt");

        try {
            Scanner scanner = new Scanner(file);

            while (scanner.hasNext())
                //System.out.println(" *********** ");
                //System.out.println(" *********** ");

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
        } catch (FileNotFoundException e) {

    private static void parseLine(String line)
        if (line.length() > 0  && line.charAt(0) == '$')
            Scanner firstLine = new Scanner(line);
        else if (line.length() > 0  && line.charAt(0) == '[')
            // subsequent line
            Scanner gameLine = new Scanner(line);

                //System.out.println(gameLine.next() + "^^^^^^^^^");


I wanted to know if you guys can give me some tips and point me in the right direction and what not.

Help would be Greatly Appreciated =D

Edited by mike_2000_17: Fixed formatting

8 Years
Discussion Span
Last Post by .11

> I think I need multiple loops to get rid of the commas, dollar sign, and
> end bracket separately.

Not required IMO; a single application of the replaceAll method of the String class on each line read should do the job. Use a regular expression for your task.

public class TrioTest {
   public static void main(final String[] args) {
      String s = "[2H, 4C, KC, 5D] $10 [QS, QD] $5 [AC, 3C, 4D] $25 [10H, 5C, 7H]";
      Pattern pat = Pattern.compile("[$,\\[\\]]");
      Matcher mat = pat.matcher(s);
      String newS = mat.replaceAll("");
      System.out.println("Old S: " + s + "\nNew S: " + newS);


Or, simply use the `replace' method of the String class to strip off each unrequired character from the target string.


Late reply, but thanks a lot! I should be able to get the Data needed. I decided to leave the $, that we I know it's a bet and to move on to the next player.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.