0

I'm currently trying to create a little program which creates Recipes as csv files. That worked fine till the point where I want to add an Ingredient with spaces in between for example: 'real big bananas'. Whenever I write something like this or with underscores or with minus it enters null instead of the word I wrote. I have a class Ingredients(double amount, String unit, String ingredient, String tags) with getter and setters for all of the variables. My Code for creating a csv

public static ArrayList<Ingredients> createIngredientList() {

    Scanner scan =  new Scanner(System.in);
    ArrayList<Ingredients> list = new ArrayList<Ingredients>();
    char quit = 'Y';
    String unit, ingredient, tags;
    double amount;

    while(quit == 'Y') {
        System.out.print("Please use underscores instead of spaces e.g. real_big_boats.\n");
        System.out.print("Amount: ");
        amount = scan.nextDouble();

        System.out.print("Unit: ");
        unit = scan.next();

        System.out.print("Ingredient: ");
        ingredient = scan.nextLine();

        System.out.print("Tags (e.g. tag;tag;tag): ");
        tags = scan.nextLine();

        list.add(new Ingredients(amount, unit, ingredient, tags));

        System.out.print("More Ingredients? (Y/N) ");
        String s = scan.next();
        s = s.toUpperCase();
        quit = s.charAt(0);

    }

    return list;

}

You can find the whole class in my pastebin below. https://pastebin.com/vi09kGqi

user8953265
  • 21
  • 1
  • 10

2 Answers2

1
    while (quit == 'Y') {

        System.out.print("Please use underscores instead of spaces e.g. real_big_boats.\n");
        System.out.print("Amount: ");
        amount = scan.nextDouble();
        scan.nextLine() ;

        System.out.print("Unit: ");
        unit = scan.next();
        scan.nextLine() ;

        System.out.print("Ingredient: ");
        ingredient = scan.findInLine("(\\s|\\S)*");
        scan.nextLine() ;

        System.out.print("Tags (e.g. tag;tag;tag): ");
        tags = scan.next();
        scan.nextLine() ;

        list.add(new Ingredients(amount, unit, ingredient, tags));

        System.out.print("More Ingredients? (Y/N) ");
        String s = scan.next();
        scan.nextLine() ;
        s = s.toUpperCase();
        quit = s.charAt(0);
    }
theRiley
  • 809
  • 9
  • 15
0

This is due to a known problem with Scanner in java. Check the link here: Scanner is skipping nextLine() after using next() or nextFoo()?

You can use a blank scanner.nextLine() after every scanner.nextDouble() or scanner.next() to overcome this.

   public static ArrayList<Ingredients> createIngredientList() {

    Scanner scan =  new Scanner(System.in);
    ArrayList<Ingredients> list = new ArrayList<Ingredients>();
    char quit = 'Y';
    String unit, ingredient, tags;
    double amount;

    while(quit == 'Y') {
        System.out.print("Please use underscores instead of spaces e.g. real_big_boats.\n");
        System.out.print("Amount: ");
        amount = scan.nextDouble();
        scan.nextLine();

        System.out.print("Unit: ");
        unit = scan.next();
        scan.nextLine();

        System.out.print("Ingredient: ");
        ingredient = scan.nextLine();

        System.out.print("Tags (e.g. tag;tag;tag): ");
        tags = scan.nextLine();

        list.add(new Ingredients(amount, unit, ingredient, tags));

        System.out.print("More Ingredients? (Y/N) ");
        String s = scan.next();
        s = s.toUpperCase();
        quit = s.charAt(0);

    }

    return list;

}

Output:

Please use underscores instead of spaces e.g. real_big_boats.
Amount: 111
Unit: 111
Ingredient: a b c
Tags (e.g. tag;tag;tag): a b c
More Ingredients? (Y/N) N
Ingredients [amount=111.0, unit=111, ingredient=a b c, tags=a b c]

Another solution would be to accept every input as String by using scanner.nextLine() and then parsing it to the desired type.

S.K.
  • 3,337
  • 2
  • 13
  • 27
  • Unfortunately this ain't the answer. In my opinion there is no problem with the scanner at least not this problem :D When I enter single words for ingredients it will do what I want. Just not for multiple words with some character between them. – user8953265 Sep 09 '18 at 12:23
  • It worked fine for me with spaces between words for ingredients and tags. I have pasted the output here. – S.K. Sep 09 '18 at 12:40
  • So where did u paste the scanner.nextLine() than? behind the ingredients = scan.nextLine(); or what? – user8953265 Sep 09 '18 at 13:03
  • After `scan.nextDouble();` and `scan.next();` – S.K. Sep 09 '18 at 13:06
  • When i do as you recommend and with the same input as u had in your example I get: amount, unit, ingredient, tags 111.0,111,null,a b c – user8953265 Sep 09 '18 at 13:11
  • strange...try debugging your program. check the value of ingredient at runtime. – S.K. Sep 09 '18 at 13:14
  • Input: "111,as,a v dc,asd da f" Output: "111.0,as,null,a v dc" It seems like the ingredient section will be skipped and tags = ingredient – user8953265 Sep 09 '18 at 13:20
  • did u keep a breakpoint at `list.add(new Ingredients(amount, unit, ingredient, tags));` and saw the values while debugging? – S.K. Sep 09 '18 at 13:27
  • I did that and it seems there's no problem with the scanner but with the list because the tags, won't be added to the list. – user8953265 Sep 09 '18 at 13:49