Results 1 to 11 of 11
- 05-10-2011, 01:51 AM #1
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
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.
- 05-10-2011, 02:00 AM #2
Sure.
Use the Math class.
If you want to be pointed in the right direction...
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(departmentName = "stop" )
Same for not equals.Java Code:if(blah.equals("foo"))
Java Code:while ( departmentName != "stop" ) // wrong while ( ! departmentName.equals("stop")) // correct
- 05-10-2011, 02:12 AM #3
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
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.
- 05-10-2011, 02:15 AM #4
Booleans are primitives. Like all primitives they do not have methods but you are trying to call a method. Something like:
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.Java Code:int x = 10; int y = 40; (x < y).doSomething();
- 05-10-2011, 02:17 AM #5
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
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.
- 05-10-2011, 03:29 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Ever class needs to have a method called main if you want it to be run.
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.Java Code:public class X{ public static void main(String[] args){ System.out.println("X"); } }
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.
- 05-11-2011, 01:02 AM #7
You need an opening curly bracket not a semicolon.
- 05-11-2011, 01:02 AM #8
It also appears that you actually don't have a main method. You cannot place all that other code inside the main method.
- 05-11-2011, 01:20 AM #9
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
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.:(
- 05-11-2011, 01:23 AM #10
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 }
- 05-11-2011, 02:26 AM #11
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
-
How do I stop the program but not exit the JFrame?
By rajkobie in forum New To JavaReplies: 1Last Post: 04-23-2011, 03:53 PM -
Can't seem to understand why my while loop doesn't exit!
By Vortexnl in forum New To JavaReplies: 5Last Post: 02-11-2011, 08:33 PM -
Fatal Exception, Program will exit(pop up)
By singularity in forum New To JavaReplies: 5Last Post: 05-04-2010, 05:39 PM -
How to exit the program by typing "exit"?
By Laythe in forum New To JavaReplies: 6Last Post: 08-19-2009, 08:32 PM -
How to exit the program..
By coco in forum New To JavaReplies: 1Last Post: 08-01-2007, 05:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks