1

Okay, so I'm currently working on a Computer Science project and I'm coming across an error. Whenever I run these two classes, I get an error where it doesn't read the input and just does nothing after my Scanner gives it a value. I'd appreciate some clarification on what I'm doing wrong. Thanks.

 public Recipe(String Directions, String RecipeName, int CalorieCount, MethodOfCook H){
     directions = Directions;
     recipeName = RecipeName;
     calorieCount = CalorieCount;
     hi = H;
 }

public static void RecipeDisplay(){
    for(int i = 0; i <= Ingred.size(); i++){
        if (Ingred != null){
            Recipe.DisplayHelper();
            System.out.println("===============================");
            System.out.println(recipeName);
            System.out.println("===============================");
            System.out.println("Directions; " + directions);
            System.out.println("Calories; " + calorieCount);
            System.out.println("Cooking Method; " + hi );
        }
    }
}

public static void DisplayHelper(){
    int index = 0;
    System.out.println("Would you like to add a recipe?(Type YES or NO)");
    Scanner wantTo = new Scanner(System.in);
    String bad = wantTo.nextLine();

    if(bad == "YES"){
        index++;
        System.out.println("First we'll start with Ingrediants, Input as many as you like");
        Recipe.addIngrediants();
        Recipe.editIngrediants();
        Recipe.removeIngrediants();

        System.out.println("Great, now lets name your Recipe;");
        Scanner nameDude = new Scanner(System.in);
        String yesName = nameDude.nextLine();
        yesName = recipeName;

        System.out.println("Moving on, Input your Directions;");
        Scanner inputDude = new Scanner(System.in);
        String bud = inputDude.nextLine();
        bud = directions;

        System.out.println("Now input a Calorie Count (Numbers Only!)");
        Scanner numberMan = new Scanner(System.in);
        int jman = numberMan.nextInt();
        jman = calorieCount;

        System.out.println("Now select an Enum Method;");
        System.out.println("Remember your values are;");
        Scanner sc = new Scanner(System.in);
        MethodOfCook bu = hi;
        for (MethodOfCook bum : bu.values())
            System.out.println(bum);

        // read strings from Scanner - display Algorithm value or   throw exception
        while(true){
            try
            {
                System.out.print ("Enter value  ");
                System.out.println("Great, your value is" + bu.valueOf(sc.next()));
                Recipe ji = new Recipe(bud, yesName, jman, bu);
                System.out.println(ji);
            }
            catch (IllegalArgumentException m){
                System.out.println("  Error: " + m);
            }    
        }
     }
     else{
        System.out.println("Okay thanks.");
     }
}

The error is mainly in my DisplayHelper I believe, also I'm trying to link the scanner inputs to my recipe class, so that I can build an ArrayList of recipes in a separate class, thanks in advance! (Sorry for the strange format, it doesn't seem t want to work.)

ya23
  • 14,226
  • 9
  • 46
  • 43
  • 3
    "just does nothing" I bet it doesn't. Does it go into an infinite loop? Does it start doing some heavy IO? Does the program exit? Does it print something to the console? Does it throw an exception? Is it waiting for a contended resource? Are you making it sleep? ... – Andy Turner Nov 09 '15 at 14:14
  • 4
    `if(bad == "YES"){` is not the correct way to compare strings in Java. – Andy Turner Nov 09 '15 at 14:17
  • 1
    What's that while(true) supposed to do? – Aurasphere Nov 09 '15 at 14:20
  • 1
    You should do a Compare of Strings with if(String.equals("YES")) – Lukas Hieronimus Adler Nov 09 '15 at 14:21
  • here probability of going into infinite loop is very high and comparison between the string is also not correct.So its sure you are going to face alot of errors. – Dark Army Nov 09 '15 at 14:34
  • 1
    Style comment (irrelevant to the problem, but could not resist): instead of if (success_case) else use if (!success_case) . It makes code much easier to read. – ya23 Nov 09 '15 at 14:36
  • Ah thanks, but it actually does nothing or atleast in eclipse it terminates itself. How should I stop the infinite loop? – Jackson Honroth Nov 09 '15 at 14:38
  • Why are you declaring 5 different Scanners...? Just use one. – Jaroslaw Pawlak Nov 09 '15 at 14:41

0 Answers0