If Statement Requires A Return Of Type Boolean
Dec 6, 2014
Im trying to do this
if( 2/2 ) {
system.out.println( "number is not event" );
} else {
system.out.println( "number is event" );
}
However if statement requires a return of type boolean. So im forced to this instead
if( (2/2) != 0 ) {
system.out.println( "number is not event" );
} else {
system.out.println( "number is event" );
}
Is there a way to achieve the former method, without typecasting, if you had to typecast how do you do it?
View Replies
ADVERTISEMENT
Sep 7, 2014
I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is strong return a, if it is notstrong return b. How would you write that in a Code?
I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.
mlong should return an int depending on the X.moth. at the moment my code looks like this:
// File1:
public class date {
public int day;
public int month;
public int year;
}
// File 2:
public class monthlength {
public int mlong(date X) {
int t;
t = X.month;
if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12)
{ return 31; }
if(t == 4 || t == 6 || t == 9 || t == 11)
{return 30;}
}
}
View Replies
View Related
Apr 2, 2014
How do i take input values for TwoDPoint (which are objects) and return it back in numerical values also print them.
When i create an object in main method and pass values to my function of return type TwoDPoint,it gives error:- found int,int need TwoDPoiint,TwoDPoint.
// Here is what i tried to do:
Created class TwoDPoint that contains two fields x, y which are of type int. Defined another class TestTwoDPoint, where a main method is defined.In the main method created two TwoDPoint objects.
Then I modified the class TestTwoDPoint and add another function to it. This function takes two TwoDPoints as input and returns the TwoDPoint that is farthest from the point (0,0).
Then I added another function to TestTwoDPoint. This function takes two TwoDPoints as input and returns a new TwoDPoint whose x value is the sum of x values of the input TwoDPoint's and whose y value is the sum of the y values of the input TwoDPoint's.
class TwoDPoint {
int x = 2;
int y = 4;
}
class TestTwoDPoint {
public static void main(String args[]) {
TwoDPoint obj1 = new TwoDPoint();
System.out.println(obj1.x);
System.out.println(obj1.y);
[Code] ....
View Replies
View Related
Apr 14, 2015
code=Java
import java.util.Random;
import java.util.Scanner;
public final class Derp {
public static int WIN, Tick;
public static Scanner Input = new Scanner(System.in);
[code]...
why this boolean statement isn't working correctly. It's not detecting that the WIN and Tick are the same and instead chooses to always run the second statement.
View Replies
View Related
Mar 30, 2015
I wrote a class for encapsulating coins and I was to do a boolean statement but when I test the statement the results are not showing.Here is the code for my coin class coins.java
package project_3;
/**
*
* @author user a
*/
public class Coins {
private double pennies;
private double nickles ;
private double dimes ;
private double quarters ;
private int dollars ;
[code]...
View Replies
View Related
May 24, 2015
I want to write a program that ask if you want to go to the movies. If the user type in yes then it'll print out (Alright let go) but if the user type no then it would print (whatever). The trouble that I'm having is. What's the best way to use Boolean and Strings together in a if statement?
public class Night {
static Scanner UserInput = new Scanner (System.in);
public static void main (String [] args){
boolean user1, user2;
user1 = true;
user2 = false;
[Code] ....
View Replies
View Related
Sep 25, 2014
So I'm trying to check if the new coordinates vs original coordinates are diagonal and 1 line further, and if there is a piece there(getNum()) if it's 2 lines further, so I'm trying to return a boolean value then.
so if it's the first if, it returns true, if it's the 2nd it returns true, then I say else for all other scenario's, and return false there, but my compiler says my method is missing a return statement.
public boolean check(int[] d) {
int x,y;
x = loc[0][0];
y = loc[0][1];
int sx = d[0];
int sy = d[1];
[Code] .....
Edit: used a local boolean and returned that after my if's.,
View Replies
View Related
Mar 5, 2014
An assert statement checks a Boolean expression during run time execution. Is this true?
View Replies
View Related
Sep 25, 2014
While practicing java i have came across boolean data type. i have executed below program but i am not sure how i got output of the program like below:
10 > 9 is true
Here is my program:
class BoolTest3
{
public static void main(String args[])
{
System.out.println("10 > 9 is "+(10>9));
}
}
My question is: how this program will come to know that 10>9 true or false.. how bolean data type used in this program.
View Replies
View Related
Jun 13, 2013
I searched everywhere but i didn't found anything about the real size of primitive boolean type used by java.
View Replies
View Related
Jan 8, 2014
What is the difference between two statements:
Class1 class1 = new Class();
class1 = Class2.method1();
and
Class1 class1 = Class2.method1();
I have one more query on the same lines ... I always need to call the method1 of Class2 whenever i create a object of class1. So I wanted to go with the constructor in Class1. But the method1 in Class2 has a return statement. so is there any better way to do this other than constructors.
Sample code:
public int class Class2{
public static method1(){
return 2;
}
}
public class Class1{
public Class1(){
Class2.method1();
}
}
View Replies
View Related
Nov 20, 2014
This was an example of code that I'm trying to get to work, I swear I took it down just as it was written in class however mine says missing return statement....
Scanner keyb = new Scanner(System.in);
System.out.print("enter rows and columns");
int rows = keyb.nextInt();
int cols = keyb.nextInt();
int[] [] array = new int [rows] [cols];
printArray(array);
[code]....
View Replies
View Related
Apr 2, 2014
Im having trouble with my method return. I am new to using methods and cant seem to grasp the idea on the return part. I have to write a method that tells if a number is prime or not. This is what I have so far and it wont compile because it is saying "missing return statement } "..
import javax.swing.JOptionPane;
public class IsPrimeMethod
{
public static void main(String []args)
{
String primeNum;
int number;
int i = 2;
[code]....
View Replies
View Related
Jan 1, 2015
class Base
{
public Base rtc() {
System.out.println("am in parent");
return new Base();
}
}
class Ret extends Base
[Code] ....
Output :
I am in child class but b1.rtc requires b2 to hold the return value y not Ret's object.
View Replies
View Related
Apr 27, 2014
My task is to make a mortgage calculator where the user selects which calculation they want the program to do via a menu. I got the menu to work and it keeps on looping until terminated so that's good. The starts when I want the user's choice (P, I or T) to be used in another method which will then execute another set of code (the calculation that needs to be done). I think passing parameters and return statements are what I need to use, but after reading and watching videos, I'm still not sure how to implement it into my program. For now, I want the user to input the letter "P" and then I want that information to be passed to the method, loanCalculator() where the if statement will make a decision to call the method, calcPayment and display the number 0 via the console. Once it can do that, I'll fill in the calculation methods with the proper code since I can at least navigate the user input to its associated calculator. It just keeps on looping the menu without going through the other methods.
import java.util.Scanner;
//Example of "big loop" in main to repeat using a No Trip (0,N) test first
public class Mortgage {
// constants
static double loanAmount;
static double interestRate;
static int term;
[code]....
View Replies
View Related
Jul 2, 2014
consider this program :
public class hello {
/**
* @param args
*/
public static void main(String[] args) {
int s = new hello().h();
System.out.println(s);
} public int h(){
try{
int g = 10/0;
[Code] .....
the output is 7. how the flow is working. i understand that there is a divide by zero exception after which the control goes to catch. what about the return statement in catch . why is it overridden by finally..........
View Replies
View Related
Jan 22, 2015
I am trying to compile the arguments program but it is giving missing return statement error. how to correct the error message.
public class Arguments {
public static Void main ( String args[])
{
int count, i=0;
String sdata;
[code]...
View Replies
View Related
Feb 14, 2015
I am trying to understand the following code.This return statement should actually return the char at myArray[index] first, then increments the index afterwords correct?
Public char next(){
return myArray[index++];
}
View Replies
View Related
May 4, 2014
I get an Sqlexecption with this message "com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set."
When I execute this query :
...
Statement stmt = conn.createStatement();
stmt.executeUpdate("IF EXISTS (SELECT name FROM master.sys.databases WHERE name = N'Repository')
"
+ "PRINT 'Database exists'
[Code] .....
I want to create a database and a table in sql server if it doesn't exist. How can i prevent this error.
View Replies
View Related
Jan 25, 2015
I'm attempting to format my doubles to two decimal places within my return statement. I have tried to use DecimalFormat but it gives me an error because my method needs to return a double and that results in a string being returned. I have also tried using the *100.00/100.00 method and that doesn't work when the number already ends in 0.
If I pass -150.00 it gives me -150.0 when I need two decimal places.
How can I go about doing this?
View Replies
View Related
Feb 7, 2015
why Java does not support return type in method overloading. I coded following and it compiles and runs without any errors.
class Ideone
{
public static void main (String[] args) {
sum(3,5);
[code]....
If Java did not support overloading based on return type, this program should not work, right?
View Replies
View Related
Apr 7, 2014
I have been assigned to write a program that has a user input random numbers then the program is to sort them in different ways. I have the coding for the different sorts but, I have an error saying that I am missing a return statement in the "Bubble" method. I am trying to return "arr[i]" in the "for loop" which gives me this error, and when I try to take the "return arr[i]" outside of the "for()" loop the error reads the it cannot locate variable "i".
import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
public class SwingSorts extends JFrame implements ActionListener
{
JRadioButton bubble;
JRadioButton selection;
[Code] .....
View Replies
View Related
Jul 8, 2014
Write method distance to calculate the distance between two points (x1, y1) and (x2, y2). All numbers and return values should be of type double. Incorporate this method into an application that enables the user to enter the coordinates of the points.
Hints:
- The distance between two points can be calculated by taking the square root of
( x2 - x1 )2 + ( y2 - y1 )2
- Use Math class methods to compute the distance.
- Your output should appear as follows:
Type the end-of-file indicator to terminate
On UNIX/Linux/Mac OS X type <ctrl> d then press Enter
On Windows type <ctrl> z then press Enter
Or Enter X1: 1
Enter Y1: 1
Enter X2: 4
Enter Y2: 5
Distance is 5.000000
Type the end-of-file indicator to terminate
On UNIX/Linux/Mac OS X type <ctrl> d then press Enter
On Windows type <ctrl> z then press Enter
Or Enter X1: ^Z
View Replies
View Related
Oct 30, 2014
Alright, I don't understand how to link my compress method to my return statement method "getPText". Also in my compression I only want it to compress for 3 or more consecutive letters.
import java.util.Scanner;
class RunLengthCode {
String pText;
String cText;
void setPText(String PText) {
pText = "";
}
[Code]...
View Replies
View Related
Dec 25, 2014
I need to write a method that will consume string representation of Object type and will return one object of this type. How to set return type for the method in this case?
Here is exmaple :
public <?> identifyType(String typeString){
if (typesString.matches("String")){
return new String("");
}else if (typeString.matches("Integer")){
return new Integer(0);
}
//....etc..}
View Replies
View Related
Apr 20, 2014
I have attached a picture. I am struggling to understand one of the errors. It says it requires an int and a string. I have put a number in but I don't understand the string part!
View Replies
View Related