Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

smallest positive integer  RSS feed

 
izham zamli
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hye there,
im playing around with some java code.
but googling around produce 0 input to me.

how can i exclude -1 in finding smallest positive integer?

 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why you want to exclude the specific value -1. Is that value mentioned in your requirements somewhere?
 
izham zamli
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I don't understand why you want to exclude the specific value -1. Is that value mentioned in your requirements somewhere?


no no no,
i mean exclude any inserted negative integer.
sorry for misinformation
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So basically you want to find the lowest natural number number that is entered.

But you have set min to 0, and this IS the lowest natural number.

Also, it is better than class names start with a CAPITAL letter, and variable names start with a lowercase letter.
So smallestValue should be SmallestValue, and int N should be int n.
Java won't complain if you don't follow this convention, but it is good to.

 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can exclude any negative number with a conditional statement e.g. if (x < 0) .....carry out commands.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Ahmed Bin S said, Name of a class should start with capital letter. Please refer this Java Programming Style Guide. Make separate method for finding minimum number. Don't write such code in main method. Here problem is you are taking input from user and immediately checking whether it is less than min ( where you set min = 0 which is already lowest positive number) or not, so obviously you will get min = 0 as output. Here in your code will never be true as min = 0 is lowest. If you want to take input from user & immediately check whether less than previous value stored in min variable then you have to write a condition to assign user's first entered positive value as min like this, declare boolean variable Below code in for loop, after N = input.nextInt();
 
izham zamli
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for helping me out guys.understand now
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're most welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!