Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can't figure out what's wrong

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Thanked 0 Times in 0 Posts

    Default Can't figure out what's wrong

    The user should input non negative integers, These values must be filled in an array of 10 elements. If the user enters -1, or the array becomes full whatever comes first, the program should terminate. The output should be the value of integers entered by the user in the reverse order.

    My Problem:
    When I enter -1, the -1 also gets displayed in the array.It should display non negative integers, the -1 is a condition to break out of the loop.
    Also if I enter -1, the elements in the array that are not filled display 0. I don't want 0 to be displayed if the value of elements are null.

    import java.util.Scanner;
    public class whatswrong {
    	public static void main(String[] args) {
                            int array[];
    			int i=0, userInput;
    			array = new int[10];
    			Scanner scan = new Scanner (System.in);
    			while (i <= 10){
    			System.out.println("Please enter a positive integer or -1 to quit");
    			userInput = scan.nextInt();
    			array[i] = userInput;
    			if (i == 10)
    			if (userInput < -1)
    			if (userInput == -1)
    			int[] reverse = new int[10];
    			int revIndex = 0;
    			for (int j = array.length -1 ; j >= 0; j--){
    				reverse[revIndex] = array[j];
    			System.out.println("reverse: ");
    			for (int k = 0; k < array.length; k++)

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 636 Times in 540 Posts

    Default Re: Can't figure out what's wrong

    You're trying to do a few things at once here. Break your problem down into smaller steps. Specifically, separate the code that's getting user input from the code that's reversing the array. Which one isn't working? Then step through it with a debugger, or at least at some print statements, to figure out exactly when the code does something different from what you expect.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. My pseudocode is apparently wrong, and I can't figure out why...
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 12th, 2012, 12:17 AM
  2. i cannot figure out what's wrong
    By biyuss in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 17th, 2011, 09:22 PM
  3. can't figure whats wrong with my add method?? help!
    By b094mph in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 8th, 2011, 05:00 PM
  4. I'm new at this and can't figure it out
    By jaheh06 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 22nd, 2010, 08:44 AM
  5. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 08:49 AM