Results 1 to 13 of 13
Thread: Arrays
- 10-18-2010, 02:55 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 25
- Rep Power
- 0
Arrays
I'm having a problem with my code so far
It is saying imcompatible types-found double but expected double[]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(); } }
- 10-18-2010, 03:25 PM #2
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();
- 10-18-2010, 03:30 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 13
- Rep Power
- 0
The write way to read array from user is :
thanksJava 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(); } }
-rohitjava
- 10-18-2010, 04:14 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 25
- Rep Power
- 0
This is what I have so far:
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.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]); } } }
- 10-18-2010, 04:59 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 25
- Rep Power
- 0
- 10-18-2010, 05:20 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
- 10-18-2010, 06:14 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 25
- Rep Power
- 0
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]); } } }
- 10-19-2010, 12:54 AM #8
Member
- Join Date
- Oct 2010
- Posts
- 25
- Rep Power
- 0
- 10-19-2010, 08:53 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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-19-2010, 04:17 PM #10
Member
- Join Date
- Feb 2009
- Posts
- 18
- Rep Power
- 0
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.
*/
- 10-19-2010, 04:28 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 10-20-2010, 10:26 AM #12
Member
- Join Date
- Feb 2009
- Posts
- 18
- Rep Power
- 0
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.
- 10-20-2010, 10:34 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Help with 2D Arrays.
By Meta in forum New To JavaReplies: 1Last Post: 03-30-2010, 02:27 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Arrays
By karabo101 in forum New To JavaReplies: 12Last Post: 10-11-2009, 05:02 PM -
2D Arrays
By Major90 in forum New To JavaReplies: 6Last Post: 11-06-2008, 02:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks