Printing 2 different variables in the same line Java VM [duplicate] - java

This question already has an answer here:
How to print both bool and double in same line? [duplicate]
(1 answer)
Closed 2 years ago.
I am new to java and i have 2 different Variables one is a double and one is a boolean. I am having difficulty printing them into the same line, this is my code.
class Main {
public static void main(String[] args) {
boolean isTrue;
isTrue = false;
double money;
money = 99999.99;
System.out.println(isTrue, double);
}
}
Thanks a lot if you answer

Many ways to do this and if you visited the javadocs you would see that there is also System.out.print which does not do a line feed, so you could use that as
System.out.print(isTrue);
System.out.println(money);
Or you could use System.out.printf, or convert values into a String first

There are few methods to do this, one is mentioned in the above answer and the other is below, that you can do this with a professional approach by using printf.
public static void main(String[] args) {
boolean isTrue;
isTrue = false;
double money;
money = 99999.99;
System.out.printf("%b%.2f%n", isTrue, money);
}

Related

cant find my main error, debugging at java [duplicate]

This question already has answers here:
Writing a function inside the main method - Java
(7 answers)
What is a classpath and how do I set it?
(10 answers)
Closed 2 years ago.
so I'm running a program in java and I can't really find the main error
this is my code:
public class Main
{
public static void main(String[] args) {
double myCheck = 50.00;
double yourCheck = 19.95;
double fiinalRATE = 0.15;
System.out.println("Tips are");
calcTip(myCheck);
calcTip(yourCheck);
public void calcTip(double bill);
{
tip = bill * fiinalRATE;
System.out.println("The tip should be at least " + tip);
}
}
and this is the error that I'm getting I think its the header but I don't really know what to put I'm kinda new at java though
You can't declare function inside function. You have to pull function out from main() to the Main.class
You cannot declare a method inside another method. So the compiler gets crazy :)
Just move you calcTip() function outside main() function (after closing curly bracket of main() or before declaration of main()).
public class Main
{
public static void main(String[] args) {
double myCheck = 50.00;
double yourCheck = 19.95;
double fiinalRATE = 0.15;
System.out.println("Tips are");
calcTip(myCheck);
calcTip(yourCheck);
}
public static void calcTip(double bill) {
// fiinalRate must be declared as parameter of calcTip()
// or as static field in Main class,
// otherwise the code doesn't compile.
double tip = bill * fiinalRATE;
System.out.println("The tip should be at least " + tip);
}
}

how to make an array function return a value [duplicate]

This question already has answers here:
How can I convert List<Integer> to int[] in Java? [duplicate]
(16 answers)
Closed 2 years ago.
So to summarize my problem, I want the function int[] get_marks() to be able to return the array, but I don't know how to get my ArrayList marks to convert into an array which I can use the return thing for. I need the arraylist to be converted because I need to be able to find the frequency and mode of the array (parts of the code I have not started because I can't figure out how to convert the arraylist).
Below is my comment less code so far.
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
class Main {
public static int[] get_marks(){
Scanner input = new Scanner(System.in);
ArrayList<Integer> marks = new ArrayList<>();
final int FLAG = -1;
int entries = 0;
System.out.println("Enter your marks. Type -1 when done.");
while (entries != FLAG){
entries = input.nextInt();
if (entries >=0 && entries <= 100){
marks.add(entries);
} else if (entries == FLAG) {
break;
} else {
System.out.println("Invalid entry. Marks must be between 0-100.");
}
}
input.close();
System.out.println("Your Marks are: " + marks);
}
public static void main(String[] args) {
get_marks();
System.out.println();
}
}
There's no reason not to simply return an ArrayList<Integer> or even better a List<Integer>. If you insist on returning an int[], your question is a duplicate of this.
Why not just return the ArrayList marks?
public static ArrayList<Integer> get_marks(){return marks}
Also, you could make the method return void. Is there any reason you need it to return a value in main?
To elaborate, if you make it void, it will just do all the calculations and culminate in System.out.println("Your Marks are: " + marks);. Given your code, is that not what you want? If so, make get_marks() return void:
public static void get_marks() {//run the code, print the marks, etc.}
Why do you need the get_marks() method? You may not have posted all your code, but if this is all your code, just get rid of get_marks() and put it all in main(). If all main() does is call get_marks(), consolidate the methods.

how to check if the variable is null? [duplicate]

This question already has answers here:
Check whether a String is not Null and not Empty
(35 answers)
Closed 5 years ago.
I know this doesnt work but how can I know if the user added even one char?
public class Program
import java.util.Scanner;
{
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
String b = a.nextLine();
//I know this doesnt work but how can I know if the user added even one char?
if (b!=null){
System.out.println(b);
}
}
}
you can use .equals("") or .isEmpty()
check check if the variable is null
You can do this
if (!b.isEmpty()) {
System.out.println(b);
}

"Return statement is missing" error in NetBeans 6.9.1 [duplicate]

This question already has answers here:
Compiling Error: Missing return statement [duplicate]
(8 answers)
Closed 6 years ago.
So as of recent I am testing out using methods that return a value, and I keep getting an error stating that I am missing a return statement in my method "basicPoints". I have a return statement, but I'm not sure why it keeps giving me this error, do I need to place the return statement in a different portion of the method?
public class Bridge {
private static int answer;
public static void main(String[] args) {
basicPoints(2, "clubs");
System.out.println("Points equal: " + ans);
}
public static int basicPoints(int level, String suit){
if (suit.equalsIgnoreCase("clubs")){
int ans;
ans = level * 20;
return ans;
}
}
}
Your issue is that when the method runs and let's say suit is "hearts", then the method won't return anything because it never crosses a return statement. Try placing a return statement after your if statement. For example, return -1 if you didn't find that suit and didn't test for it

Strings from input are never equal? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
import java.util.Scanner;
public class Michal {
public static void main(String[] args) {
reply();
}
public static void reply() {
Scanner input=new Scanner(System.in);
String name=input.nextLine();
if(name=="john"){
System.out.println("bear!");
}else if(name=="mary")
{
System.out.println("lovely lady!");
}else{
System.out.println("I don't know that person.");
}
System.out.println(name);
input.close();
}
}
I consider myself still a beginner to Java ,so please don't be harsh in your answers. I was trying to create a program that returns an answer every time it gets a certain input , however it seems to return "I don't know that person" all the time.
if(name=="john")
is not the right way to compare strings. Use equals() instead:
if(name.equals("john")){
System.out.println("bear!");
} else if(name.equals("mary")){

Categories