string + operation error? [duplicate] - java

This question already has answers here:
After a string literal, all the + will be treated as string concatenation operator why? [duplicate]
(5 answers)
Closed 5 years ago.
So I made a simple program following the curriculum in Think Java. I was able to successfully make the code they asked for. But I encountered an error. When I add the operation at the end of the string, I get a different number than if I put the operation on its own line. Can anyone explain why? Where is that number in the last line of code coming from exactly?
Thank you, everyone!
public class Time{
public static void main(String args[]){
int hour=14;
int minute=22;
System.out.print("The number of seconds since midnight is: ");
System.out.println((hour*60*60)+(minute*60));
System.out.println("The number of seconds since midnight is: "+(hour*60*60)+(minute*60));
}}
**Thank you so much, Nongthonbam Tonthoi and everyone else (and those who directed me to the duplicate question). I probably wouldn't have found the answer because I didn't even know that was the same issue. Now I do though; I learned a lot from combining these responses. I really appreciate it.

Change this:
System.out.println("The number of seconds since midnight is: "+(hour*60*60)+(minute*60));
to:
System.out.println("The number of seconds since midnight is: "+ ((hour*60*60)+(minute*60)));
Add extra brackets.

Related

puzzle 65-java puzzlers. int overflow can occur in calculations involving subtractions of negative numbers, int.Max_Value, etc How do you avoid it? [duplicate]

This question already has answers here:
How can I check if multiplying two numbers in Java will cause an overflow?
(15 answers)
Closed 1 year ago.
int ans = Integer.MAX_VALUE -(-1); //should I explicitly cast my method parameters in calculation to a wider bit type ?
Found a solution on searching the internet. An article that may help learners like me.
https://www.drdobbs.com/jvm/signalling-integer-overflows-in-java/210500001

Print current line number in intellij [duplicate]

This question already has answers here:
How can we print line numbers to the log in java
(22 answers)
Closed 3 years ago.
I am trying to print the current line number(line number of ide) of a certain code from intellij. How do I do that?
1 System.out.print(getCurrentLineNo());//sample code
2 System.out.print(getCurrentLineNo());//sample code
3 System.out.print(getCurrentLineNo());//sample code
4 System.out.print(getCurrentLineNo());//sample code
One way to do this is to get the stack trace element and call getLineNumber.
public static int getCurrentLineNumber() {
return Thread.currentThread().getStackTrace()[2].getLineNumber();
}
Note that I used an index of 2 because that refers to the frame of the method that calls getCurrentLineNumber. If you are just doing this inline, i.e. like this:
System.out.println(Thread.currentThread().getStackTrace()[1].getLineNumber());
You should use an index of 1 instead.
Note that an index of 0 refers to the frame of the getStackTrace method.
I suppose it too expensive to get StatckTrace for that, but it can help you for some debug.
System.out.println(new Exception().getStackTrace()[0].getLineNumber());
Also, you can look here
You can get line number like this...
public static int getCurrentLineNo() {
return Thread.currentThread().getStackTrace()[2].getLineNumber();
}

How to see if first half of string contains a specific word [duplicate]

This question already has answers here:
How to check whether a string contains a substring in JavaScript?
(3 answers)
Closed 7 years ago.
I'm a high school student beginning in java and I don't know how to print something if the first of half of a string contains the word "the" and how to print something different if its in the second half of the string and if its not in the string something different as well. here is my code so far:
String sentnc = (JOptionPane.showInputDialog("What is the message?"));
int a = sentnc.length();
if (sentnc.contains("the"))
{
txa1.setText("the enemy is attacking tonight");
}
This is in a button action performed tab.
Any help would be appreciated.
To search only the first half of the String :
if (sentnc.substring(0,a/2).contains("the"))
To search only the second half :
if (sentnc.substring(a/2).contains("the"))

Subroutine dice rolling assignment [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 just learning subroutines and I'm still very, very amateurish at this. My assignment asks me to toss two dice, display what the dice display (random numbers), display total of the two die, and let the program continue until 7 or 11 is the total. We haven't done anything on arrays, so I don't know if I should use them or not.
Also, I was going to use a for loop, but how would I get the program to stop at either 7 or 11 using it? Should I try another loop?
Please help guide me on what I have to do! I'm very confused about how to make the methods and put them into the main method. Just an explanation would do!
Thank you!
Here's the pseudocode for this problem. I won't give you the actual code but this will set you in the right direction:
Loop forever: {
Integer A = Random #1-6
Integer B = Random #1-6
Integer total = A + B
If total == 7 or total == 11 BREAK from the loop
Print total
}
Some more hints: Looping forever is usually achieved by setting up a while loop whose condition is always true. For example, while (true) or while (1 == 1).
Also, look into the java.util.Random class for generating a random number. It's really straightforward and it's good to start learning how to use the Java docs early in your learning process.
I would suggest a while loop. In your case,
instantiate the two dice
while the total isn't either 7 or 11 {
roll again
}
print out the result of the roll that wasn't either 7 or 11.
should be a reasonable starting point for you. I won't go into too much detail so that you still get the chance to implement the idea yourself and learn from it. If you don't know how to simulate a dice roll, #Kon's answer is helpful.
You may write a subroutine that returns the total number and call it in main method in while-loop
class Rolling-dice {
public static int roll() {
// roll the first dice and display the number
// roll the second dice and display the number
// return total`number
}
public static void main(String[] args) {
int result = roll();
// while result not 7 or 11 call roll()
}
}

How to convert an amount of different currency into words in java? [duplicate]

This question already has answers here:
How to convert number to words in java
(31 answers)
Closed 9 years ago.
I have an amount with different currencies, I want to convert an amount into words.suppose if i have a number 1000000, i have to print it for "Rupees ten lakh only" for india, "Dollar one million only" for usa etc...
Any suggestions would be great.
Thanks in advance,
Msn
I'm going to take a stab in the dark and assume you're talking about String and int data types.
To append the value of the int type onto the String use the String's method valueOf, for example:
String formattednumber = "$" + String.valueOf(amount);
In the future please provide more information to exactly you want and what you have tried, also code would be helpful.
If you need to print numbers as words then this might be helpful.
same logic can be used to append currency name.
hope that helps.

Categories

Resources