Page 2 of 2 FirstFirst 12
Results 21 to 31 of 31
  1. #21
    Taszk is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    i see where your coming from but for some odd reason enter 1,2,3,4,5,6,7,8,9,10 and 0 to exit gives me 2 as smallest and 3 as second smallest. Shouldn't it be 1 and 2?

    and now when i just enter 0 to exit i get those numbers i initialized at the beginning as output. i tried putting the print statement inside the if but since there's a loop it repeats it when it goes around.

  2. #22
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Don't put the print statement in the while loop.

    You set your variables to the numbers I said? Post your updated code so I can see it.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #23
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I kind of figured he probably wasn't very familiar with arrays/array lists. Just suggested it because the logic can be much simpler.

  4. #24
    Taszk is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Java Code:
    import java.util.*;
    public class Project2Beta{
    	public static void main(String[] args){
    		//main method calls output method and executes it
    		output();
    
    		
    		//Variables must be integers
    		int num, Larg=-2147483648, Small=2147483647, Small2=2147483647;
    		Scanner in=new Scanner(System.in);
    		num=in.nextInt();
    	
    		
    	while (num != 0){
    		output1();
    		num=in.nextInt();
    		if(num > Larg){
    				Larg=num;}
    		if(num < Small2 && num > Small && num != 0){
    				Small2=num;}
    		if(num < Small && num !=0){
    				Small2 = Small;
    				Small=num;
    				}
    							
    		
    		}	
    		System.out.print("Largest: "+Larg+" \nSmallest: "+Small+" \nSecond Smallest: "+Small2);
    	}
    	
    	//Method called output 	
    	public static void output(){
    		System.out.print("Enter a number to begin executing Largest, Smallest and ");
    		System.out.println("Second Smallest number program, Otherwise terminate program by entering 0");
    	}
    	
    	//Method called output1
    	public static void output1(){
    		System.out.print("Enter another number: ");
    	}
    }
    I didn't put the print statement in it. I put the print statement inside the brackets of the ending of the last if statement.

  5. #25
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    The place where you are reading the input in while loop is causing the problem. Read the number at the end of the while loop, instead of in the beginning. Thats what is causing you to skip the first value.

    Java Code:
    while (num != 0){
    		output1();
    		[COLOR="red"]//num=in.nextInt(); Don't have this here[/COLOR]
    		if(num > Larg){
    				Larg=num;}
    		if(num < Small2 && num > Small && num != 0){
    				Small2=num;}
    		if(num < Small && num !=0){
    				Small2 = Small;
    				Small=num;
    				}
    		num=in.nextInt(); [COLOR="red"]// Have it here[/COLOR]
    		}
    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  6. #26
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Quote Originally Posted by Taszk View Post
    Java Code:
    import java.util.*;
    public class Project2Beta{
    	public static void main(String[] args){
    		//main method calls output method and executes it
    		output();
    
    		
    		//Variables must be integers
    		int num, Larg=-2147483648, Small=2147483647, Small2=2147483647;
    		Scanner in=new Scanner(System.in);
    		[b]num=in.nextInt();
    	
    		
    	while (num != 0){
    		output1();
    		num=in.nextInt();[/b]
    		if(num > Larg){
    				Larg=num;}
    		if(num < Small2 && num > Small && num != 0){
    				Small2=num;}
    		if(num < Small && num !=0){
    				Small2 = Small;
    				Small=num;
    				}
    							
    		
    		}	
    		System.out.print("Largest: "+Larg+" \nSmallest: "+Small+" \nSecond Smallest: "+Small2);
    	}
    	
    	//Method called output 	
    	public static void output(){
    		System.out.print("Enter a number to begin executing Largest, Smallest and ");
    		System.out.println("Second Smallest number program, Otherwise terminate program by entering 0");
    	}
    	
    	//Method called output1
    	public static void output1(){
    		System.out.print("Enter another number: ");
    	}
    }
    I didn't put the print statement in it. I put the print statement inside the brackets of the ending of the last if statement.
    Its ignoring the first number because you're overwriting it when it enters into the while loop.
    Just delete the first num = in.nextInt(); and it'll work just fine. Set num to 1.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  7. #27
    Taszk is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Thank you all for your help. Really appreciate it.

    Is there any place i could put the print statement so it still prints the output and if i enter zero at the beginning of the program it just exits without displaying anything?

  8. #28
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Write another if statement for it.
    Java Code:
    if(!(Larg == -2147483648 && Small == 214748647 && Small2 == 214748647)){System.out.println("My print statement.");}
    Last edited by Dark; 05-24-2011 at 11:16 AM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  9. #29
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Quote Originally Posted by Taszk View Post
    Is there any place i could put the print statement so it still prints the output and if i enter zero at the beginning of the program it just exits without displaying anything?
    Keep the current code as it is. But if you want to exit if the first input itself is a zero then you can use System.exit(). Something like,
    Java Code:
    .... num = in.nextInt();
    if (num == 0) {
            // optional print statement.
    	System.exit(0);
    }
    while (num != 0) {...... //rest of the code
    Use this just before your while loop, after you read the integer first time. You can also have any print statement inside the if statement for display purpose before exiting. :cool:

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  10. #30
    Taszk is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Thanks for everything. :)

  11. #31
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Quote Originally Posted by Taszk View Post
    Thanks for everything. :)
    Come back again if you face any issues. We would be more than happy to help you.

    I hope you can get at least "some" sleep before heading to school. :cool:

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 16
    Last Post: 08-09-2010, 11:40 PM
  2. Cant figure out where went wrong.
    By leviathan in forum New To Java
    Replies: 15
    Last Post: 06-06-2010, 06:55 PM
  3. Replies: 3
    Last Post: 01-11-2010, 06:48 AM
  4. [SOLVED] Can't figure out what's wrong
    By roach_van in forum New To Java
    Replies: 10
    Last Post: 09-05-2008, 07:09 AM
  5. Replies: 2
    Last Post: 07-11-2008, 09:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •