Results 1 to 13 of 13

Thread: Arrays

  1. #1
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default Arrays

    I'm having a problem with my code so far

    Java Code:
    public class arraysReversed
    {
        public static void main(String[] args)
        {
            // input from user
            Scanner input = new Scanner(System.in);
            
            // Declaring our variables and setting up the array
            double[] values;//Intializing the Array
            values = new double[10];//Assigning 10 integers for the array to hold
            
            //Ask the user to enter 10 integers
            System.out.println("Please enter ten numbers.");
            values = input.nextDouble();
        }
    }
    It is saying imcompatible types-found double but expected double[]

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,956
    Rep Power
    8

    Default

    Your values variable is an array of doubles. input.nextDouble() returns a single double. That's like saying:
    [this group of something] = [a single member of the group]

    Which obviously doesn't make sense.

    You want to assign a specific index of the array. Something like this:

    Java Code:
    values[0] = input.nextDouble();

  3. #3
    rohitjava is offline Member
    Join Date
    Aug 2010
    Posts
    13
    Rep Power
    0

    Default

    The write way to read array from user is :
    Java Code:
    public static void main(String []arr){
        // input from user
            Scanner input = new Scanner(System.in);
    
            // Declaring our variables and setting up the array
            double[] values;//Intializing the Array
            values = new double[10];//Assigning 10 integers for the array to hold
    
            //Ask the user to enter 10 integers
            System.out.println("Please enter ten numbers.");
            //values = input.next
            for(int i=0;i<values.length;i++){
                values[i]=input.nextDouble();
            }
    
    }
    thanks
    -rohitjava

  4. #4
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    This is what I have so far:

    Java Code:
    public class arraysReversed
    {
        public static void main(String[] args)
        {
            // input from user
            Scanner input = new Scanner(System.in);
            
            // Declaring our variables and setting up the array
            double[] values;//Intializing the Array
            values = new double[10];//Assigning 10 integers for the array to hold
            
            //Ask the user to enter 10 integers
            System.out.println("Please enter ten numbers.");
            for (int i = 0; i<values.length; i++) //for loop to display the values entered
            {
                values[i] = input.nextDouble();
                System.out.println("Value " + i + " is " + values[i]);
            }
            
        
        }
    }
    It is also asking me to display them in reverse order in which they were read. I am not sure how I would set this formula up.

  5. #5
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by djl1990 View Post
    Hi,

    just to confirm do you want to print the values in reverse order of what they were entered in. for example 1, 2, 3 into 3, 2, 1

    or do you want to print each of the arrays in reverse format, for example 21 into 12, 245 into 542 etc.

    It's for the reversed order. Ex. 1, 2, 3 into 3, 2, 1. Thanks

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default

    Quote Originally Posted by Latanyar View Post
    It is also asking me to display them in reverse order in which they were read. I am not sure how I would set this formula up.
    How do you think you should go about printing that array out backwards?

    How would you print it out forwards?

  7. #7
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    When I add another for loop it's still printing the values out in correct order.

    Java Code:
    public class arraysReversed
    {
        public static void main(String[] args)
        {
            // input from user
            Scanner input = new Scanner(System.in);
            
            // Declaring our variables and setting up the array
            double[] values;//Intializing the Array
            values = new double[10];//Assigning 10 integers for the array to hold
            
            //Ask the user to enter 10 integers
            System.out.println("Please enter ten numbers.");
            for (int i = 0; i<values.length; i++) //for loop to display the values entered
            {
                values[i] = input.nextDouble();
               System.out.println("Value " + i + " is " + values[i]);
            }
            
            for (int i = 0; i>values.length; i--) //for loop to display integers in reversed order
            {
               values[i] = input.nextDouble();
                System.out.println("Value " + i + " is " + values[i]);
            }
            
        
        }
    }

  8. #8
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    How do you think you should go about printing that array out backwards?

    How would you print it out forwards?

    On my first for loop it shows how I would print it out forwards. On my second for loop, I thought it would print out backwards, but apparently that is wrong.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default

    To print it out backwards what do you think "i" should be?
    That is, which index in the array should you initially point at?

  10. #10
    coyne20 is offline Member
    Join Date
    Feb 2009
    Posts
    20
    Rep Power
    0

    Default

    This works!

    import java.util.ArrayList;
    import java.util.List;

    public class ArraySwap
    {

    public static void main(String args[])
    {
    int [] array = {0,1,2,3,4};
    List<Integer> intlist = new ArrayList<Integer>();

    for(int i = array.length - 1; i >= 0; i--)
    {
    intlist.add(array[i]); // adds the value of an index into the list
    array[i] = 0; // clears the original array with a 0.
    }

    Object[] temp = intlist.toArray(); // the list is converted into an object array

    for(int i = 0; i <= temp.length - 1; i++)
    {
    array[i] = (Integer) temp[i]; //each index in the list is re-casted
    //back to the array.
    }

    for(int i = 0; i <= array.length - 1; i++)
    {
    System.out.println(array[i]); //each index
    }
    }

    }

    /*
    * This piece of code simply takes an array of integers and reverses it with
    * the use of a list.
    */

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default

    Nice code dump...or not.
    It's not helpful.

    And it's one of the more convoluted ways of simply printintg out an array backwards.

  12. #12
    coyne20 is offline Member
    Join Date
    Feb 2009
    Posts
    20
    Rep Power
    0

    Default

    Ok I realise that my code formatting isnt the best. But the code does meet the objective of re-arranging the values in the array in reversed order.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

Similar Threads

  1. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  2. Help with 2D Arrays.
    By Meta in forum New To Java
    Replies: 1
    Last Post: 03-30-2010, 02:27 AM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  4. Arrays
    By karabo101 in forum New To Java
    Replies: 12
    Last Post: 10-11-2009, 05:02 PM
  5. 2D Arrays
    By Major90 in forum New To Java
    Replies: 6
    Last Post: 11-06-2008, 02:08 PM

Posting Permissions

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