• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I convert scanner input into a primitive integer type?  RSS feed

 
Mark Richardson
Ranch Hand
Posts: 80
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two classes. One class is a launcher class which has the main() method in it. The other class has the operation which will compute the area of a triangle based on two passed numbers. In other words, my program takes input of 2 numbers (base and height of a triangle)
and calculates the area of the triangle. However, I've hit a wall where I have a compilation error on this line in my code:



The error states:

The method run(double, double) in the type Triangle is not applicable for the arguments (Scanner, Scanner)

My Main Launcher Class:




and my Triangle class:


 
Campbell Ritchie
Sheriff
Posts: 54495
150
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by only writing one Scanner object. You hardly ever need two Scanners pointing to System.in, usually only one, but you mustn't close it. You are simply passing the wrong things to that method in line 20. Pass the right things and it will work.
Most of that code shou‍ld be moved out of the main method into different methods and the two values would be better as local variables. It would also free you from the temptation to make them static.

By the way: If you come back to that code in three months, you will find it difficult to understand because you haven't given the variables names which make it obvious what they mean. Similarly run doesn't run; it prints the area. The same applies to method names.
 
Mark Richardson
Ranch Hand
Posts: 80
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Start by only writing one Scanner object. You hardly ever need two Scanners pointing to System.in, usually only one, but you mustn't close it. You are simply passing the wrong things to that method in line 20. Pass the right things and it will work.
Most of that code shou‍ld be moved out of the main method into different methods and the two values would be better as local variables. It would also free you from the temptation to make them static.

By the way: If you come back to that code in three months, you will find it difficult to understand because you haven't given the variables names which make it obvious what they mean. Similarly run doesn't run; it prints the area. The same applies to method names.


Ah - Thank you!!
 
Mark Richardson
Ranch Hand
Posts: 80
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Start by only writing one Scanner object. You hardly ever need two Scanners pointing to System.in, usually only one, but you mustn't close it. You are simply passing the wrong things to that method in line 20. Pass the right things and it will work.
Most of that code shou‍ld be moved out of the main method into different methods and the two values would be better as local variables. It would also free you from the temptation to make them static.

By the way: If you come back to that code in three months, you will find it difficult to understand because you haven't given the variables names which make it obvious what they mean. Similarly run doesn't run; it prints the area. The same applies to method names.



So, now I'm creating 3 classes:

Input.java <--- has the scanner object and takes the input
Triangle.java <--- contains the findTriangleArea method
Launcher.java <---- contains my main method.


Isn't this overkill just to have a small program which finds the area of a triangle? or by doing this will I "scale-up" my knowledge and follow better practices?
 
Zachary Griggs
Ranch Hand
Posts: 80
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Three classes is perfectly fine, even for a smaller program (and especially if one is a launcher).
 
Campbell Ritchie
Sheriff
Posts: 54495
150
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Richardson wrote:. . . Input.java <--- has the scanner object and takes the input . . .
That sounds a good idea. Please search through my posts about utility classes; that may take you some time, but you will end up with an Input utility class which you can keep for ever, and enhance whenever you need to. Beware: I made a serious mistake in one post.
Isn't this overkill just to have a small program . . .?
No. I would go farther than ZG and say that you shou‍ld probably look on three as a minimum even for a small project. I think you will find it easy to write too few classes and difficult to write too many.
. . . will I "scale-up" my knowledge and follow better practices?
Yes
 
Liutauras Vilda
Marshal
Posts: 4117
227
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Richardson wrote:Isn't this overkill just to have a small program which finds the area of a triangle? or by doing this will I "scale-up" my knowledge and follow better practices?

Even if you building a kennel, always try to use skyscraper's building techniques, especially when you learning. After all, you don't know exactly how big your projects will grow. Building that way, your code will be more flexible and easier to extend. Requests for the extensions/changes of the apps will be requested (in your career) way more often than you think.

When building software, if your requirement is to take integer numbers, always think, how you'll be able and how easy it is going to be to adapt your code for double's when the user asks for that functionality (or maybe worth to build-in right away?).

Equally important is to pay attention to code formatting. Well indented and formatted code makes your code reader think, that professionals been at work.

Always make instance variables private.
Variable names supposed to follow camel casing... baseinput -> baseInput; heightinput -> heightInput -> height (that is probably preferred), or -> heightInCentimeters ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!