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

    Default Can't figure out what's wrong, help please.

    I'm trying to write a program that keeps getting numbers from the user, until the user enters a 0.
    The program then prints the smallest two numbers that were entered, and the largest number that was entered.
    Example:
    User enters the numbers 15, 33, 26, 12, 19, -5, 98, 12, 2, 0
    The output will be
    "Largest number:" 98
    "Smallest number:" -5
    "Second smallest number:" 2

    This is my code:
    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, Small, Small2;
    		Scanner in=new Scanner(System.in);
    		num=in.nextInt();
    		Larg=num; Small=num; Small2=num;
    	
    		
    	while (num != 0){
    		output1();
    		num=in.nextInt();
    		if(num > Larg){
    				Larg=num;}
    		if(num > Small && num != 0){
    				Small2=num;}
    		if(num < Small2 && num != 0){
    				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: ");
    	}
    }
    My first problem is my largest and my smallest seem to output correct all the time but my second smallest doesn't. My second problem is if i just enter 0 at the start of the program instead of the program exiting it outputs "Largest: 0, Smallest: 0, Second Smallest: 0" then exits. Is there an easy way of fixing this without going threw to much trouble it's 2:20am and i have to hand this in by 6am and i'm on no sleep. Lol.
    Last edited by Taszk; 05-24-2011 at 08:51 AM.

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

    Default

    Please wrap your code in code tags
    [code]
    YOUR CODE HERE
    [/code]


    edit: Are you allowed to store the input in an array, and then loop through the array(or ArrayList)? I know you can do it as you go, but it may be easier to "remember" the entered values.

    Java Code:
    while (num != 0){
    output1();
    num=in.nextInt();
    if(num > Larg){
    Larg=num;}
    if(num > Small && num != 0){
    Small2=num;}
    if(num < Small2 && num != 0){
    Small=num;}
    
    }
    There can also be a bug here. What if the first input is 10, all three values will be 10, the next input will be a 2, so small will become 2, then if the user enters 5, small 2 becomes 5, then the user enters 1, small becomes 1, finally the user enters 0, will the 2 still exist somewhere?
    Last edited by sunde887; 05-24-2011 at 08:52 AM.

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

    Default

    Sorry about that i didn't know. There's no limit to what i could use. Could you help me out i'm new to java and ain't to familiar with much. Thanks.

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

    Default

    Java Code:
    if(num > Small && num != 0){
    Small2=num;}
    if(num < Small2 && num != 0){
    Small=num;}
    I don't think this is doing what you want it to do.

    If num is 5, small is 2 you then set small2 to 5?
    Then num is 7, small is still 2 and now small2 is 7?

    I think you meant to put
    Java Code:
     if(num < Small2 && num > Small && num != 0){//Set variable small2}
    if(num < Small && num !=0){//Set variable small}
    That would set small2 to your second smallest and small to your smallest. I'm not sure if you will have to change how you originally set your variables, but try this.
    • 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.

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

    Default

    No the 2 wouldn't exist anymore. Is there a way to keep the numbers the user enters?

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

    Default

    @Sunde, I don't think he is in Array's yet. I remember doing an exercise like this when I first started learning in chapter 2 or 3 and arrays came in chapter 5 or 6.
    • 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. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    You could set them to an array or string, you don't need to. The logic portion I said should solve your problem with incorrect values being stored.

    However, when you set small to a smaller number, you should set small2 to small before setting small to num.
    • 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.

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

    Default

    i changed the portion you mentioned above

    Java Code:
    	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){
    				Small=num;}
    when i used numbers 9,10,8,6,7,5,3,4,2,1 in that order i ended up with Largest 10, Smallest 1 and Second Smallest 4

  9. #9
    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 Dark View Post
    You could set them to an array or string, you don't need to. The logic portion I said should solve your problem with incorrect values being stored.

    However, when you set small to a smaller number, you should set small2 to small before setting small to num.
    See above bold text.
    • 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.

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

    Default

    Like this:
    Java Code:
    		if(num < Small2 && num > Small && num != 0){
    				Small2=Small;}
    		if(num < Small && num !=0){
    				Small=num;}
    If so i ended up with 3 as second smallest? Or i probably didn't do what you asked to correctly

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

    Default

    No like this:
    Java Code:
    if(num < Small && num !=0)
    {
         Small2 = Small;
         Small=num;
    }
    Just change the last line if your if statements.
    • 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.

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

    Default

    Sorry for bothering you so much i really appreciate your help this is what it looks like now
    Java Code:
    		if(num > Larg){
    				Larg=num;}
    		if(num < Small2 && num > Small && num != 0){
    				Small2=num;}
    		if(num < Small && num !=0){
    				Small2 = Small;
    				Small=num;}
    i put 1,2,3,4,5,6,7,8,9,10 and ended up with largest 10 smallest 1 and second smallest 1

    could it have something to do with the beginning of my program where i set larg, small, small2 all to num?
    Last edited by Taszk; 05-24-2011 at 09:43 AM.

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

    Default

    Yes, if you enter the lowest number first small and small2 will both be equal to the lowest number.
    • 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.

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

    Default

    How would i go about fixing this? and why is it that if the first number i enter is 0 the program doesn't just exit? it displays Largest: 0, Smallest: 0, Second Smallest: 0 then exits.

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

    Default

    Your print statement isn't inside the while loop, when you enter 0 the only thing that is happening is you are exiting the while loop.

    Since you set Larg, Small, and Small2 to 0 before entering the while loop, and print the results after the while loop no matter what you will also print that. You could put the print statement inside an if statement. ie
    Java Code:
     if(!(num == 0 && Larg == 0)){System.out.println("My print statement");}
    As for the problem with small and small2 being the same if the lowest number is entered first, give me a second to think it through.
    • 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.

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

    Default

    EDIT: There was a problem with my suggestion. Rethinking it.
    Last edited by Dark; 05-24-2011 at 10: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.

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

    Default

    i tried putting the print statement inside the if statement but because of the loop it repeats it every time it goes around. No problem i don't think i'm getting any sleep today i'll be heading straight to school.

    Thanks again for your time.

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

    Cool

    Taszk,

    Are you aware of ArrayLists? Are you allowed to use them? They would make this thing pretty simple in fact.

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

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

    Default

    Only thing i know about Arrays as of now is that they're similar to Strings we're allowed to use them but i have no idea how to use them. I haven't gotten that far in my book.

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

    Default

    Actually my edited suggestion would work, as long as you had at least 2 numbers.

    Initialize the numbers to a really high and low number. Its clunky, but it will work. Don't use it to solve your problems outside of this program, there are better ways to get around this problem. It would be better to write them all to an ArrayList and then read from there.

    Java Code:
    int Larg = -2147483648;
    int Small, Small2 = 214748647;
    If you only enter 1 number, it won't work. Those numbers are as high and low as an int can handle, so any number in between will cause it to change.
    • 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.

Page 1 of 2 12 LastLast

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
  •