This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple array object

 
alaina peeler
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you decrement it ? number--
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
alaina peeler
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i want to prompt them to input how ever many values are left...
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
should i use a while loop to calculate how many numbers are left? or would it be another for loop?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic