File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes simple array object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple array object" Watch "simple array object" New topic
Author

simple array object

alaina peeler
Greenhorn

Joined: Dec 04, 2005
Posts: 29
i am supposed to create a simple program that prompts the user to enter an integer and then asks the user to enter that many values stored in an array. Then they are printed out...then the order is reversed and printed again. It's working fine my only problem is when i prompt the user to "Enter x values:" the number x never changes and i cant figure that out....considering if the user enters 5 at the beginning they are supposed to be prompted to input 5 values...here is my code...

import java.util.Scanner;
public class AssignmentOne
{

/** Creates a new instance of AssignmentOne */
public static void main(String[] args)
{
int number;

Scanner scan = new Scanner(System.in);

System.out.println("Enter an Integer: ");
number=scan.nextInt();


int[] values = new int[number];

for(int count=0; count < values.length; count++)
{
System.out.println("Enter " + (number) + " values: ");
values[count]=scan.nextInt();

}
for(int value : values)
System.out.println(value + " ");


System.out.println("The numbers in reverse order:");
for (int count = values.length-1; count >=0; count--)
System.out.println(values[count] + " ");

}

}
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Why don't you decrement it ? number--


[My Blog]
All roads lead to JavaRanch
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
What do you want to tell the user? How many values are left that they should enter? Obviously the value of number never changes since it is the length of your array. The value of count DOES change, though. Can you use this to calculate how many values are left?

Or do you want to tell them which number they are entering? What varialbe keeps track of this?

Or do you want to print something else? In other words, you need to explain what you want to print here in order for us to help you further. The above are just a few guesses with some clues about how to solve them.

I hope this helps.

Layne


Java API Documentation
The Java Tutorial
alaina peeler
Greenhorn

Joined: Dec 04, 2005
Posts: 29
yes i want to prompt them to input how ever many values are left...
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by alaina peeler:
yes i want to prompt them to input how ever many values are left...


So can you figure out a way to calculate this value? You might want to use the number and count variables that you already have. How can you use these to calcuate how many numbers are left?

Layne
alaina peeler
Greenhorn

Joined: Dec 04, 2005
Posts: 29
should i use a while loop to calculate how many numbers are left? or would it be another for loop?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: simple array object