Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By JosAH

Thread: new project

  1. #1
    maxostarr is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default new project

    i was just messing around and found the last if statement was not working. when i type in "math" it just ends. pleas help. thanks.

    Java Code:

    1. import static java.lang.System.out;
    2. import java.util.Scanner;
    3. public class test {
    4. public static void main(String[] args) {
    5. out.println("hi");
    6. out.println("what's your name?");
    7. Scanner s = new Scanner(System.in);
    8. String input = s.next();
    9. out.println("hi "+ input);
    10. out.println("I can do math. I can / divide * multiply + add - or subtract. witch one do you want? choose / * + -.");
    11. math();
    12. }
    13. public static void math(){
    14. Scanner s = new Scanner(System.in);
    15. String input = s.next();
    16. switch (input){
    17. case "/":
    18. out.println("First Number");
    19. double firstNumber = s.nextDouble();
    20. out.println("Next Number");
    21. double nextNumber = s.nextDouble();
    22. out.println("The answer is " + (firstNumber/nextNumber));
    23. break;
    24. case "*":
    25. out.println("First Number");
    26. double firstNumber1 = s.nextDouble();
    27. out.println("Next Number");
    28. double nextNumber1 = s.nextDouble();
    29. out.println("The answer is "+ (firstNumber1*nextNumber1));
    30. break;
    31. case "-":
    32. out.println("First Number");
    33. double firstNumber2 = s.nextDouble();
    34. out.println("Next Number");
    35. double nextNumber2 = s.nextDouble();
    36. out.println("The answer is " + (firstNumber2-nextNumber2));
    37. break;
    38. case "+":
    39. out.println("First Number");
    40. double firstNumber21 = s.nextDouble();
    41. out.println("Next Number");
    42. double nextNumber21 = s.nextDouble();
    43. out.println("The answer is " + (firstNumber21+nextNumber21));
    44. break;
    45. default:
    46. out.println("Fail");
    47. break;
    48. }
    49. out.println("Now what, none or math?");
    50. String input2 = s.next();
    51. out.println(input2);
    52. if(input2 == "math"){
    53. out.println("/ or * or - or +");
    54. math();
    55. }else{
    56. }
    57. }
    58. }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: new project

    You can't compare two Strings for equality with the == operator; use the String.equals( ... ) method instead (read the API documentation for the String class).

    kind regards,

    Jos
    foulkelore likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    maxostarr is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Replies: 7
    Last Post: 11-27-2012, 10:16 AM
  2. Creating a project in eclipse from existing project
    By Suraiya in forum New To Java
    Replies: 1
    Last Post: 10-08-2011, 09:14 AM
  3. Adding a project to an existing project
    By Seijuro in forum NetBeans
    Replies: 4
    Last Post: 08-08-2010, 10:15 AM
  4. open existing project project ..
    By itaipee in forum Eclipse
    Replies: 1
    Last Post: 12-28-2008, 08:12 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •