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

Thread: Arrays

  1. #1
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Arrays

    PHP Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package largestn;
    import java.util.*;
    /**
     *
     * @author Xyn
     */
    public class Largestn {


        
    /**
         * @param args the command line arguments
         */
        
    public static void main(String[] args) {
         
    int larN1;
    int larN2;
    int length=10;

            
            
            
            
            
    System.out.println("Insert 10 numbers and you will recieve the top 2.");
        
        
            
    Scanner keyboard = new Scanner(System.in);
            
    int num1 keyboard.nextInt();
            
    int num2 keyboard.nextInt();
            
    int num3 keyboard.nextInt();
            
    int num4 keyboard.nextInt();
            
    int num5 keyboard.nextInt();
            
    int num6 keyboard.nextInt();
            
    int num7 keyboard.nextInt();
            
    int num8 keyboard.nextInt();
            
    int num9 keyboard.nextInt();
            
    int num10 keyboard.nextInt();

        
    int[] anArray = {num1num2num3num4num5num6num7num8num9num10};
     
    System.out.println(anArray);
       

        
    }} 
    I'm trying to store 10 ints, and im going to attempt to print the 2 highest ints ((No this is not an assignment, I saw someone else trying to complete it I figured id give it a try for practice.

    I understand im probably taking the long way. But when I get to the system print trying to print anArray I get [I@cf40f5 printed or some other weird letters n numbers... This is telling me that the numbers im inserting are not properly getting stored... Anyways, ill probably have more questions as I go in this thread.

    I don't want to be spoon fed, just suggestions on where to go from here. Thanks.

    **You can ignore length=10; its irrelevant lol.


    **** Nevermind, after further testing of printing individual values of the array they are getting stored properly, just I guess you cannot print the whole array at once..

    How can I simplify this? And how do I find the biggest 2 numbers in a array of 10 numbers? I think I could do it but it would be ALOT of if statements.
    Last edited by Emperor_Xyn; December 7th, 2011 at 07:12 PM.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Arrays

    How can I simplify this? And how do I find the biggest 2 numbers in a array of 10 numbers? I think I could do it but it would be ALOT of if statements.
    Then don't use if-statements. Use a for-loop that iterates as many times as the number of integers stored in the array, and name a variable "int biggest" or something like that. In the for-loop, tell it to check each value of the array compared to the currently stored maximum value, and redefine the maximum value if necessary.

    Note that also instead of defining ten numbers like you have (num1, num2, etc.) you could directly define your array and use its elements

    anArray[0] = keyboard.nextInt();
    anArray[1] = keyboard.nextInt();

    etc. Even better, you could use a for-loop instead of writing the same line of code ten times.
    Last edited by snowguy13; December 7th, 2011 at 08:09 PM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. The Following User Says Thank You to snowguy13 For This Useful Post:

    Emperor_Xyn (December 7th, 2011)

  4. #3
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: Arrays

    In the for-loop, tell it to check each value of the array compared to the currently stored maximum value
    How to do you code something to take the highest int??? I'm sure its not that easy thats why i'm puzzled.
    What forumula?



    Thanks though =) I no longer have so many repeating lines.

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Arrays

    Quote Originally Posted by Emperor_Xyn View Post
    But when I get to the system print trying to print anArray I get [I@cf40f5 printed or some other weird letters n numbers... .
    This means you are attempting to print the array variable and not the contents of the array. Is the JVM supposed to read your mind as to how ou want the values inside the array to be displayed? You need to iterate over the array and print out the values yourself. The added bonus is you can specify in what format they get printed: all on one line separated by commas, each on its own line etc.
    Improving the world one idiot at a time!

  6. #5
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: Arrays

    Quote Originally Posted by Junky View Post
    This means you are attempting to print the array variable and not the contents of the array. Is the JVM supposed to read your mind as to how ou want the values inside the array to be displayed? You need to iterate over the array and print out the values yourself. The added bonus is you can specify in what format they get printed: all on one line separated by commas, each on its own line etc.
    Ahh, I thought if you printed an array it would just print
    anArray[0] = Blah
    anArray[1] = blah

    My fault =p Need to format!

  7. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Arrays

    The Arrays class has a toString method (several of them) but it may not format the output how you like.
    Improving the world one idiot at a time!

  8. #7
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Arrays

    How to do you code something to take the highest int???
    I don't think the Array class has a maximum method, but you can check each value individually by using the Math.max() method. It accepts two numbers and returns the larger of the two. If you compare each value using the for-loop to the current maximum, by the time the for-loop is done, you'll have the highest number in the array.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  9. The Following User Says Thank You to snowguy13 For This Useful Post:

    Emperor_Xyn (December 9th, 2011)

Similar Threads

  1. Arrays
    By ruffu054 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 7th, 2011, 11:18 PM
  2. Arrays
    By av8 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 25th, 2011, 01:21 PM
  3. 2d arrays help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 27th, 2010, 05:27 PM
  4. Arrays
    By mlan in forum Java Theory & Questions
    Replies: 2
    Last Post: February 26th, 2010, 10:23 AM
  5. 2d Arrays
    By mgutierrez19 in forum Collections and Generics
    Replies: 5
    Last Post: October 27th, 2009, 04:08 PM