I trying to display the value of a variable from a method, but I don't know why it will I can't do it.
//Java extension package
import javax.swing.JOptionPane;
public class Palindrome
{
public static void main(String[] args) {
int number = 0;
int palindrome = 0;
I have created an enumerated data type that represents months. In my program I have asked the user for the month. I hold the month entered by user into a string variable. From that string variable I'm trying to display the month using my enumerated data type. I know I can use if statements, but is there another simple way to do it?
import java.util.Scanner; public class demo { //Enum class called month that has constants in it representing months enum month{january,february,march,april,may,june, july,august,september, october,november, december};
The term "Local variable" is related to scope. That is a local variable is one which is defined in a certain block of code, and its scope is confined inside that block of code.And a "Member variable" is simple an instance variable.
I read in a discussion forum that when local variables are declared (example code below), their name reservation takes place in memory but they are not automatically initialized to anything. On the other hand, when member variables are declared, they are automatically initialized to null by default.
Java Code: public void myFunction () { int [] myInt; // A local, member variable (because "static" keyword is not there) declared } mh_sh_highlight_all('java');
So it seems that they are comparing local variables and member variables. While I think a member variable can also be be local in a block of code, isn't it?
when we create another variable and set it equal to the first : Car c2 = c1;
we're pointing c2 at the same car object that c1 points to (as opposed to pointing c2 at c1, which in turn points at the car). So if we have code like,
Car c1 = new Car(); Car[] cA = {c1, c1, c1, c1};
are we doing the same? Are we creating four *new* reference variables, each of which points at the same car (again as opposed to pointing them at c1 itself)? I think so, but want to make sure I'm understanding this correctly.
I have a JFrame jf and JPanel jp on it. jp has five TextFields named cel1, cel2.. cel5. I wish to construct a String Cel + for loop index and run a for loop to reset the values of all the text fields using a single statement such as cel1.SetText("abc"). Similar things can be done in foxfro. How does one do it in java?
Write a class named FileDisplay with the following methods:
1.) constructor: the class's constructor should take the name of a fil as an arugment. 2.) displayHead: This method should display only the first five lines of the file's contents
Here is the following code I have made so far
import java.io.*; public class FileDisplay { private String filename; public FileDisplay(String Filename) throws IOException
[Code] ....
First, in my constructor I have taken in an argument and used that argument to open up an output file. Meanwhile, I'm trying to work n the displayhead method to print out information and to read data to. I haven't opened up my input file yet, but I'm not understand how can I read a print data to an output file. in
public void displayHead() {FileWriter file=new FileWriter(Filename)}
do I make create another instance of the filewriter class to output data?
In simple words, suppose to I want to display some messages on my output file in the displayhead function. Since I already have opened up the file in the constructor, how do I combine that in this method...
I am developing a product where the client needs to display the content of XML in a JSP page. This XML will be extracted from database and will be temporarily sotred in a String object and will then be flushed to JSP page for display. This was working fine unitl now. But now the customer wants display each tag with one color. attribute in another color and data in another color.
For ex: <?xml version="1.0" encoding="UTF-8"?>
this should be displayed in one color. Then
<ichicsrmessageheader>Data</ichicsrmessageheader>
In the above one the tag should be displayed in one color and "Data" should be displayed in another color
In the above sample tag should be in one color and attribute should be in one color ex: red and value of the attribute should be in another color ex: blue and "Data" should be in one color ex: green.
Here is the sample screen shot of how xml should is currently being displayed.
How do I use a variable in an if statement to return a public void method from another class? I've tried:
System.out.print("select 1 - 6: "); Scanner myScanner = new Scanner(System.in); myScanner.nextInt(); if (myScanner.nextInt()==1) { carSelection colour = new carSelection(); colour.carSelection();
The method colour in class car selection is just 4 print line statements? I've // out the if statement to see if it is correctly calling the method and displaying the data and it is?
Regarding the code examples in Head First Java, this is from Chapter 5, regarding the beginning creation of the dot com game. There are two classes in quesiton
the first is the SimpleDotComTester class: public class SimpleDotComTester { public static void main(String[] args) { SimpleDotCom dot = new SimpleDotCom(); int[] locations = {2, 3, 4}; dot.setLocationCells(locations); String userGuess = "2"; String result = dot.checkYourself(userGuess); } }
and the second one is the code for the checkYourself () method in the SimpleDotCom class
public class SimpleDotCom { int[] locationCells; int numOfHits = 0;
public void setLocationCells(int[] locs)
[code]....
Now I noticed that both classes use a variable called result; the program runs fine, but assume from the example that you can use the same variable name two different classes;
i have a datatable with id= tblId#{emp.id} <c:set var="tableID" value="tblId#{emp.id}"> displaying employee details inside c:forEach closing dataTable.
[code]...
For ex.Displaying 5 table dynamically.Need to validate check box selected or not.5 table are displayed.But it shows 5th table row id only.It is because of local varible declaration?
I'm making a program where the user enters an ID number and can submit the amount of sales that person made for a specific product and write to a file. One of the things I have to do is making a running sum of the sales for each product for all people combined as well as an accumulation of the a persons sales every time he enters data. Everything seems to be working execpt my values like person1Total, person2Total...etx and product2Total.
When I try to write them to the file, they always come out at 0.0 even though they should be the sum of the indexes of the array sales.
I'm working on a program where the user inputs song information (name, artist, filesize, duration) which is stored in a database. Once the user adds a song, the details should be stored in the first empty song slot (4 song slots total) and then taken back to the menu interface.
What I'm having trouble with is that I can add the first song fine (song1), but when I try to add another song, the information entered into song1 is gone. I've worked out that this is because every time I call upon the addNewSong() method, I'm creating four Song objects that are brand new with the line:
Song song1 = new Song(); Song song2 = new Song(); Song song3 = new Song(); Song song4 = new Song();
How to fix this problem is where I'm stuck because if I move these lines elsewhere, I get a 'cannot find symbol - variable song1' error.
Here is my code:
public class SongDatabase { Scanner console = new Scanner(System.in); public void addNewSong() { Song song1 = new Song(); Song song2 = new Song(); Song song3 = new Song(); Song song4 = new Song(); if (song1.isEmpty()) {
I have code that displays 3 game scores the series total and average. For some reason game 3 outputs 0 and game 1 and 2 output correctly. I don't see anything different that game 3 is doing that game 1 and game 2 aren't so I'm not sure what the problem is.
java file
package com.valenciaprogrammers.mybowlingscores; import java.text.DateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; public class BowlingScores implements Comparable<BowlingScores>{ private long id;
I have a problem with my code. When, on strausParser class, i create the new object Beam i give him an array of Node object called "nodiestremi".
The problem is that when a Beam is created, the Beam created before take the Node of new Beam. Why it appends?
public class Main { public static void main(String[] args) throws NumberFormatException, IOException{ SetFile.setupWindows(); Structure structure = new Structure(); } public class SetFile { }
My code has a method where the users input a bunch of variables and then those variables get added together in a new variable. What I want to know is how do I call the variable that is in the other method to another method?
import java.util.*; public class Calc{ public static void main (String [] args){ determinevalue1(); determinevalue2(); determineTotalvalue(double value1, double value2);
I have a class Tree in which all the methods to build a tree are in place. But however I would want variable of by Tree which is pointing to the last node being added to the tree.
So basically every time you keep adding a node to the tree you tail pointer gets updated to point to the last node. I have this so far.
public class NonEmptyTree implements Tree { private Tree left; private int data; private int leftleafCount; private int rightleafCount; private Tree right; private Tree tail; // This variable must be shared by all the object. There needs to just one tail pointer to the tree. public Tree insert( data ) { tail = // gets updated every time when new node gets added.
I am going through Thinking in Java, 4th Ed and I came across the section that talks about overloading variable arguments. I tried out a piece of code from the book. (Method and class names not exactly the same).
public class Varargs { public static void m1(Character... args) { System.out.println("Character");
[code]....
In the above code, the compiler throws an 'Ambiguous for the type varargs' error. The error goes away if the first method is changed to:
public static void m1(char c, Character... args)
why there is ambiguity in the first piece of code and not the second.
I am reading input from a file that has following information:
line 1 = numbers of integers in array, line 2 = elements in array1, line 3 = elements in array2.
These lines constitute a test case. There are 1000 test cases in the input file.
So basically, I read the length of arrays, populate the arrays by reading from the file.
The code is below ( I have not included reading input code):
while(test_case<1000){ if (count == 1){ //count keeps track of lines in input file vec_length = Integer.parseInt (tokenizer.nextToken()); count++; continue; } if (count == 2){ //populates array1 vector1 = new int[vec_length]; for (int i = 0; i < vector1.length; i++) vector1[i] = Integer.parseInt (tokenizer.nextToken()); count++; continue; }
Array2 is populated using the same as above code. However when I use the following code:
for (int i=0; i<vec_length; i++) temp += vector1[i]*vector2[i];
I get " local variable vector1 and vector2 have not been initialized error". But both arrays have been initialized in the if{} block. Is it because initialization was local to if block?
My objective is to write a program that calculates the bodyfat of people. The difficult thing is, that the calculations are different for males and females, so I tried to prompt the user to state whether they are male or female, then use and "if" statement to tether their response to the corresponding calculations.
Here's my algorithm:
Here's what I have:
package bodyweight; import java.util.Scanner; public class Weightcalc { static Scanner console = new Scanner(System.in); public static void main(String[] args){
[Code] ....
It keeps telling me that bodyweight is not initialized, but when I do, I get a hell of a lot more bugs on everything telling me they aren't initialized. I just want the program to transfer the user to select inputs, based on whether they are male or female.
I have BlueJ installed on my computer and it does the job of compiling the java source code written in it. If I want to write and compile source code outside of BlueJ do I still need to download the Java SDK and set the PATH variable, even though I am apparently able to do it in BlueJ?
I am developing a program that seems to need to transfer the value of a variable from one class to another. However, neither of the classes is the main class.
This simplified code should make my problem clear:
This is my main class where I create runone from Class1 and runtwo from Class2:
public class MainClass { public static void main(String[] args) { Class1 runone = new Class1(); Class2 runtwo = new Class2(); } }
This is Class1 where I set up x and give it the value 20. I also have a getter-method to be able to access the value of x from other classes.
public class Class1 { int x = 20; public int getX(){ return x; }}
This is Class2, where I try to access the value of x that was set up in runone.
public class Class2 { int y; public Class2() { y = runone.getX + 10; System.out.println(y); } }
Unfortunately, I get the error message "runone cannot be resolved to a variable."
What do I do to be able to access the variable x (that is set up in Class1) from Class2?