• Post Reply Bookmark Topic Watch Topic
  • New Topic

trying to create a simple class in Netbeans  RSS feed

 
dave forde
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I'm new to java and trying to learn the ropes on Netbeans!

What I want to do is make a class which does the following

Asks for and reads a first number on the command line.
Asks for and reads a second number using a JOptionPane
Asks for and reads your name using a JOptionPane

I want it to output the name and the remainder of the first number divided by the second number.

Sorry I know this seems really easy but I'm new to this haha!

Thanks a lot!
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

What have you got so far? Post it here and be sure to UseCodeTags (←click).
 
dave forde
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
apologies completely forgot to post that!



I think a fair bit of it is wrong but I'm not sure where!

Also, I know I've got the remainder formula wrong, I can't figure out how to get a remainder of first (divided by) second!

Thanks
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the remainder, use the mod operator, e.g. 5 % 2 == 1;
 
dave forde
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, I'm getting an error on the line:



(illegal start of type)

and errors on the lines



(cannot find symbol: variable JOptionPane)
 
dave forde
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Knute, would that just be ?

Thanks
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need the outer class Calculating.

You need to add an import statement for the JPanel. Give those a try.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dave forde wrote:Thanks Knute, would that just be ?


Yes, that's the way to do it if first and second are both ints. But there is a problem with second you may run into.
 
dave forde
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You don't need the outer class Calculating.

You need to add an import statement for the JPanel. Give those a try.


I've tried the following for an import statement:



But I'm getting the error "illegal start of type, <identifier> expected"

Sorry this must seem so basic hahaha
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imports have to be the first statements; only a package statement can go before import statements.
 
dave forde
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Imports have to be the first statements; only a package statement can go before import statements.


Ah that's got rid of the error, thanks!

I've had a read of it and moved some bits and pieces around, and added an answer as well as remainder (just for a more detailed output)
this is what I have now:



the only remaining errors I have are:





(both "non-static variable input cannot be referenced from a static context")

 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is caused by having too much in the main method. Look at our FAQ.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For reference, private Object input; is non-static and main() is static, but Campbell is right. You really should get in the habit of taking things out of the main() method. It can be as simple as this:

 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get the main method even shorter:-
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!