Printing statements using a method [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Basically I've been set some work where I have to have 4 methods but I don't really understand how methods work. One of these methods basically has to print out two set statements that's it and I have no idea how to do it as most examples across the web deal with methods being used for maths and nothing as simple as printing out two lines for a title. Can anyone please help?

Method is just a way of extracting a piece of code and making it callable on demand. So if you print out two lines...
System.out.println("This is line 1");
System.out.println("This is line 2");
And you want to call it more than once, then you would extract it into a method.
private void myNewMethod()
{
System.out.println("This is line 1");
System.out.println("This is line 2");
}
Then call that method from anywhere in your program like so
myNewMethod();
These types of questions are off topic however as you need to demonstrate an understanding of your problem before we can help fix it.

Related

Is there an equivalent of Apex's 'system.debug' in Java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 10 months ago.
Improve this question
I recently started coding with Apex and have been utilizing the 'system.debug()' method. It helps me keep track of variables in my code by printing the variable in the 'debug log'. Now I am studying Java, and I was wondering if an equivalent method exists in Java. I would like to see what my variables are in certain parts of my code.
You can use a PrintStream, like System.out to call print or println in order to display your debug. You can also redirect System.out to file using setOut.
However, if you use an IDE and a debugger, you can create a breakpoint to the line that you wish to debug and let a debugger run until it reaches your line.

Trying to System.out.print() in a setOnMouseClicked event [Java app] [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm writing a Java application that calls a method letsPlay. I want to see if the code works, and sout'ed to test if it's running or not. When I run the app, nothing is printed. Is it because Java doesn't sout anything, or is it a problem with my code?
private void letsPlay(boolean player1turn) {
if(player1playing && player1turn)
System.out.println("It is " + nameField1.getText() + "'s turn.");
}
One of the two:
(probably) player1playing or/and player1turn are false, causing the expression to be evaluated to false, so the statement won't be executed
output stream is directed to somewhere else and not to the console, to solve this you'll need to do something like:
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
The easiest way to find what's your problem is to debug your code. Use the debugger, it'll save for you time, much of it.

Value of the String [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have just come across a string variable in my application code where the value is assigned as follows
String YEAR="${year}";
What does the value of the String variable here?
Without being interpolated, just ${year}.
I can almost guarantee that String is going to be interpolated elsewhere in the code.
Maven, for example, uses this syntax to inject variable values into it's configuration settings.
Use an IDE such as IntelliJ and do a find usage on this variable. An extended find (search/find in path) may also yield results.
As others here have suggested, It is most likely being interpolated somewhere else.

is there any way to read from text file word by word (i.e without using split)? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
is there any way to read from text file word by word (i.e without using split)? . I am able to read line by line directly with nextLine() with a scanner object, but I got error when I tried with next(). Thanks in advance.

In Work Calculator [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to do a calculator which just i need a multiplication . I will gave a example. Imagine 2 jTextField. I need to get data which is wrote to these jtextfields, and multiplicate them. And show result and jTextField3. This work needs the work under Action Performend jButton. It can be easy for you guys but i didnt understand. Thanks for reading..
num1=Integer.parseInt(txtfield1.getText());
num2=Integer.parseInt(txtfield2.getText());
total=num1*num2;
totalField.setText(String.valueOf(total));
Note:I tried this code but it didnt work. But i need just like that.
I just need a new code entire code about what did i said on the question . Ok i'm nut giving order to you but , just please help me. Or what should i do ?
2 problems with the code that we can see:
NumberFormatException isn't caught
Ints are not declared
Use this instead:
try{
int num1=Integer.parseInt(txtfield1.getText());
int num2=Integer.parseInt(txtfield2.getText());
int total=num1*num2;
totalField.setText(Integer.toString(total));
} catch(NumberFormatException e) {
totalField.setText("Error in input");
}

Categories

Resources