1

I want to make a program, which moves every entry by +1 for every new entry.

e.g

myArray[0]= Thomas

and after another entry it becomes:

myArray[0] = Fred

myArray[1] = Thomas

.

after 5 entries it becomes .

myArray[4]=Thomas;

import java.util.Scanner;
import java.util.Arrays;
public class UB5{
    static String input(String name){
        Scanner user_input = new Scanner(System.in);
            System.out.println("Enter your name: ");
            name = user_input.nextLine();
            return name;
        }
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String myArray[] = new String[5];
        myArray[0] = "";
        myArray[1] = null;
        myArray[2] = null;
        myArray[3] = null;
        myArray[4] = null;
        String s = "";
        String temp = myArray[0];
        myArray[0] = myArray[1];
        myArray[1] = temp;
        for(int i = 0; i < myArray.length; i++){
            myArray[i] = input(s);
            if(myArray[i] == null){               
                myArray[i] = myArray[i+1];
            }
        }

        System.out.println(Arrays.toString(myArray));
    }
}

The unwanted output that I get:

Enter your name:
Thomas
Enter your name:
Peter
Enter your name:
Fred
Enter your name:
Bruce
Enter your name:
Angelo
[Thomas, Peter, Fred, Bruce, Angelo]
xarz x
  • 15
  • 6
  • 4
    Use an ArrayList instead and look into `ArrayList#add(int index, E element)` – RobOhRob Nov 25 '19 at 17:20
  • 1
    You have multiple problems in your code. You're incrementing your index both in the loop header and in the loop body, and you're comparing strings with '!='. – Bill the Lizard Nov 25 '19 at 17:23

2 Answers2

5

I wouldn't use an array for this, I'd use one of the List implementations (perhaps ArrayList) and insert at the beginning of it via add(index,element). (I guess you'd need to check size() and do a remove(index) to remove the one falling off the end, otherwise your list would just continually grow.)

If using an array were a requirement, I'd probably use arraycopy to move the elements, then assignment to put the new entry at index 0:

System.arraycopy(myArray, 0, myArray, 1, myArray.length - 1);
myArray[0] = newEntry;

As Bill pointed out, you can't use == and != with strings. I suggest reading the answers to this question for details on that.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • is there a way to use a for-loop or something similar instead of arraycopy? My most advanced tool for this task is using methods. – xarz x Nov 25 '19 at 19:15
  • @xarzx - `arraycopy` *is* a method. But sure: Loop from 4 through 1, copying element n-1 to element n. – T.J. Crowder Nov 26 '19 at 07:19
1

A few things:

  1. This is a use-case for a Linked List, not an array, but I assume this is just an exercise for school or practice. You would just insert the value at the head of the LinkedList.

  2. This is likely where you're having issues: myArray[i] = myArray[i++];. i++ increments the index variable, so you're incrementing it in the for loop as well as in the for loop body. You also need to check that you're not going out of bounds on an array. Basically, for every iteration of your loop, you're incrementing i twice. You probably mean myArray[i] = myArray[i+1]; which will not increment i

  3. myArray[i] != "Null" Don't do this for String comparisons. Use .equals. Preferably "Null".equals(myArray[i]). Even better, don't assign those values to "Null", and just leave them as null and you can do myArray[i] != null. The reason for this is described here: How do I compare strings in Java?

Christopher Schneider
  • 3,745
  • 2
  • 24
  • 38
  • I changed the things you mentioned and it seems to be better now. How do I change the for-loop, so the elements will change their position properly? – xarz x Nov 25 '19 at 19:27