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 8 of 8

Thread: Help With Counter

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help With Counter

    I'm trying to make this program check what number is the highest and write it out in that order, not working for me. How would one solve this?

    public class Upg11 {
    	public static void main(String[] args) {
    		int[] tal = new int[5];
    		System.out.print("Mata in fem heltal: ");
    		tal[0] = Keyboard.readInt();
    		tal[1] = Keyboard.readInt();
    		tal[2] = Keyboard.readInt();
    		tal[3] = Keyboard.readInt();
    		tal[4] = Keyboard.readInt();
     
    		for (int n = 0; n < 5; n++) {
    			int i = 1;
    			if (tal[n] < tal[i]) {
    				int temp = tal[n];
    				tal[n] = tal[i];
    				tal[i] = temp;
    			}
     
    		}
     
    		System.out.println("Talen är " + tal[0] + 
    				" " + tal[1] + " " + tal[2] + 
    				" " + tal[3] + " " + tal[4]);
    	}
    }


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Thumbs up Re: Help With Counter

    you need to go through certain sorting algos:

    Click here
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,808
    My Mood
    Hungover
    Thanks
    147
    Thanked 696 Times in 594 Posts

    Default Re: Help With Counter

    Quote Originally Posted by Catgroove View Post
    I'm trying to make this program check what number is the highest and write it out in that order, not working for me. How would one solve this?
    Sorry, but saying "not working for me" is as useful a question as us saying "then make it work" is a useful answer. What's happening? What did you expect to happen? Are you seeing any Exceptions? Strange behavior? Something else?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Counter

    Quote Originally Posted by KevinWorkman View Post
    Sorry, but saying "not working for me" is as useful a question as us saying "then make it work" is a useful answer. What's happening? What did you expect to happen? Are you seeing any Exceptions? Strange behavior? Something else?
    I'm trying to make it display the numbers again but the in a size order. Highest first. Currently just shows them without changing positions.

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 288 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help With Counter

    Look into the Arrays.sort method
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  6. #6
    Junior Member
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Counter

    you forget to increament i.i should be increament by 1.

  7. #7
    Junior Member
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Counter

    I tried it before and doesn't work.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,808
    My Mood
    Hungover
    Thanks
    147
    Thanked 696 Times in 594 Posts

    Default Re: Help With Counter

    Quote Originally Posted by Catgroove View Post
    I tried it before and doesn't work.
    Then make it work. :p
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Dice Counter
    By Xxl0n3w01fxX in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 26th, 2011, 10:49 AM
  2. Odd Even Zero Counter?
    By velop in forum Java Theory & Questions
    Replies: 1
    Last Post: February 19th, 2010, 01:13 PM
  3. String counter
    By chkang0130 in forum Algorithms & Recursion
    Replies: 9
    Last Post: December 8th, 2009, 10:56 AM