Pass User Input - Initializing Array In Method

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


ADVERTISEMENT

How To Pass Array To Method

Aug 3, 2014

So if I assigned values in arrays like the below:

This is the method in main class:

public static void trainer(){
trainer[] trainerArr = new trainer[4];
trainerArr[0] = new trainer("Ben Yap", "Male", "Kuala Lumpur", 10000, "Yoga");
trainerArr[1] = new trainer("Wilson Ting", "Male", "Kuala Lumpur", 10001, "Kick-boxing");

[Code] .....

How can i pass these arrays into another method in the main class?

View Replies View Related

Unable To Pass A Two Dimensional Array To A Method

Sep 27, 2014

I have a question about an error I am getting when trying to pass a two dimensional array to a method. I keep getting the "incompatible types, int cannot be converted to int[][]". I am getting the error in a few different place (see comments - at the first call of the method, at a recursive call, and at the return statement. I believe I am passing the same type of array in all cases to the type of array defined in the method parameters.

Below is my code.

// this is a call from the main method
int[][] c = new int[temp1.length][temp1.length];
c = MatrixMultiply(a,b); // this is first place the error occurs
} // end main
public static int MatrixMultiply(int[][] A, int[][] B) {
// throw new UnsupportedOperationException("Not supported yet.");
int a[][] = A;

[Code]....

View Replies View Related

Trying To Pass Java Array To Method But It Is Not Working

May 5, 2015

***** start code ***

public class AddArray {
public static void main(String[] args) {
int sum = 0;
sum = addArray(myarray);
System.out.println(" hello");
System.out.println("This program will create an array then pass the array to an method to be totaled");
int myarray[] = new int [6];

[Code] ....

View Replies View Related

Initializing Constructor As Parameter For A Method?

Jan 2, 2014

So, I am learning Swing, and Swing Layouts. I just came across a really confusing (for me) line of code.

Java Code:

setLayout(new BorderLayout()); mh_sh_highlight_all('java');

So, this is weird for me because I don't really understand why the BorderLayout class constructor is being initialized as a parameter for the setLayout..

View Replies View Related

Get Grid Method That Gets Input From User

May 9, 2014

I have a get grid method that gets input from the user. I also have a display grid method that is supposed to display the get grid method but it doesn't. What do I do to make the display grid method work properly. I am not allowed to use a main method.

private int a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4,d1,d2,d3,d4;
public void getGrid ()
{
System.out.println();
System.out.print ("Enter Row 1: ");
a1 = i.nextInt();
a2 = i.nextInt();
a3 = i.nextInt();
a4 = i.nextInt();
System.out.print ("Enter Row 2: ");
b1 = i.nextInt();
b2 = i.nextInt();
b3 = i.nextInt();
b4 = i.nextInt();

[code]....

View Replies View Related

Scanner Array Initializing

Oct 12, 2014

Scanner sc = new Scanner(System.in);
Question question = new Question();
Quiz quiz = new Quiz();
System.out.print("Enter the prompt: ");
question.prompt = sc.nextLine();

[Code] .....

Here is the example run that I am trying to achieve:

* 1. Enter the prompt: What is the capital of the USA?
* 2. Enter a possible answer: New York City, NY
* 2. Is New York City, NY the correct answer (y/n)? n
* 3. Enter a possible answer: Pittsburgh, PA
* 3. Is Pittsburgh, PA the correct answer (y/n)? n
* 4. Enter a possible answer: Washington, DC
* 4. Is Washington, DC the correct answer (y/n)? y
* 5. Enter a possible answer: Chicago, IL
* 5. Is Chicago, IL the correct answer (y/n)? n

The problem is that my for loop runs through correctly the first time, but doesn't seem to react to me initializing my choices array after that. It ends up looking like this:

Enter the prompt: What is the capitol of the USA?
Enter a possible answer: New York City, NY
Is New York City, NY the correct answer (y/n)?n
Enter a possible answer: Is the correct answer (y/n)?

But I cannot understand why it's doing that.

View Replies View Related

C Struct - Initializing 3D Array?

Oct 23, 2014

I have a C struct as follows:

struct myHAI
{
int id;
char celcius[5];
char fahrenheit[5];
 
[Code] .....

I think the Java code is just a little to kludgey. I have to add 256 records to the 3D array which means I have to call the method 256 times. I'd really like to initialize the class all at once similar to the C struct. But I don't know if that is possible. Thus, any less kludgey way of initializing a 3D array?

View Replies View Related

Initializing Array - Equal Sign?

Nov 15, 2014

i know that int [][] x = new int[2][2] will generate a 2x2 array but I'm looking at a certification mock question and I see double [][] da = new double [3][]. What is the empty [] on the right hand side of the equal sign trying to tell me? Is there some default value?

View Replies View Related

2D Array Input User For GUI

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

Array Initialization Method - Filling Entire Array With Last Input Value

Feb 7, 2015

I am passing input from the user to a method that will initialize an array of the data (scores in this case). The method is filling the entire array with the last input value.

array initializer method

Java Code:

public static float[] inputAllScores(float validScore) {
float[] diverScores = new float[7];
for (int i = 0; i < diverScores.length; i++) {
diverScores[i] = validScore;
}
return diverScores;
} mh_sh_highlight_all('java');

[Code] .....

View Replies View Related

User Input For Array Size

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

Allow User To Input A Value Into Cell In 2D Array?

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

Sorting Array Of Strings From User Input

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

Polynomials Into Array Using User Input With Scanner

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

Boolean Method - Adding Information To Mailing List Based On User Input

May 5, 2014

This program contains a superclass and a subclass that will gather the following information from the user:

name, address, phone number, and customer number.

Everything works fine except that I have to create a boolean method in this program that is required to determine based on user input whether they want to be added to a mailing list.

I cannot get this method to work with main, each time it is called it will always return the value but main will constantly read the last statement (else, where it will read "not wanting to be added to the mailing list).

The only way I can get this part of the program to work is by adding an equals method in main that ignores the case, but I am required to write a boolean method so this is not allowed.

Superclass:

public class Person
{
private String name;
private String address;
private String phoneNum;
public Person(String pName, String add, String number)
{
name = pName;
address = add;

[Code] ...

View Replies View Related

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 View Related

How To Change Value In Array Through User Input And Print Out Result

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

User To Input 10 Numbers - Find Smallest Element In Array

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

Pass Arguments From Main To Paint Method

Feb 5, 2015

I want to take command line arguments and pass them to a paint method. This is a test program that will just draw some equations. How can I get the input array clinputs[] to be used in public void paint( Graphics g) ?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LinePlot extends JFrame {
public LinePlot() {
super( "Line Plot" );
setSize(800,600);

[Code] .....

View Replies View Related

Does A BufferReaderobject Remember Its Last Location If Pass It On To New Method

Jun 28, 2014

createBufferReader(){
BufferedReader br = new BufferedReader(new FileReader(new File(...)));
br.readLine();
br.readLine();
secondMethod(br);

secondMethod(BufferedReader br){
br.readLine(); //

will the br continue from the first line of the file or the one he last read in the createBufferReader method?

View Replies View Related

How To Pass A Loop In Method String ToString

Apr 7, 2014

I am looking to pass a long list of results in a String toString() method,this is my code

public void newlist(){
for(int i = 0 ; i <= nbComposant;i++){
System.out.print(ref+i+" (quantity "+quantity+i+")");
}
}
public String toString(){
return newlist();
}

View Replies View Related

How To Pass Object Type To A Method / Then Create Instance

Aug 9, 2014

Essentially, the code I want is:

public void randomCreate(ParentObject obj){
int x = random(0-4); //pseudo
int y = random(0-4); //pseudo
create new ParentObj(x,y);
}

ParentObject is actually abstract, so you would only ever pass one of its children objects to it, and a child object of that type would be created. It seems like there should be a way to pass a type, rather than an object, and then create an instance later down, but I don't know if that is actually possible, or if it is poor programming style.

View Replies View Related

Java Program That Will Ask A User To Input Grades Until User Inputs Sentinel Value

Apr 9, 2014

Write a java program that will ask a user to input grades until the user inputs the sentinel value -1. The program should test each input to determine whether 0<=grade<=100. If the grade entered is within this range, the program should print "Grade accepted;" if not, the program should print "Invalid input".

View Replies View Related

Program That Takes User Odd Number And Print Prime Numbers Lower Than User Input

Nov 4, 2014

I have been struggling with this program for weeks. This program is supposed to take a user's inputted odd number and then print out all prime numbers lower than that number.

public class PrimeNumber
{
Scanner scan = new Scanner(System.in);
int userNum;
String neg;

public void getUserNum()

[code]...

View Replies View Related

Pass Array Data Between Methods

Feb 6, 2015

How do I pass the data within an initialized array from inside one method to another method of the same class? Will I need to return the array, assigning it to a temp array, which will then be passed as an argument to the other array? The idea is to create an array for an entire year, and be able to manipulate or edit data for a particular month using the other method.

public class Temperature {
static Scanner input = new Scanner(System.in);
static String [] monthArray = {"January", "February", "March", "April", "May",
"June", "July", "August", "October", "November", "December"};
public static void main(String[] args) {

[Code] .....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved