• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

simple array object

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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] + " ");

}

}
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you decrement it ? number--
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic