Hey it's me the programming noob again and I need some help with my code!

Discussion in 'Programming' started by Lightcab, May 5, 2015.

  1. So, Hey everyone it's me Lightcab. I just started programming this week and I'm totally enjoying it but I'm getting a problem. My program is not running properly. First take a look at my code. So, whenever I run my program it prints "What do you want to do" and then when i type something like add or mul, it does nothing. I didn't get any errors as well. So guys please help this programming newbie.
     
  2. You need to make a loop so once you get past the first question, the application will not exit and wait for the next response.
     
    • Like Like x 1
    • Optimistic Optimistic x 1
  3. Hmmm....Ok let me try. But where should i add the loop and thanks for the reply dude :).
    (EDIT: I used switch case and now it is fine but can you still tell me where am i supposed to add loop)
     
    #3 Lightcab, May 5, 2015
    Last edited: May 5, 2015
  4. between
    10 System.out.println("What do you want to do: ");
    and
    35 }
     
  5. Thanks for the reply dude but can you please explain more. I mean I didn't get it :) and once again thanks for the reply.
     
  6. Heya, good to see you started!

    First of all hit CTRL + SHIFT +F every once in a while if you're using eclipse, or CTRL + ALT + L in IntelliJ, it auto cleans up your code, meaning you don't really have to worry about it and your code still looks pleasant!

    You can make a scanner loop as follow;
    Code (Text):
    Scanner sca = new Scanner(System.in);

    while(sca.hasNext()) {
       
    }
    Scanner.hasNext() will wait until user input is available and pause your program for a little while if there is none, as soon as input is available it'll continue your while loop and you can work with the input data. Keep in mind that while(Scanner.hasNext()) won't ever terminate so it's advised to add a breaking condition in there somewhere (like a stop word). If you can't get a loop condition to go false you can always leave the loop by executing the 'break;' command.

    Also, in the OP you compare Strings via the == operator. Strings should be compared using the .equals() method provided on the String object. Comparing non primitive objects (everything thats not int, float, double, short, boolean, byte, long, or char) using == will cause the program to check if they have the same address in memory rather than if they have the same value (to check if they are exactly the same object). In your case;
    Code (Text):
    String s = sca.next();
    if(s.equals("add")) {
              // etc
    }

    Good luck!
     
    • Like Like x 2
  7. Thanks dude. Your explaining capability is really good and thanks for the extra tips. I don't know much about loops so I think that it is gonna be my next topic and once again thanks :).
     
    • Friendly Friendly x 1
  8. the loop while can crash your program if she doesn't have end; use her with caution .
     
  9. while loops are perfectly safe and will not crash your program. What the poster above probably meant is that if for some reason you get stuck in a while loop that never terminates (i.e. the condition will never be false) nor is there any form of user input being read and or processed the program will never exit this loop and will thus become non responsive and you'll have to crash it yourself.
     
    • Agree Agree x 2
  10. Thanks again dude.
     
  11. I smell homework
     
    • Funny Funny x 2
  12. I believe this should work for you:
    Code (Text):

    package prac1;

    import java.util.Scanner;

    public class Main {
        private static boolean running = true;

        public static void main(String[] args){
            while(running) {
                Scanner sca = new Scanner(System.in);
                double a, b, c, d;
                String s;
                System.out.println("What do you want to do: ");
                s = sca.next();
                if (s.equals("add")) {
                    System.out.println("Enter the first number: ");
                    a = sca.nextDouble();
                    System.out.println("Enter the second number: ");
                    b = sca.nextDouble();
                    c = a + b;
                    System.out.println("The sum of " + a + " and " + b + " is " + c);
                    continue;
                }
                if(s.equals("mul") || s.equals("multiply")) {
                    System.out.println("Enter the first number: ");
                    a = sca.nextDouble();
                    System.out.println("Enter the second number: ");
                    b = sca.nextDouble();
                    c = a * b;
                    System.out.println("The product of " + a + " and " + b + " is " + c);
                    continue;
                }
                if(s.equals("quit") || s.equals("exit")) {
                    running = false;
                    break;
                }
            }
        }
    }
     
     
    • Like Like x 1
  13. So continue; is the only thing I should add. Hmm...Ok and thanks :).