Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: problem with my code (FOR LOOPS) neeed help plZ

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default problem with my code (FOR LOOPS) neeed help plZ

    hello dear , good morning ^^
    i have a problem with my code !~
    coz i have a task to prepare a code for a program to find the two largest values of the 10 values entered.


    PHP Code:
        Scanner input = new ScannerSystem.in );
        
        
    int largest1  ;// largest number
        
    int number// user input
        
    int counter// number of values entered
        
    int largest2 ;// the second largest number 
        
    System.out.println"Enter the number 1:" );
         
    number input.nextInt();
         
         
    largest1=number;
         
    counter=2;
         while(
    counter<=10){
             
         
            
    System.out.println"Enter the number "+counter+":" );
            
             
    number input.nextInt();
              
              if((
    number>largest1))
                  
    largest2=largest1;
                 
    largest1=number;  
            
              
    counter++ ;     
    }
           
    System.out.println("largest1 "+largest1 );
           
    System.out.println(largest2 +"largest2");
         
         
         

            }

        } 
    what the wrong with my code

    for example ;;
    if i entered for example 10 numbers
    1 4 6 88 22 99 11 101 99 26
    and then the output will be
    largest1 = 101
    largest2 = 99

    help me , i'm beginner in java ^^


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: problem with my code (FOR LOOPS) neeed help plZ

    Quote Originally Posted by jessy sonita View Post
    if i entered for example 10 numbers
    1 4 6 88 22 99 11 101 99 26
    and then the output will be
    largest1 = 101
    largest2 = 99
    Isn't that exactly what it should output?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem with my code (FOR LOOPS) neeed help plZ

    yes , it should out put
    101
    99

    what's the problem with the code plz

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: problem with my code (FOR LOOPS) neeed help plZ

    Quote Originally Posted by jessy sonita View Post
    yes , it should out put
    101
    99

    what's the problem with the code plz
    You tell us. What does it do instead of what you expect it to do? Have you stepped through this with a debugger, or at least added some print statements? Recommended reading: http://www.javaprogrammingforums.com...t-println.html

    Hint: Curly brackets are your friend.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    jessy sonita (November 25th, 2011)

  6. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem with my code (FOR LOOPS) neeed help plZ

    if i wanna use the while loop as i put in the code ~!
    how will it be ?

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: problem with my code (FOR LOOPS) neeed help plZ

    Quote Originally Posted by jessy sonita View Post
    if i wanna use the while loop as i put in the code ~!
    how will it be ?
    I don't understand your question. You already do have a while loop in your code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem with my code (FOR LOOPS) neeed help plZ

    Big problem with an assignment.
    For some reason, when I input 0, 6, 2, 3, 4, 5, 6, 7, 8, and 9 into this program, it tells me my largest is 9 and second is 0.
    Any Java experts here that can tell me how to fix this to have 8 as the result of 2nd largest?

  9. #8
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem with my code (FOR LOOPS) neeed help plZ

    give any code that tells me the 2nd largest number is 8 not 0

  10. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: problem with my code (FOR LOOPS) neeed help plZ

    Quote Originally Posted by jessy sonita View Post
    give any code that tells me the 2nd largest number is 8 not 0
    That's not how this works. That's academic dishonesty and is extremely rude.

    I already gave you several hints that you have ignored. Respond to them, and we'll go from there. Use a debugger or follow the directions in the link I gave you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #10
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with my code (FOR LOOPS) neeed help plZ

    Keep the statements under if condition in braces. Like below

    if((number>largest1)){
    largest2=largest1;
    largest1=number;
    }

  12. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: problem with my code (FOR LOOPS) neeed help plZ

    Hemambara, please read this: http://www.javaprogrammingforums.com...n-feeding.html

    Consider this a friendly warning.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #12
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: problem with my code (FOR LOOPS) neeed help plZ

    The logic is wrong, not just the if statement.

    Ironically, correcting the if statement may give the correct result for the particular case, but the code will be wrong.

    A good idea is to also test with numbers sorted in the opposite direction, like:

    1, 9, 8, 7, 6, 5, 4, 3, 2, 0

    That will go wrong even if you fix the if

Similar Threads

  1. Problem with my jsp code .Need Help Please!!!
    By ur2cdanger in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: October 22nd, 2011, 01:10 AM
  2. I have a problem with my code I think
    By byrne in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 12th, 2011, 04:18 AM
  3. Problem in the loops
    By shubhen in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 22nd, 2011, 02:45 AM
  4. [SOLVED] I am a java newb and I am having a problem with my while loops.
    By Deaththekid in forum Loops & Control Statements
    Replies: 22
    Last Post: July 10th, 2011, 07:26 AM
  5. Help Neeed
    By arunjib in forum Java Theory & Questions
    Replies: 19
    Last Post: March 11th, 2011, 01:48 PM