This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Ask user for 3 integers and find out if they make a triangle or not.

 
Marshal
Posts: 65760
250
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think a few more hints are in order. By the way: once you have got these methods working for ints, you will find it easy to create methods for entry of doubles, BigIntegers, BigDecimals, etc. They will look almost the same as getInt.The documentation comment is an essential part of the method because you will be using that class again and again. There is a part of the description missing in line 5. There is a corner case where min == max which I have not specifically addressed. I shall leave it to you whether you permit that corner case or not.
 
Bartender
Posts: 1245
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I've used documentation comment in my project. I can make method for double( rather than float), BigIntegers, BigDecimals whichever I want. Thank you for help, you solved my big issue which was pending since ages.  
Last question suppose I want store age of a person and age value definitely be within 0 to 100(Just assume). As we know range of byte is -128 to 127 which fits in our requirement i.e. 0 to 100 then Is It good to declare age as byte rather than int? where int takes more size in memory compared to byte?
 
Campbell Ritchie
Marshal
Posts: 65760
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you try to do anything with it, your byte will be promoted to an int anyway, so why worry about 24 bits of memory?
 
Ganesh Patekar
Bartender
Posts: 1245
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
 
Bartender
Posts: 2220
47
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not to mention that it would be burdensome to use.
Consider this example:
 
Ganesh Patekar
Bartender
Posts: 1245
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:

hahaha yes I realised that by default It's int,using byte for that means putting myself in trouble. Sometimes I think about weird things
 
Campbell Ritchie
Marshal
Posts: 65760
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing weird about it; you s‍hould find out about that sort of thing here where it doesn't matter if you are corrected.
 
Ganesh Patekar
Bartender
Posts: 1245
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
 
Ranch Hand
Posts: 62
2
Mac Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing I should be throwing an Exception here instead of just System.out.println()?   ANything else I could make better?  Would it be worth putting in a try, catch?  

 
Sheriff
Posts: 6259
167
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An exception should be reserved for things that are truly exceptional.  Here, it is expected that the user might enter an ill-formed number.  Your current code is the correct approach.
 
Ganesh Patekar
Bartender
Posts: 1245
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can remove int counter2 and print number information like Where are you using  int intHolder?
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Mac Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fixed. Thanks

IntHolder was a variable in my last faild attempt which I forgot to delete
 
Ganesh Patekar
Bartender
Posts: 1245
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Mac Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys.  I've been trying to add an input Utility class for user input.  What i do is call the method in the utility class 3 times.  If I wanted to do this in a while loop, would I need to use an array instead of variable - sideA, sideB and sideC?   When user enters invalid input, it just goes to the next variable instead of staying on the same one.  

// Method inside Triangle class which calls triangleInput method inside InputUtility class


// This is the whole Triangle class


//This is the whole importUtility class



 
Knute Snortum
Sheriff
Posts: 6259
167
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code that needs a loop when you don't get good input:

Also, you should declare intSides inside this method.
 
Ganesh Patekar
Bartender
Posts: 1245
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Knute mentioned, you can use while loop in triangleInput() method.
What is our condition? Ask user to input value until user enters a valid input value. So loop block will have code for asking input from user. But how long to execute loop ? what is the condition of valid input value?. The condition for valid input is input should be an int (If you want floating-point numbers then use double) value so execute while loop until hasNextInt returns true means execute while loop until user enters int value. Just convert these textual statements into code.
 
Rancher
Posts: 1041
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:



Not "or" but "and" operators are needed here.
 
Bartender
Posts: 708
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right. Typed too fast.
 
Ivan Jozsef Balazs
Rancher
Posts: 1041
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a,b,c are positive then at most one of these guys can be non-positive:
-a+b+c
+a-b+c
+a+b-c

Namely the greatest among them against the sum of the others. Because if we take not the greatest, it can not balance against the one proving its being not the greatest, let alone the sum of the two others. Another reasoning: the sum of two of the guys there is 2a, 2b and 2c respectively. So if two among them were non-positive, so would be their sum, the double of a given quantity.

So all three numbers above are positive if and only if their product is positive.

This equivalence might be mathematically correct but computationally not forcibly the ideal criterium.
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Mac Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought my while loop would run at least once as sc.hasNextInt() should return false as nothing is passed to it?  Would a do while loop be better?  I tried that also and it didn't work.



Ivan Jozsef Balazs - does that mean I have to stop the user from entering any negative numbers?  
 
Knute Snortum
Sheriff
Posts: 6259
167
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bod MacNeil wrote:I thought my while loop would run at least once as sc.hasNextInt() should return false as nothing is passed to it?


No, it will block.  That is, it will wait forever for input, since it can't determine the correct value to return without input.

Would a do while loop be better?  I tried that also and it didn't work.


This is a situation that is tailor made for a do loop.  

So what did you try and how specifically didn't it work?

does that mean I have to stop the user from entering any negative numbers?  


I would think so.  I don't think a negative length is legal.
 
Campbell Ritchie
Marshal
Posts: 65760
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bod MacNeil wrote:. . .

That form of loop looks incorrect to me, and I think it will not work.



Ivan Jozsef Balazs - does that mean I have to stop the user from entering any negative numbers?  

If you follow my suggestion of creating a utility class, you can overload the netxInt method to return only values in a particular range. I showed you part of that method yesterday well over a week ago.
 
This is my favorite show. And this is my favorite tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!