0

I have a text file that is has the following contents(portion):

attributes_Accepts_Credit_Cards,
attributes_Accepts_Insurance,
attributes_Ages_Allowed,
attributes_Alcohol,
attributes_Ambience_casual,
attributes_Ambience_classy,
attributes_Ambience_divey,
attributes_Ambience_hipster,
attributes_Ambience_intimate,
attributes_Ambience_romantic,
attributes_Ambience_touristy,
attributes_Ambience_trendy,
attributes_Ambience_upscale,

I am trying to attain an output, such that I would get a list with the following appended characters:

business['attributes_Accepts_Credit_Cards'],
business['attributes_Accepts_Insurance'],
business['attributes_Ages_Allowed'],

However at the moment my code outputs in the following manner:

business['attributes_Accepts_Credit_Cards'],
business['
attributes_Accepts_Insurance'],
business['
attributes_Ages_Allowed'],
business['
attributes_Alcohol'],
business['
attributes_Ambience_casual'],
business['
attributes_Ambience_classy'],
business['
attributes_Ambience_divey'],
business['
attributes_Ambience_hipster'],
business['
attributes_Ambience_intimate'],

code:

public class TestScanner {

    public static void main(String[] args) throws FileNotFoundException {

        Scanner scanner = new Scanner(new File("/list.txt"));
        scanner.useDelimiter(",");
        while(scanner.hasNextLine()){
            System.out.println("business['"+ scanner.next()+"'],");
        }
        scanner.close();
    }

}
user
  • 854
  • 2
  • 12
  • 28

1 Answers1

1

You need to add the newline character to your delimiter:

scanner.useDelimiter(",\n");
shmosel
  • 49,289
  • 6
  • 73
  • 138
  • then the output became: business['attributes_Accepts_Credit_Cards, attributes_Accepts_Insurance, attributes_Ages_Allowed, attributes_Alcohol, – user Dec 14 '16 at 00:25
  • 1
    @user Try `",\r\n"`. – shmosel Dec 14 '16 at 00:26
  • 2
    @user Careful, because the newline type can vary between operating systems. Have at look [here](http://stackoverflow.com/questions/20056306/match-linebreaks-n-or-r-n) for some better solutions (e.g. `",(\r|\n|\r\n)"`). – shmosel Dec 14 '16 at 00:28