Results 1 to 11 of 11
  1. #1
    Beaner462 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Unhappy Using a while loop and using a string variable to exit the program

    I had to delete my work because I got in trouble for plagiarizing my own assignment because of people stealing my code from here and submitting it as their own and my instructor caught it late, so as damage control and to hopefully show the university that this is in fact my own work, I am removing these posts with my code. To the university, my email is brianskinner462@email.phoenix.edu to prove beyond a doubt that this is in fact my own work that I posted here myself.
    Last edited by Beaner462; 05-24-2011 at 01:49 AM. Reason: people plagiarizing my work.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Quote Originally Posted by Beaner462 View Post
    was wondering if someone could point me in the wrong direction.
    Sure.

    Use the Math class.

    If you want to be pointed in the right direction...
    Java Code:
    if(departmentName = "stop" )
    This is an assignment and not a comparison (==). Even if you used == it is not guaranteed to work correctly. Use the equals method instead.
    Java Code:
    if(blah.equals("foo"))
    Same for not equals.
    Java Code:
    while ( departmentName != "stop" ) // wrong
    while ( ! departmentName.equals("stop")) // correct

  3. #3
    Beaner462 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Bah. Was hoping no one caught that before I edited it. I think I'm on to something here, but now I'm getting errors that "boolean cannot be dereferenced". I'm thinking I need to change something from string to boolean or vice versa. It seems all that the textbook chapters reference are double or float numbers....can't seem to find a string modifier for this. Sorry if my lingo is incorrect, this stuff is rather confusing for me as far as names go. I think I have to concept of how it is supposed to work, just not the names.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Booleans are primitives. Like all primitives they do not have methods but you are trying to call a method. Something like:
    Java Code:
    int x = 10;
    int y = 40;
    (x < y).doSomething();
    In the above code the (x < y) results in a boolean and then it tries to call a method. This is what it means by dereferencing.

  5. #5
    Beaner462 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Ok, so I was onto something. Using the corrections that Junky helped me with and changing my boolean variable "departmentName" to a String variable, I successfully compiled the code. However, when I go to run the Payroll class, it displays the error "Exception in thread "main" java.lang.NoSuchMethodError: main" This sounds very basic. Did I forget to declare my main method?? It looks good according to the textbook.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Ever class needs to have a method called main if you want it to be run.

    Java Code:
    public class X{
      public static void main(String[] args){
        System.out.println("X");
      }
    }
    main must be static so the jvm can get to it. From the main method you do what you want. Create objects, make calls on objects, make static method calls, declare variables, etc.

    In your main method, you would create an instance of Payroll with the constructor you wrote. You can then call methods on this created object.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    You need an opening curly bracket not a semicolon.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    It also appears that you actually don't have a main method. You cannot place all that other code inside the main method.

  9. #9
    Beaner462 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default Yikes

    Well if I don't have a main method, how is it that I "cant" put that code into it? I'm thinking I may have to drop this class. This is my fourth week in and I swear I never had this much trouble starting the class but now I feel so overwhelmed.:(

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    You cannot place methods inside other methods. So what you need is:
    Java Code:
    class Foo {
        public static void main(String[] args) {
    
        }
    
        public void methodOne() {
    
        }
    
        public void methodTwo() {
    
        }
    
        // etc
    }

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    In the previous assignment your main method did all the work. What you have to do now is split some of the work of into a different class. Create another class to represent a Department. All the information and methods it needs are spelt out in your instructions. Now in your main method you still get user input but once that has been done you create a new object with the information entered by the user. Then call the method(s) of the object to do what is needed (calculate stuff, print stuff etc).

Similar Threads

  1. How do I stop the program but not exit the JFrame?
    By rajkobie in forum New To Java
    Replies: 1
    Last Post: 04-23-2011, 03:53 PM
  2. Replies: 5
    Last Post: 02-11-2011, 08:33 PM
  3. Fatal Exception, Program will exit(pop up)
    By singularity in forum New To Java
    Replies: 5
    Last Post: 05-04-2010, 05:39 PM
  4. How to exit the program by typing "exit"?
    By Laythe in forum New To Java
    Replies: 6
    Last Post: 08-19-2009, 08:32 PM
  5. How to exit the program..
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 05:56 PM

Posting Permissions

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