-4
  • List item

  • fill the array with random integers from 1 to the size limit

  • prompt to see if the user wants to do another

  • You will need to use nested for loops to assign values to the elements of the array and to print them // So that the values in the array aren't huge keep them limited to a range that is determined by the number of rows{

  • If you used a Do loop to start the square the While condition could be held here to check a sentinel

** So I have been able to do all of those things except to get my do while to work! When it finishes running through the for statements it asks my print statement then ends the code, doesnt even give me the chance to enter an input for my }while statement.

SOLVED*******

  • possible duplicate of [Java passing scanner into method](http://stackoverflow.com/questions/23462447/java-passing-scanner-into-method) - the answer provided is pretty much the exact same answer that would be given here. – Makoto May 08 '14 at 04:27
  • Got it! Thank you so much! I just needed to make another scanner! – user3614778 May 08 '14 at 04:32
  • what is it?why have you removed the content.If your problem is solved by yourself then post your own answer.If your problem is solved by any of the answer provided below then accept the same – SpringLearner May 08 '14 at 04:39
  • why you want to remove it!! keep it. It may helps someone else – Salman May 08 '14 at 04:39
  • @user3614778 re-edit the question and put the question back. Your thread will not be deleted. Your question was a genuine one and others might face same problem in future. SO does not only care to answer your specific question but also serves as repository for questions. – KNU May 08 '14 at 04:43
  • I just don't want people taking my code!! – user3614778 May 08 '14 at 05:01

3 Answers3

0

From this post, Using scanner.nextLine()

"It's because when you enter a number then press Enter, input.nextInt() consumes only the number, not the "end of line", primitive data types like int, double etc does not consume "end of line", due which this "end of line" remain in buffer ane When input.next() executes, it consumes the "end of line" from buffer from the first input. that's why, your String sentence = scanner.next() only comsume the "end of line", does not wait to read from keyborad."

So, after you entered the number and read it using nextInt(), the end-of-line character remains in buffer, and when you call nextLine(), it reads this character and returns. You should use readLine() for all input with Integer.parseInt() where appropriate.

Community
  • 1
  • 1
blueygh2
  • 1,460
  • 10
  • 13
  • Ah I got it!!! So I just made another scanner called again and it worked.. Thank you guys so much! You can now delete this thread or lock it! Thanks!! – user3614778 May 08 '14 at 04:31
  • You should mark this answer as accepted (tick on the left). That's the way to resolve a question. – indivisible May 08 '14 at 04:32
0

At the bottom of the loop change to answer = input.nextLine(); answer = input.next();. You can refer to http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Totò
  • 1,694
  • 12
  • 17
0

you can put your statement answer = input.nextLine(); inside the if block to be the last statement in the block after for loop block so your code will be like the following after changing the statement place:

import java.util.Scanner;

public class SquareMakerCF{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

String answer="";

do
{
  System.out.print("Enter the number of rows/columns: ");
  int rows = input.nextInt();

  if (rows != 0)
  {
    int [][] matrix = new int[rows][rows];

    for(int row = 0; row < matrix.length; row++)
    {
      for (int column = 0; column < matrix[row].length; column++)
      {
        matrix[row][column] = (int)(Math.random()*rows);
      }
    }

    for(int row=0; row<matrix.length; row++)
    {
      for(int column = 0; column < matrix[row].length; column++)
      {
        System.out.print(matrix[row][column] + " ");
      }

      System.out.println();
    }

    // new statement postion
      answer = input.nextLine();
  }
  else
  {
    System.out.print("Do not enter 0");
  }
  System.out.println("Do you want to do another? Enter yes or no: ");

}
while (answer.equalsIgnoreCase("yes"));


}
}
Salman
  • 1,080
  • 3
  • 26
  • 55