Associating Array With Input In Java
May 11, 2014
OK! I have a declared and populated string array containing final tennis scores i.e.{"love", "15", "30", "40", "game"}.The user has already entered the score as integers 0,1,2,3,4. I need to design a method in a set get class to use the score input in the main class to display the final score in the main.here's what I got:
import java.util.*;
public class DemoTennisGame
{
public static void main(String[] args)
{
String player1;
String player2;
int score1;
int score2;
[code]....
View Replies
ADVERTISEMENT
Dec 17, 2014
In my project I need to create a pizza ordering system. I have created a array list of pizza toppings and each pizza topping must have a cost property associated with it, then the user can pick the toppings they want and the cost of each topping will be added up and the total cost of the whole pizza will be displayed. But I don't know how to associate a cost with each topping. I have been told that I can access the cost through a get/set method and store it in a private member variable. But I don't know how to do this?
I've tried to do it, but it hasn't worked. This is the code
public void setCost( double cost){ //my get/set method
this.cost = cost;
}
public double getCost(){
double price = 0;
return price;
[Code] .....
But there is an error on each of the ingrediantCost method lines, saying
The primitive type double of cost does not have a field Pepperoni
...and the same for chicken, ham and onion.
View Replies
View Related
Aug 3, 2014
I am working on a problem where i have to create a 2d array with given input of the dimensions (odd number) of array, along with a number within the array and to then print out all of the numbers surrounding that number.
Anyway, i am working on simply making the spiral, which should look like the one below.
n = 3
7 8 9
6 1 2
5 4 3
where the 1 always starts in the center with the 2 going to the right, 3 down, then left etc. etc. I was able to create the code by starting on the outer edges rather than the center and working my way to the middle, however my code always starts from the top left and goes around to the center where it needs to start from the top right. I am having trouble altering my code to meet this criteria. This is what i have thus far.
import java.io.*;
public class Spiral
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number of elements : ");
int n=Integer.parseInt(br.readLine());
[Code] .....
View Replies
View Related
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
Oct 25, 2014
Create a one dimensional array which holds 10 values. Ask the user to input an index value between 0 and 9. Print the value the user selected. Be sure to explain the output to the user. That is my assignment, and here is my code:
import java.util.Scanner;
public class Array {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a[]= new int[9];
a[0] = 10;
a[1] = 20;
[code]....
I don't know how to use the scanner to get someone's input properly.
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
Dec 10, 2014
I have this very simple application just to test console input:
import java.util.ArrayList;
import java.util.Scanner;
public class WriteTester {
[Code]....
When I let it run, only every third entry is put into the array list and I have to hit "enter" three times for the "break" in line 21 to trigger. I cannot find out why.
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
Jul 17, 2014
how do I define array elements using input?
<script language="Javascript">
/*//////////////////////////////////data base array/////////////////////////////////////*/
var array ga[]; /*guest array*/
ga[0] = "Joe";
ga[1] = Buck;
ga[2] = 11;
ga[3] = Pine;
ga[4] = Street;
ga[5] = Apt;
[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
Jul 18, 2014
Creating an array searcher wherein it all depends on the user input. On the first user input it would be the array to search from while the second input would be something to be searched for inside the loop?
View Replies
View Related
Mar 13, 2014
I have to take user input and then count how many times each number that the user input and print each one out. For some reason, I can't even get the for loop statement to print and it's pretty much the same as my other program except for the loop which is a little different.
//User inputs numbers between 1 and 100, program counts how many of each integer is and ends with a 0
import java.util.Scanner;
public class occurrence {
public static void main(String[] args) {
//scanner/values
Scanner input = new Scanner(System.in);
int number = 0;
int c = 0; //array count
[Code] ....
View Replies
View Related
Feb 8, 2014
What I'm trying to do is compare String input to a char array. Let me make it a little more plain, I'm working on a cipher assignment, and my line of thought is this: I will get String input from the user, COMPARE the characters in the string input to an alphabet array, which will then be compared to the cipher array so that the cipher's counterpart can be chosen over the alphabet's. Any way that I might compare the random input keyed in by the user to that alphabet array?
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
Dec 11, 2014
I am trying to change an input String to an array of characters, but it only stores the word before the space into the array. Here is the code:
Scanner scanner = new Scanner(System.in);
System.out.println(" Enter text: " );
String text = scanner.next();
char[] characterArray = text.toCharArray(); // convert string to array of characters
String char = "";
for( i = 0; i < characterArray.length; i++) {
char = char + characterArray[i]
} System.out.println(char);
Just typing hello gives me hello, but when I type hello world it does not type in the word "world".I am trying to change an input String to an array of characters, but it only stores the word before the space into the array.Here is the code:
Scanner scanner = new Scanner(System.in);
System.out.println(" Enter text: " );
String text = scanner.next();
char[] characterArray = text.toCharArray(); // convert string to array of characters
String char = "";
for( i = 0; i < characterArray.length; i++) {
char = char + characterArray[i]
} System.out.println(char);
Just typing hello gives me hello, but when I type hello world it does not type in the word "world".
View Replies
View Related
Nov 15, 2014
So in my code, I have the user input a number, and the program will then display that element in the array (I've done this bit). However I need to write code to check that the user is entering a valid index number, and if they don't, I need to prompt them that the number they have entered is incorrect and let them retry, and I don't know how to.
This is my full code, I have added a comment underneath where I need to add the code:
import java.util.Arrays;
import java.util.Scanner;
public class StudentNames {
public static void main(String[] args) {
String[] names = new String[8];
Scanner s = new Scanner(System.in);
for (int i = 0; i < 8; i++) {
[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
Mar 8, 2015
I am having an issue with a parallel array that I created that returns array values based on an input. The code only works for the first four array locations just fine. The remaining values are not found. I thought it might be a memory allocation issue with the array size, I tried to dimension the array, however, using the Eclipse editor get errors.
import java.util.*;
import javax.swing.JOptionPane;
public class StudentIDArray {
static String[] studentNum = new String[]
{"1234", "2345", "3456", "4567", "5678", "6789", "7890", "8901", "9012", "0123"};
static String[] studentName = new String[]
[Code] .....
View Replies
View Related
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
Nov 20, 2014
im trying to make a small program where the user enters their selection choice and it prints out the corresponding information. for example, if they choose "3", i want "3. Soup de Jour" to print out
import java.util.Scanner;
public class rest
{
public static void main(String[] args)
[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
Sep 13, 2014
I'm working a an assignment for my IT/215 class and I'm having a little trouble with my arrays. My code compiles just fine but when it comes to displaying the information I've input into my array it doesn't seem to want to and I'm not sure what I'm doing wrong here. On line 136 originally I had [DVDs [count] = count+1]] but it wouldn't compile. I then put line 136 to [ DVDs[count] = new Inventory();] and I think that's were my issue is but I'm not sure....
// Purpose:the purpose of this software is to display inputs as wells as the stocks and price of inputs.
class Inventory
{
private double ItemNumber; //to store Item number
private String ItemName; //to store Item name
private double UnitsInStock; //to store number of units in stock
private double UnitPrice; //to store Unit price
private double InventoryValue; //to store Inventory value
private double TotalInventory; //to store Total Inventory value
[Code] .....
View Replies
View Related
Apr 5, 2014
I have a college question ask me to write a class StringRevert which does the following:
-prompting user for an interger n
-creating an array of n string
-repeatedly read character string from user input and store them in the array until end of array is reached or user input -quit(quit should not saved in array)
-print the string from array in reverse order, from last enter to first enter.
-assume user always supplie correct input
This is what I've done so far but how to get it working.
import java.util.Scanner;
public class StringRevert {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("enter value of n: ");
int n1 = in.nextInt();
String[] n = new String[n1];
[Code] ....
View Replies
View Related
Apr 22, 2015
a. Write a Java program to input 10 integer numbers into an array named fmax and determine the maximum value entered. Your program should contain only one loop, and the maximum should be determined as array element values are being input. (Hint: Set the maximum equal to the first array element, which should be input before the loop used to input the remaining array values.)
b. Repeat 1a, keeping track of both the maximum element in the array and the index number for the maximum. After displaying the numbers, display these two messages:
The maximum value is: _________
This is element number __________ in the list of numbers
Have your program display the correct values in place of the underlines in the messages.
c. Repeat 1b, but have your program locate the minimum value of the data entered.
I did parts a and b but for part see i just want to know if i did it correctly or not
import java.util.Scanner;
public class MinimumValueArray {
public static void main(String[] args) {
//Variable Declaration
Scanner keyboard = new Scanner(System.in);
int size = 10;
[Code] ,.....
When I run it i get this The minimum value is 0.0
The element that holds the value is 0 right away. is this right for the minimum or am i supposed to enter values and it will display the minimum value like in parts a and b wit the maximum? will the minimum just always be 0 or ?
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