Accepting And Assigning User Input Into Multidimensional Array?
Apr 7, 2014
I have been asked to write a library program that will keep record of books and the year it was published. The program should ask the user how many rows he wants accept the string input from the user and display them in rows and columns. This is how i code it
package multidimension;
import java.util.Scanner;
public class bookrecords {
public static void main(String[]args){
//declaring a scanner variable
Scanner input =new Scanner(System.in);
[code]....
View Replies
ADVERTISEMENT
Nov 16, 2014
I have a Homework that's on Polymorphism and inheritance. The problem is of a triangle class. The super class is GeometricObject and the subclass Triangle. I have the parent and child classes compiling, now it's the test program.
The problem is the examples in the book don't show anything about prompting input for objects. The objects in the Triangle class are: side1, side2, and side3 and so I created objects in the test: Triangle sideOne = new Triangle(); . I've tried to compile but jGrasp doesn't like anything I do and the instructor in my class hasn't shown us examples yet. Can I prompt input for reference type and not just primitive type?
View Replies
View Related
Mar 25, 2014
Java Code:
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class SortTable {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
[Code]...
View Replies
View Related
Oct 28, 2014
I am having trouble with an assignment. I need the user to input the size of the array and print when asked. In my program, it prints 100 numbers instead of the user input number, such as 15.
import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;
public class Lab9 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int [] values = new int [100];
[Code] .....
View Replies
View Related
Apr 16, 2015
I am trying to do a simple noughts and crosses game. I have displayed a board using a 2 dimensional array and display it using a for loop. The array is of type int. What I want to do is allow the user to choose a specific cell within the array to change to an x or o but I am not sure how to go about doing this. I have seen lots of examples of noughts and crosses online but seem to all be examples of how to check a win or lose situation.how to allow a user to choose certain cells in a 2d array would be great.
View Replies
View Related
Apr 3, 2015
I am working on an assignment where I initially have to take as input a number. That number will be the number of strings the user is going to type. As they are typed, they are to be sorted alphabetically, then printed. It took me a while to get this far, and I am stuck on how to properly invoke the other methods in main. There has to be at least three methods: main, one to do the sorting, and one to print the new array. I'm quite sure there are mistakes in my code, but this is definitely a challenge for me. Here is the code. Keep in mind that the methods being invoked are blank on purpose.
I am not allowed to use the Arrays, Arraylist or Collection class. This is basically to test my ingenuity and to see how arrays actually work.
import java.util.*;
public class Sort_as_Inserted {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of elements to be sorted: ");
String element_number = input.nextLine();
[Code] ....
View Replies
View Related
Feb 19, 2014
Assignment: Your task is to implement a similar scheme to store polynomials of any number of terms, such that the number of terms and the components (coefficient, variable and exponent) of every term are entered from the keyboard. To implement the interactive input we will using the Java class Scanner, defined in the java.utils standard package. The Scanner class can be used in Java to read data types from a file.
Since the input console (keyboard) is treated as the file called System.in, we can create a Scanner for that input stream as new Scanner (System.in), as shown below. Once you define a Scanner object, using its method next() you can read Strings from the file/keyboard. The incomplete program below is your assignment. You are supposed to complete without changing the existing code. Your output should be the terms of the polynomial entered by the user, separated by + signs. Additional instructions in the code below, that you will change to achieve the requested functionality.
import java.util.Scanner;
public class Polynomials {
public static void storeTerm (int coeff, String var, int exp, String poly[][], int
where){
//ENTER THE COEFFICIENT, VARIABLE AND EXPONENT INTO THE
//ARRAY POLY THAT REPRESENTS THE POLYNOMIAL, AT POSITION "where"
//THAT RANGES BETWEEN INDEX 0 AND POLY.LENGTH-1
[Code] ....
Anyways, I'm not looking for straight answers. I just want to know which section I should start on first because I was advised to tackle programs one problem at a time.
View Replies
View Related
Nov 21, 2014
what would the return be for adding two array's together,
public static int sum (int[][] a, int[][] b) {
int[] [] c = new int[a[0].length][b[0].length];
for (int i=0; i< a.length; i++){
for (int j=0; j<a[i].length; j++){
c[i][j] = a[i][j] + b[i][j];
}
}
return c[i][j];
}
My return is wrong...... I know, Any hints?
View Replies
View Related
May 5, 2015
At the bottom of this code snippet you will see the print statement. I am trying to divide the multidimensional array by its sum. Can this be done simply in the System.out statement, or do I need a separate method?
private static void Histogram() throws IOException {
int[][][] ch = new int[4][4][4]; /* 4 for 64 and 8 bins, 8 for 512 bins */
BufferedImage image = ImageIO.read(new File("airplane_training1.jpg"));
for(int x = 0; x < image.getWidth(); x++)
for(int y = 0; y < image.getHeight(); y++) {
[Code] .....
View Replies
View Related
Feb 7, 2015
I need to pass user input from the main method, which is then validated using another method that is returned as a valid score, and then I pass the valid input to another method that stores the data in an array. The array is initialized within the method. I tried to use an if-else statement to initialize the array, because I originally did this at the beginning of the method. I soon learned that I was creating a new array everything I accessed the method. Needless to say, this isn't working either.
public static void main(String[] args) {
int judges = 7;
float[] validScores = new float[judges];
for (int i = 0; i < judges; i++) {
float score = -1;
[Code] ....
View Replies
View Related
Dec 14, 2014
I have to do a small program about parking. In the 2-d array (parking lot), contains different kinds of cars, the user have to :
1)enter which car he wanna move,
2)what direction (w,a,s,d) and after that,
3) how many moves( not out of bound) he wants to make, and finally
4) we print out the new parking lot for his next move
And i am stuck at how to move the car to corresponding position and then prompt user for the next move?
For example:
I want to move A to the right by 1 (d)
擷取.PNG
print result:
擷取1.PNG
How do I do that? The code that i have right now
public class CarParkGame {
public static void main( String [] args) {
String carPark[][] = new String [6][6] ;
carPark[0] = new String[] {"A","A","A","0","0","0"};
carPark[1] = new String[] {"b","0","0","0","0","c"};
carPark[2] = new String[] {"b","X","X","0","0","c","<exit>"};
[Code] .....
View Replies
View Related
Mar 13, 2015
import java.util.Scanner;
public class ColumnSum {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int userpick = 0;
int sum = 0;
int [][] matrix = {{5, 9, 87, 74, 12, 7}, // row 1
[code]...
Right now my code gets all the numbers in a row and adds those up, but I want it to get the numbers in a column and add them instead. The problem is I don't know how to get the userpick (the number that the user picks to determine which column gets added) to be set to that particular column.
View Replies
View Related
Jan 14, 2014
I come to the point: I just started to learn java through various manuals and in one of them I came across a declaration of an array that I do not understand:
int[][] multiArr = new int[2][];
the manual says that you can allocate the multidimensional array multiArr by defining size in only the first square bracket but I can't undestand how you can use this array. Seems to be no way to store data with it!
View Replies
View Related
Feb 13, 2014
I tried this:
String[][]f = new String[1][1] {{"Harry"}{"Hairy"}};
I also tried this:
String[][]f = new String[1][1] {{"Harry"},{"Hairy"}};
but I get an error
View Replies
View Related
Mar 2, 2015
public class StuTest {
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args){
int[][] testScores; // [students][tests]
String[] stuNames;
[Code] ....
The method I am having issues with is the "printStudentReport" method. I am trying to calculate the average of a randomly generated number of students and tests. For my "printTestReport" method, I have to calculate the average of the test by test number.
View Replies
View Related
Feb 2, 2015
I've found different examples on line, but none that use the split method with a multidimensional array. I would like a user to input coordinates (x,y) for city locations and store them into a 2-D array. For the user to input the x,y-coordinates on one line I need to use split(',') and then parse the string array to a double which will then be used to calculate the distances from one another.
My issue is how to store the String vales into the 2-D array. I do not want to store the x-value at even (cityArray[0]) and y-value at odd (cityArray[1]) 1-D locations.
View Replies
View Related
Dec 5, 2014
I want to print each value in the multi-dimensional array using a for loop but there is a problem.
Here is the script:
Java Code:
public class LearningJava
{
public static void main(String[] args) {
int[][] Grid = {
{12, 24, 36, 48, 60},
{1, 3, 5},
[Code] ....
Printing i: 0, x: 12
Printing i: 0, x: 24
Exception in thread "main" Printing i: 0, x: 36
Printing i: 0, x: 48
Printing i: 0, x: 60
java.lang.ArrayIndexOutOfBoundsException: 5
at LearningJava.main(LearningJava.java:11)
It's suppose to get the length of each array and print all the values in that array and then move to the next one.
I tried adding .length
Java Code: for(int x = 0; x < Grid[i][x].length; x++) mh_sh_highlight_all('java');
but then it doesn't work at all.
View Replies
View Related
Dec 19, 2014
I have to write a program that will read a picture and then print out the number of blocks inside it.I have to read the picture as a binary matrix of the size r c (number of rows times number of columns).The blocks are groups of one or more adjacent elements with the value 1.
- Blocks are built exclusively of elements with value 1
-Each element with value 1 is a part of some block
-Adjacent elements with value 1 belong to the same molecule.
We only take into account the horizontal and vertical adjacency but not diagonal.
INPUT:
In the first line of the input we have the integers r and c, separated with one space. Then we have the r lines, where each contains s 0's and 1's.The numbers inside the individual lines are NOT separated by spaces.The OUTPUT only print the number of blocks in the picture.
Example:
INPUT:
7 5
01000
00010
00000
10000
01000
00001
00100
OUTPUT:
6
THIS IS WHAT I CAME UP SO FAR:
import java.util.Scanner;
class Blocks{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
[code]...
View Replies
View Related
Dec 2, 2014
I am trying to write a code that asks the user to input ten numbers and then finds and displays the smallest number out of the ones given. I am supposed to implement arrays into the program to do this. But the problem I have run into is that when I compile the code in jgrasp, I am given several error messages and I am not quite sure what I have done wrong. I'm assuming it is either a syntax or a logical error on my part but reading over the code I do not understand what is causing these errors.
This is the most current draft of my code:
import java.util.Scanner;
public class Exercise7_9 {
public static void main(String[] args) {
double[] numbers = new double[10];
//Enter ten double numbers: Scanner(System.in)
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Please enter ten numbers: ");
[Code] ....
/* Sample Run:
Enter ten numbers: 1.9, 2.5, 3.7, 2, 1.5, 6, 3, 4, 5, 2
*/
And these are the exact error messages:
----jGRASP exec: javac -g Exercise7_9.java
Exercise7_9.java:35: error: '.class' expected
if (double m > list[i]) {
^
Exercise7_9.java:35: error: illegal start of expression
if (double m > list[i]) {
[Code] .....
View Replies
View Related
Apr 8, 2014
how you can do array.length, I have an array with [][][][], and what do I do to find the length of one section?
So let's say
Java Code: int array[][] = {
{5, 5, 5, 5},
{4, 4, 4, 4},
{3, 3, 3, 3}
} mh_sh_highlight_all('java');
How do I find out the length of the {5, 5, 5, 5} which is 4. Because there are 4 place-holders.Is there a way?
View Replies
View Related
Feb 18, 2014
I have a file which contains certain positions ([a][b]) that require to be placed in a certain multi-dimensional array. For example I might have an array String[][] that is a size of 6x6 but only have values in positions [2][1] and [3][2]. Because it is important for me to maintain the given array size and also do certain actions with the given positions I cannot modify the size. In addition I need to count the surrounding neighbors each element has (including elements that are null). However because some of my further code cant process with null elements I need to remove all null elements with " " (blank).
I am not sure how this is done or if it's even possible. If it is not possible how can I do something as close as possible to my needs?
View Replies
View Related
Sep 10, 2014
I am working on a project on bluej called battleship and being new to it I cant figure out how to generate a ship(3 co ordinate long) on a 10X10 multi dimensional array. nothing graphical just plain co ordinates.
View Replies
View Related
Mar 18, 2014
Code a Java method that accepts a String array and a String. The method should return true if the string can be found as an element of the array and false otherwise. Test your method by calling it from the main method which supplies its two parameters (no user input required). Use an array initialiser list to initialise the array you pass. Test thoroughly.
public class test {
public static void main(String[] args) {
Printhelloworld();
String[] verbs = {"go", "do", "some", "homework"};
printArrays(verbs);
[Code] .....
View Replies
View Related
Mar 22, 2015
import java.util.*;
import java.text.*;
public class Linearize {
public static void main(String[] args) {
// Create new Scanner and Random and Decimal objects
Scanner s = new Scanner (System.in);
Random g = new Random ();
DecimalFormat oneplaces = new DecimalFormat (".00");
[code]....
I am really close to finishing this program and my output is almost there, except it's only printing out the first half or so of the array. I have two for loops and two counters to determine the size of the array and then copy the multidimensional array's values into the 1D array, but it only prints out the first half of the array as such:
How many rows in the array? 4
How many columns in the array? 2
This 2D array contains:
35.23, 26.94,
99.48, 66.69,
7.31, 25.18,
64.53, 21.25,
Converted to a 1D array:
35.23, 26.94, 99.48, 66.69,
And yes, I realize that I should be formatting my Print statements with % but my instructor doesn't seem to care about it (he never taught it to us) so for the time being I am being stubborn and using .
View Replies
View Related
Aug 20, 2014
I am trying to write a code for multidimensional array, allocate memory for variables and access the value of the arrays. I want them to be shown as rows and columns. but my code only shows one column and no rows. here is my code:
public static void main(String[] args) {
int[ ][ ] aryNumbers = new int[2][2];
aryNumbers [0][0] = 1;
aryNumbers [0][1] = 2;
aryNumbers [1][0] = 3;
aryNumbers [1][1] = 4;
int rows = 2;
int columns = 2;
[code]....
View Replies
View Related
Apr 19, 2013
I just started in java programming and into Arrays multidimensional. I started a simple 2 dimensional array with 5 names with genders. I 'm having issues because it does not want to print out the names with correct genders Male/ Female. The simple program should print out the 5 names with gender type.
example:
Jack - Male
Sally - Female
Dave - Male
Sue - Female
Brian - Male
Here is what I came up with so far:
public class Multiname
{
public static void main(String[] args)
{
//String[][] multiname;
String [][] multiname =
[Code] .....
This is the result:
multiname : Jack
multiname : sally
multiname : Dave
multiname : Brian
multiname : Sue
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at MultiD.main(MultiD.java:29)
View Replies
View Related