Hi, i have been working on this program and will display the array contents to the user and ask how many positions he would like to shift off the right side of the array, and replace onto the left side.

This is what is supposed to look like:

Array contents: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Shift how many positions? 5

Array contents: 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10
Shift how many positions? 2

Array contents: 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8
Shift how many positions? 0

Array contents: 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8
Shift how many positions? -8

Array contents: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1
Shift how many positions? 15

Array contents: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1
Shift how many positions? 17

Array contents: 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Shift how many positions? q

And im getting this:
Array Contents: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15

Shift how many positions?5

Array Contents: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15

Here is the main class:

public class Shift1{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

            Scanner kp = new Scanner(System.in);

                final int size = 15;
        char q = 'y';
        boolean flag = false;

        Shifter test = new Shifter(size);

        test.display();
        System.out.println();


            Scanner input = new Scanner(System.in);
            System.out.print("Shift how many positions?");{

            int value1 = input.nextInt();
            test.shift(value1);
            test.display();
        }
    }
}

Here is additional class:

public class Shifter 
{

public int [] data=new int[15];
    public Shifter()
    {
        int size=0;
}

public Shifter(int size){

    for (int i = 0; i < data.length; i++)
    {
        Random r = new Random(15);
        int second = r.nextInt(15) + 1;
        int temp = data[i];
        data[i] = data[second];
        data[second] = temp;
    }
}

public void shift(int pos){ 
    for(int x=0; x<pos; x++)
    {
        int cnt = data.length-1;
        int temp = data[cnt];
        for(cnt=data.length-1; cnt>0; cnt--)
    { 
        data[cnt] = data[cnt]-1;
    }
        data[0] =temp;
    }
}
public void display(){
    String values = "";
        for (int i = 0; i < data.length; i++)
        {
          if (i < 15)
        {
          values += (i + 1);
           if (i < 14)
        {
          values += ", ";
        }
    }
}
        System.out.printf("Array Contents: %s \n", values);
    }
}

Inshifter(int size) you are never filling the array. Since it was never initialized, the random number stuff is not only doing nothing, but as far as I can tell it is unnecessary. The loop SHOULD be (to my mind at least) like the following:

    for (int i = 0; i < data.length; i++)
    {
        data[i] = i+1;
    }

That way, your array is initialized to the numbers you want - 1 through 15 sequentially. Next comes the shifting. Take the number of elements to shift (which come off of the tail of the array), allocate a temporary array of the appropriate size, and put those elements in it. Then, move the elements of the array to the right that number of entries, and replace the front of the array with the contents of the new/temporary array. Bingo, you are done. This is as much as you will get from me (no more code) as this is obviously a homework problem. We will help, but we won't do it for you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.