Value Assign To A String
Sep 25, 2014
I am doing an assignment where I have to find the price per square inch of a pizza, compare them and display the results. I have everything figured out with the values and stuff. Now when I have to displays the results I have to display which of the two pizzas is more favorable.
I have both values / square inch for both. and I know how to find the minimum value of the two wit the Math.min class.
My question is how can I assign the char, PIZZA A to the value that I had so I can display it in the output statement, without writing PIZZA A. It should display after the difference is calculated.
Here's my code so far.
// This programs finds the price per square inch of a pizza
import java.util.Scanner;
import java.text.DecimalFormat;
public class PizzaSquareInches {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#.###");
[Code] .....
View Replies
ADVERTISEMENT
Jan 29, 2014
Below is my first ever code. I bolded where i am getting a syntax error. Why am i getting an error here if i assigned that word to a string?
public class BeerBottleTest {
public static void main(String[] args) {
int beerNum = 99;
String bottle = "bottles";
while (beerNum > 0) {
if (beerNum == 1) {
bottle = "bottle"; }
[Code] ....
View Replies
View Related
Dec 15, 2014
I'm working with doubles I'm trying to figure out how to set this up so it works?
I'm just stuck on how to set and Assign a double FedTaxWitholding that gets the Fed Tax Withholding = Gross Pay * Fed
Tax Withholding Rate.
Do I need a string for Gross pay?
View Replies
View Related
Aug 18, 2014
At first I wanted to just use an array and set each day a value, however I was told that it has to be stored as a string.
Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day:
A. Set the day.
B. Print the day.
C. Return the day.
D. Return the next day.
E. Return the previous day.
F. Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add four days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.
G. Add the appropriate constructors.
H. Write the definitions of the methods to implement the operations for the class Day, as defined in A through G.
I. Write a program to test various operations on the class Day.
import java.util.*;
public class Day {
static Scanner readinput = new Scanner(System.in);
String day;
public Day(String day) {
[Code] ....
So right now if I run my code it allows me to type in a day, then it gives me the next and previous day, the last part is to add X days to it. Ideally I would like to be able to take the day entered, depending on the day set a numeric value, then add prompt for number of days you want to add. The program should then use modal (%7 ) to add value to the value that was given based on the day, and then translate it back into a String value (the day).
View Replies
View Related
Jun 21, 2014
I have the following code in which I am looping through the rows of one array (composed of Strings) and copying it to another array. I am using .clone() to achieve this and it seems work as it changes the memory location of the rows themselves. I did notice that the String objects are still pointing to the same location in memory in both arrays but I won't worry about that for now, at the moment I just want to understand why the array I am cloning is not successfully assigning to the other array.
This is the incorrect line: ar[r] = maze[r].clone();
My code:
private String[][] maze = {{"*","*","*"," ","*","*","*","*","*","*"},
{"*"," ", "*"," "," "," ","*"," ","*","*"},
{"*"," ","*","*","*"," ","*"," ","*","*"},
{"*"," "," "," "," "," "," "," "," ","*"},
{"*","*","*","*","*"," ","*","*","*","*"},
{"*","*","*","*","*"," ","*","*","*","*"}};
//private String[][] mazeCopy = copyMaze(new String[6][10]);
private <T> T[][] copyMaze(T[][] ar){
for (int r = 0; r < ar.length; r++){
ar[r] = maze[r].clone();
}
return ar;
}
My compiler says: Required: T[]. Found: java.lang.String[]
Since ar[r] is an array and .clone() also returns an array why is this line incorrect.
View Replies
View Related
Apr 25, 2015
simple assignment of values to a previously initialized object?
See the method useModel ()
The idea is, assign the values to the temporary object, data
Then plunk it into this statement:
model.addRow ( data );
Simple enough?
I've been putzing with the syntax for multiple hours, over days, now.
With and without
[0];,
Netbeans keeps giving me: Illegal start of expression data is declared as an array of Object, although, in this case, it does not need to be an array. What is the correct syntax?
Java Code: //--
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;
import javax.swing.JFrame;
[Code]....
View Replies
View Related
Apr 27, 2015
This code currently just prints out the memory for the objects. What I am doing that it obviously wrong?
import java.util.ArrayList;
import java.util.List;
public class deck extends Card {
public final String[] SUITS = { "Heart", "Diamond", "Clubs", "Spade" };
ArrayList<Card> deck = new ArrayList<Card>();
[Code] .....
View Replies
View Related
Jul 12, 2014
I've been referencing my text and a few other sites to assist in building this class...and I'm still uncertain of the purpose of a few of the methds: next(), hasNext().
Also, I have not found a clear explanation of the following code example: tail.next = tail; There are several instances of this in the code below....I'm just not sure exactly how this assigns the value to the next object in the other class..?? ??
public class MySinglyLinkedList<T> implements SinglyLinkedList<T>{
protected NodeList<T> head, tail, current, newNode;
String name;
int size = 0;
public MySinglyLinkedList(){
head = null;
tail = null;
[Code]...
View Replies
View Related
Feb 24, 2015
//Output is:ERROR.. one of the books says, we can assign float value to int variable, but its giving error.
class floint
{
public static void main(String arg[])
{
int i;
float j=10.12f;
i=j;
System.out.println(i);
}
}
View Replies
View Related
Apr 25, 2014
Can I assign multiple values to one variable? For example I want myNum = 0 thru 9 you see im trying to program a password checker program to verify that the password meets all the criteria 8 char long, 1 upper case, 1 lower case, 1 numeric, 1 special, and don't contain and or end
View Replies
View Related
Feb 3, 2015
I'm focusing on the player1Turn() method. When I run this and say temp = A, it says pit = 0. I don't know why this is.
import java.util.*;
public class Mancala {
Scanner input = new Scanner(System.in);
public static void main(String[]args) {
[Code] .....
View Replies
View Related
Sep 25, 2014
Im new to jsp. I have the following html in my jsp
<select name="job" id="job">
<option value="Booked">Booked</option>
<option value="Assigned">Assigned</option>
<option value="In Process">In Process</option>
<option value="Completed">Completed</option>
</select>
How do I assign job in the scriplet like how we do <% String job = request.getParameter("job")%> but on the same page? <% String job = request. getParameter("job")%> returns null value.
View Replies
View Related
May 23, 2014
Let's say I have a loop that loops through objects in an ArrayList then does stuff with them. Is it better for me to store the object in a temporary local variable and do stuff with it, or always use the ".get(arrayindex)" thing?
View Replies
View Related
Apr 13, 2014
i am trying to assign unique values to nodes read from XML file.. eg: consider this XML file:
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description> ...Strong Belgian waffles...</description>
<calories>650</calories>
[code]....
now assigning these nodes "a unique value" has to be done following the LSDX labelling pattern i.e:
To the document element we first give an “a”.As there is no parent node for the document element, we assign “0” at the front of that “a” . “0a” is the unique code for the document element (breakfast_menu). For the children nodes of “0a”, we continue with the next level of the XML tree which is “1” then the code of its parent node which is “a” and a concatenation “.” . We then add a letter “b” for the first child, letter “c” for the second child, “d” for the third child and so on.Unique codes for children nodes of “0a” shall be “1a.b”, “1a.c”, “1a.d”, etc.Hence foe the above given XML the mapping would look something like this:
0a breakfast_menu
1a.b food
2ab.b name
2ab.c price
2ab.d description
2ab.e calories
2ab.f chef
3abf.b chef1
3abf.c chef2
1a.c food
2ac.b name
2ac.c price
2ac.d description
2ac.e calories
2ac.f chef
3acf.b chef1
3acf.c chef2
For more samples about LSDX labelling : 1.) Section 3.1 LSDX Labelling on this link: [URL]
2.) Fig 3 on page 1189 on this link:[URL]
right now i am using SAX parser to read xml and get the nodes in their hierarchical order..now the problem is that i have to assign these specific value to their respective nodes using java.
View Replies
View Related
Feb 7, 2015
I am trying to create an empty array that has no assigned length as the amount of elements it needs to hold will be dependent on another value. I then want to use a while loop to assign values to it. Here is an example of what im looking for it doesnt work. Iam trying to do:
int x = 12;
int i = 1;
int k = 0;
int[] factors = {}
while (i<x) {
if (x%i==0) {
factors[k] = i;
k++;
i++;
View Replies
View Related
May 15, 2014
I am trying to assign random numbers to a deck of cards without repeating the numbers. What am I doing wrong?
package hokm;
import java.util.Random;
import java.util.Scanner;
public class Hokm
{
public static void main(String[] args) {
int [][] number=new int[52][2];
[Code] .....
View Replies
View Related
Oct 10, 2014
Write a program that reads student scores, gets the best score and then assigns grades based on the following scheme:
Grade is A if score is >= best - 10;
Grade is B is score is >= best - 20;
Grades is C if score is >= best - 30;
Grade is D if score is >= best - 40;
Grde is F other wise;
The program prompts the user to enter the total number of studeents, then prompts the user to enter all of the scores, and concludes by displaying the grades.
import java.util.*;
public class AssigningGrades
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);
int studentNumber = 0;
int classScore = 0;
[Code] ....
So when I ran into problems when populating the array and I made changes. Then all of a sudden the program doesn't recognize classSize[i] at the System.out.print line.
View Replies
View Related
Apr 3, 2015
I'm trying to build a monopoly like game, and atm I'm trying to find way how to build the Community and Chance chest cards. so far, my logic is
1-create an ArrayList of cards with a given order
2-for a given number of times(for loop) generate 2 random numbers ,which will be the parameters for Collection.swap().
3-swap.
here's the code for the shuffler Button
shuffler.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0;i<shuffeledMessages.length;i++){
int randoma=(int)(Math.random()*4);
int randomb=(int)(Math.random()*4);
Collections.swap(myMessages,randoma,randomb);
}
}
});
For now things seem to work pretty ok, but I'm wondering if this is a good and efficient way to shuffle a card chest especially in case of large number of cards. plus, I'm not sure what would be a good loop count for effective shuffling, in my case I used i<arraylist.size
View Replies
View Related
Apr 4, 2015
How would I randomly assign colors and a with a name? Some functionality may require command-line arguments but I am a bit confused on how to structure this. Also, these names must appear on a color coded on a gui
View Replies
View Related
Jul 4, 2014
I need the user to be able to input a number, and for the program to assign this value to an 'int' variable. I know how to do this with a 'string' variable:
Java Code:
String options = JOptionPane.showInputDialog(null, "In your decision, how many options do you have?
" +" (NOTE: The maximum number of options = 5, and you must enter your answer as a numeral.)"); mh_sh_highlight_all('java');
But I need to know how to do this with an 'int' variable.
View Replies
View Related
Jul 26, 2014
I have the following code:
public class CollisionManager<T> {
private boolean collision = false;
private T mainEntity;
public <T extends Entities> void handleCollision(T mainEntity, T secondEntity){
this.mainEntity = mainEntity; // This is illegal.
}
}
Why "this.mainEntity = mainEntity" is incorrect and also show me the correct way to achieve this?
The error I am getting is "Type mismatch: cannot convert T to T"
View Replies
View Related
Apr 16, 2015
My biggest issues are as follows:
1) I'm trying to use a logarithm to determine the length of a user input number. I keep getting an error stating <> indetifier expected. I'm assuming this means that the program is not recognizing the function of a logarithm. I know that normally you can include that information in the method, but my teacher has stated specifically that each of these methods be called something else, as shown in the code.
2) I'm not quite sure I understand how to assign the numbers I obtain from the modular equation to a certain position in the array. As I'm asking the user to input any number these values can change so therefore I can't simply state that first number = this place.Here is my code:
import javax.swing.*;
import javax.*;
public class getSize
{
public static void main( String[] args )
[code]...
View Replies
View Related
Apr 16, 2014
Started learning about Array's I'm doing an exercise where you create a for loop that randomly assigns values to each element within the array, but where is my code going wrong?
import java.util.Scanner;
public class ArrayExamples{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double exampleArray[] = new double[5];
System.out.print("Enter a Number: ");
int num1 = input.nextInt();
[Code] .....
View Replies
View Related
Dec 16, 2014
I am writing a program for a game. It is between the user and a virtual player. The game starts with a pool of consecutive integers 1-100. The size of the pool is based on a random generated number at the beginning of the game. At the start, both players' scores are 0. For each turn, the player picks one number from the pool. That value is added to the player's score, the computer gets the sum of all the remaining numbers in the pool that divide evenly into the player's pick. The player's pick and its divisors are then removed from the pool.
The player should be able to play the game as many times as she wants without ending the program. Instructions should appear on the screen only once at the start of the program.
For each turn, both players' current score, the current pool of numbers, and a prompt for a number to be entered should show onscreen. I have written the code until I get to the function that updates the pool of numbers after a turn.
import java.util.Scanner;
import java.util.Random;
public class SlickPick {
public static void main (String[] args){
Scanner read = new Scanner(System.in);
int []pool = new int[100];
[Code] ....
My thinking is that I need to use the binary search to find the indexes of the divisors array and then use those indexes as the start values. I'm not sure how to assign the divisors indexes to start. Do I need an array for start? Whenever I run the program, the only value missing is 3.
//Name : poolUpdate
//Description : This function modifies the contents of the pool as a result of a turn of play.
//Parameters : The pool array, the divisors array, the size, and the user's pick.
// :
//Return :
public static void poolUpdate(int[] pool, int[] divisors, int size, int pick){
int low=0;
int high=size-1;
[Code] ....
View Replies
View Related
Mar 21, 2015
By using FileReader, FileWriter and their constituents, I am creating a file to contain employee information ( name, age, hours, etc. ). The user is to input all of the data on a single line and we were asked to implement StringTokenizer to assign that data to the file. I have never used the StringTokenizer before, but I have a rough idea how it is to function. I used pw.println to test what I have so far, now I would like to let the user build the initial file with the "first employees" of the company, and then view the file, and then go back and append new employee data to that same file. My question is, how can I take the user input as a StringTokenizer and add that to the file?
In the for loop below, I thought I would see if it would work, but it does not. The loop only executes once and does not allow me to enter data.
public class Records {
public static void main(String [] args) throws IOException {
Scanner input = new Scanner(System.in);
FileWriter fw = new FileWriter("dbs3.java");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
System.out.println("NEW EMPLOYEE DATA SHEET");
System.out.print("Number of new employees: ");
int number = input.nextInt();
[Code] ....
View Replies
View Related
Dec 30, 2014
I receive a java.lang.NumberFormatException: For input string: ""DepDelayMinutes"" error when trying to filter a list and then assign the results to a new list.
I have edited the code so it is an int that throws the exception so it isn't the presence of a string that is causing the error - java.lang.NumberFormatException: For input string: ""0914"" .
I believe the issue is because of the two sets of double quotes but I do not understand how they came about. The original dataset does not have any quotes whatsoever.
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class FilterObjects extends Thread{
[Code]...
View Replies
View Related