Results 1 to 12 of 12
- 01-14-2011, 05:05 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
Problem getting numbers from user and finding smallest two numbers
Here is the code written for taking input from command line and displaying the first smallest and 2nd smallest number of the given array.I am not able to understand y i am not getting the first smallest and 2nd smallest number of the given array.Can any one tell me.
The code :
Java Code:int i; int arr[] = new int[10]; Scanner scan = new Scanner(System.in); System.out.println("Enter numbers for the array: "); String s = scan.nextLine(); System.out.print("The numbers entered are :"); System.out.println(s); int tempFirstSmall = arr[0]; int tempSecSmall = arr[0]; for (i = 1; i < arr.length; i++) { if (arr[i] < tempFirstSmall) { tempSecSmall = tempFirstSmall; tempFirstSmall = arr[i]; } else if (arr[i] < tempSecSmall) { tempSecSmall = arr[i]; } } System.out.println("The 1st smallest number of the given array is:" + tempFirstSmall); System.out.println("The 2nd smallest number of the given array is:" + tempSecSmall);Last edited by Fubarable; 01-14-2011 at 05:05 PM. Reason: Moderator edit: code tags added
-
Moderator edit: code tags added. Please see the first link in my signature links below to see how to do this.
- 01-14-2011, 05:17 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
I did not understand .Can u explain me in length
- 01-14-2011, 05:38 PM #4
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
Hi radhi - what Fubarable means is, whenever you ask here for help and you copy and paste your code, always use the code tags around your code. There is a button "#" you should be able to see when you post your question. Simply, highlight your code, and press that button. It makes it easier for everyoen to read your code that way.
Secondly, have a look at the below code. All i did was do a sort on your array using Arrays.sort() method, it makes what you're trying to accomplish very simple.
Java Code:int arr[] = new int[10]; Scanner scan = new Scanner(System.in); for(int i = 0; i < arr.length; i++) { System.out.print("Enter number " + (i+1) + ": "); arr[i] = scan.nextInt(); } System.out.print("The numbers entered are : "); for(int x: arr) { System.out.print(x + " "); } System.out.println(); Arrays.sort(arr); System.out.println("The 1st smallest number of the given array is:" + arr[0]); System.out.println("The 2nd smallest number of the given array is:" + arr[1]);--user0--
- 01-14-2011, 05:43 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
Thank you very much.I will make sure that i follow it when i post some thing next time.
I am not suppose to use array.sort .so can u let me know where is it that i am doing wrong.
-
what's the purpose of this block?:
Java Code:else if (arr[i] < tempSecSmall) { tempSecSmall = arr[i]; }
-
Also and more importantly, where do you fill the array with numbers?
- 01-14-2011, 06:03 PM #8
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
if you are not allowed to use sorting, then I ask you just as Fubarable did, why have the else if statement? I think if you get rid of it, you should be good.
--user0--
- 01-14-2011, 06:15 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
Scanner scan = new Scanner(System.in);
System.out.println("Enter numbers for the array: ");
String s = scan.nextLine();
System.out.print("The numbers entered are :");
System.out.println(s);
So here s will have the numbers which i entered through command line
and after removing the else if code,my code doesn't work.
i am not able to understand the logic .Please help.
-
Again, when posting code, please use code tags.
s will not have any numbers, but rather s will hold a String, nothing more, nothing less and your int array will be empty unless you fill it. You have to iterate through this String (you could create another Scanner object with s -- new Scanner(s) and iterate through it using nextInt() to get your ints) and fill your array.
edit: I also changed your thread title from "help needed" to one that makes more sense, that summarizes your main problem. Please use more informative titles in the future.Last edited by Fubarable; 01-14-2011 at 06:26 PM.
- 01-14-2011, 06:35 PM #11
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
Thank you.I solved the problem.
- 01-14-2011, 06:36 PM #12
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
Similar Threads
-
Sum positive numbers using 10 inputs from user
By pvictory1 in forum New To JavaReplies: 15Last Post: 10-10-2010, 01:30 AM -
Reading strings from the user but dont want to accept numbers ,!
By javanew in forum New To JavaReplies: 1Last Post: 09-24-2010, 07:08 PM -
finding the largest k numbers in an array without sorting the whole list?
By jmmjm in forum Advanced JavaReplies: 5Last Post: 02-07-2009, 07:48 AM -
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM -
Finding largest and smallest integer
By mlhazan in forum New To JavaReplies: 2Last Post: 01-12-2008, 10:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks