User input in rectangular Array

Trying to get users to put numbers in an array and then out put number

Page 1 of 1

5 Replies - 1673 Views - Last Post: 09 August 2009 - 09:25 PM Rate Topic: -----

#1 sandvader  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 09

User input in rectangular Array

Post icon  Posted 08 August 2009 - 09:03 PM

I'm just trying to set up a rectangular array that will take a user inputed number and then output the numbers. Here's my question how do I get the user input into the array?
Here's my code so far
import java.util.*;
import java.text.*;

public class UserInputArray {
  Scanner sc = new Scanner(System.in);// sets program up to accept user input
  public static void main(String args[]) {

	double m[][] = {
	  {1, 2, 3, 4 },// these just put numbers into the array
	  { 0*1, 1*1, 2*1, 3*1 },
	  { 0*2, 1*2, 2*2, 3*2 },
	  { 0*3, 1*3, 2*3, 3*3 }
	};
	int i, j;

	for(i=0; i<4; i++) {
	  for(j=0; j<4; j++)
		System.out.print(m[i][j] + " ");
	  System.out.println();
	}
  }
}


I'm just baflled on what I should use any guidance would be appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: User input in rectangular Array

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,615
  • Joined: 15-May 08

Re: User input in rectangular Array

Posted 08 August 2009 - 09:47 PM

I won't give you all the code, however this will get the user input into the array. The second part you asked is how to print the array (hint:: its almost identical to getting values in) - in which you will need to iterate through the array and print out each value... eg. to print the value at [0][1] you would do something like System.out.println(myArray[0][1]);

Anyway here is a snippet
Scanner input = new Scanner(System.in);

int[][] myArray = new int[5][5];

// use a loop and nested loop to fill array
for(int i = 0; i < 5; i++)
{
	for(int k = 0; k < 5; k++)
	{
		System.out.println("Enter a Number :: ");
		myArray[i][k] = input.nextInt();
	}
}


Hope that helped
Was This Post Helpful? 1
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4187
  • View blog
  • Posts: 11,845
  • Joined: 18-April 07

Re: User input in rectangular Array

Posted 08 August 2009 - 09:47 PM

Well you are on the right track. You have declared a scanner object correctly, so now all you need to do is call its nextInt() method which will read the next integer in from the user's input. Below is your code with another loop that prompts for user input...

import java.util.*;
import java.text.*;

public class UserInputArray {
  static Scanner sc = new Scanner(System.in);// sets program up to accept user input
  public static void main(String args[]) {

	double m[][] = {
	  {1, 2, 3, 4 },// these just put numbers into the array
	  { 0*1, 1*1, 2*1, 3*1 },
	  { 0*2, 1*2, 2*2, 3*2 },
	  { 0*3, 1*3, 2*3, 3*3 }
	};
	int i, j;

	for(i=0; i<4; i++) {
	  for(j=0; j<4; j++) {
		System.out.println("Please enter a number: ");
		
		// Notice here we use our scanner to read in another integer from the input (that the user types in)
		m[i][j] = sc.nextInt();
	  }
	}
	
	for(i=0; i<4; i++) {
	  for(j=0; j<4; j++) 
		System.out.print(m[i][j] + " ");
	  System.out.println();
	}
  }
}




Other methods that the scanner has includes nextBoolean, nextFloat and nextDouble (along with many more) that can help read in various types of input. Enjoy!

"At DIC we be input collecting code ninjas... we hold local code ninja meetups where we discuss the issues and collect feedback, have cookies, then try to kill each other!" :snap:
Was This Post Helpful? 1
  • +
  • -

#4 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,615
  • Joined: 15-May 08

Re: User input in rectangular Array

Posted 08 August 2009 - 09:50 PM

Quote

"At DIC we be input collecting code ninjas... we hold local code ninja meetups where we discuss the issues and collect feedback, have cookies, then try to kill each other!"


Its true
Was This Post Helpful? 0
  • +
  • -

#5 sandvader  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 09

Re: User input in rectangular Array

Posted 09 August 2009 - 02:13 PM

View Postbbq, on 8 Aug, 2009 - 08:47 PM, said:

I won't give you all the code, however this will get the user input into the array. The second part you asked is how to print the array (hint:: its almost identical to getting values in) - in which you will need to iterate through the array and print out each value... eg. to print the value at [0][1] you would do something like System.out.println(myArray[0][1]);

Anyway here is a snippet
Scanner input = new Scanner(System.in);

int[][] myArray = new int[5][5];

// use a loop and nested loop to fill array
for(int i = 0; i < 5; i++)
{
	for(int k = 0; k < 5; k++)
	{
		System.out.println("Enter a Number :: ");
		myArray[i][k] = input.nextInt();
	}
}


Hope that helped

It indeed helped very much my ultimate goal on this was to see exactly where the data was being put in. All the samples that I ever found had the data put in manually and I just couldn't understand how it all fell together. Here is my final code I prefilled in a few arrays and then by putting different variables into i & k I can easily see how the arrays get populated
import java.util.*;
import java.text.*;

public class UserInputArray5 {
  static Scanner sc = new Scanner(System.in);// sets program up to accept user input
  public static void main(String args[]) {

	double m[][] = {
	  {8, 8, 8, 8 },// these just put numbers into the array
	  { 7, 7, 7, 7 },
	  { 6, 6, 6, 6 },
	  { 5, 5, 5, 5 }
	};
	int i, j;

	for(i=0; i<1; i++) {// i fills in data by row
	  for(j=0; j<4; j++) {// fill in data by column
		System.out.print("Please enter a number: ");

		// scanner reads in another integer from the input (that the user types in)
		m[i][j] = sc.nextInt();
	  }
	}

	for(i=0; i<4; i++) {
	  for(j=0; j<4; j++)
		System.out.print(m[i][j] + " ");
	  System.out.println();
	}
  }
}


Just a simple little code that really helped me see, thank you again for helping me get there
Was This Post Helpful? 0
  • +
  • -

#6 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,615
  • Joined: 15-May 08

Re: User input in rectangular Array

Posted 09 August 2009 - 09:25 PM

No problems at all matey, glad it helped :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1