-1

So my code looks like this so far:

public class PancakeSort {
   public static int flip(int n) {
      int temp = 0;
      for (int i = 0; i < (n+1) / 2; ++i) {
         int[] pancakes = new int[n];
         temp = pancakes[i];
         pancakes[i] = pancakes[n-i];
         pancakes[n-i] = temp;
      }
      return temp;
   }

   public static void sort (int[] pancakes) {
      for (int i=0; i<pancakes.length; i++){
         if (pancakes[i] > pancakes[i+1]){
            flip(i+1);
         }
      }
      System.out.println(pancakes);
   }
   public static void main(String[] args) {    

   }
}

But how I input a whole array of integers using standard input (StdIn.readLine())? I understand that the code might not be correct and I'm working on figuring that out,and I'm also aware that this question has been asked before in this site, but not specifically using the standard library and that is where I'm stuck.

2
  • Yes I checked them all out. But none of them used standard input(which is different way). And also the no. of elements in the array all depend on the input. Commented Feb 14, 2015 at 6:00
  • StdIn is not anything provided by core Java. Java provides System.in for reading from stdin, which you will see is used in the referenced question above. If you don't want to read from System.in then please explain what you think "using the standard library" means. Commented Feb 14, 2015 at 6:03

2 Answers 2

0

You can send integer array as input

PancakeSort pancakeSort = new PancakeSort();
pancakeSort.sort(new int[] { 100, 50, 89, 2, 5, 150 });

or Use scanner class as

int arr[] = new int[10];
Scanner sc = new Scanner(System.in);
int i = 0;
while (sc.hasNextInt()) {
    arr[i] = sc.nextInt();
    i = i + 1;
}

PancakeSort pancakeSort = new PancakeSort();
pancakeSort.sort(arr);

But in last case you must not increased the size of array.Otherwise it will give arrayIndexOutOfBoundException

Sign up to request clarification or add additional context in comments.

Comments

0

I believe you may be referencing StdIn such as a class like this one?

http://introcs.cs.princeton.edu/java/stdlib/StdIn.java.html

If so, then to get an int from the console you just call StdIn.readInt. An example of how you could approach this is:

public static void main(String[] args) 
{ 
        System.out.println("Enter number of pancakes, or enter 0 to quit");

        int[] pancakeArray = new int[0];

        while (true)
        {
            try
            {
                int entry = StdIn.readInt();
                if (entry == 0)
                {
                    break;
                }

                int[] expandedArray = new int[pancakeArray.length + 1];
                System.arraycopy(pancakeArray, 0, expandedArray, 0, pancakeArray.length);

                expandedArray[pancakeArray.length] = entry;
                pancakeArray = expandedArray;
            }
            catch (Exception e)
            {
                System.out.println("Invalid entry detected, closing input");
                break;
            }
        }

        System.out.println("Pancake array length: " + pancakeArray.length);
        sort(pancakeArray);

        System.out.println("Final pancake array in order:");
        for (int entry : pancakeArray)
        {
            System.out.println("Pancake value: " + entry);
        }
}       

This would read int after int until they entered 0 or an invalid value, then it would call your sort routine from there. There are issues in your sort routine but you said you wanted to look at that, so I will let you figure that part out.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.