Method Return Statement

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


ADVERTISEMENT

How To Link Compress Method To Return Statement Method GetPText

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

Return Statement In Method Overriding

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

Method Must Return Int Type - If Given Integer Is Strong Return A / If Not Return B

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

How To Return Values In Object Return Type Function (method)

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

Return Statement In A Constructor

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

Missing Return Statement

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

Passing Parameters And Return Statement

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

How Does Return Statement Work In Try Catch Block

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

Argument Program - Missing Return Statement

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

Incrementing Array Index During Return Statement

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

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 View Related

JDBC SQLException - Statement Did Not Return Result Set

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

Formatting Doubles To Two Decimal Places Within Return Statement

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

User Input Random Numbers Then Program Sort Them In Different Ways - Missing Return Statement

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

How To Return Array From A Method / Back Into Main Method That Prints Out Stuff

May 27, 2014

I'd like to know how to return a new array, I wrote in a method below the main method. I want to print the array but system.out.print doesn't work for arrays apparently. What structure i should use?

View Replies View Related

Set Method Return Type Determined Inside Method

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

OOP A Method Being A Return Type In A Method?

May 21, 2015

I stumbled upon a method that im not sure how to handle (yet).Below the code:

public DateTime() {
this.day = day(milliseconds);
this.month = month(milliseconds);
this.year = year(milliseconds);
this.hour = hour(milliseconds);
this.minute = minute(milliseconds);

[code]...

The two last methods stumped me. The return type to each is "DateTime", according to JUnit complaints.I know that I can use the "this" keyword to reference to the object. But how do I get these two methods to return the correct result?

View Replies View Related

Method Return 0 Value

May 1, 2015

I'm trying to calculate a certain value, but it is returning 0 and I'm not sure why. This is a measurement to define the angle from the equator and the center of the sun

public static double declination(int dayOfYear) {
double declination = asin((sin(23.45)*sin((360/365)*(dayOfYear - 81))));
System.out.println("dec " + declination);
return declination;
}

View Replies View Related

Return Outside Method

Jul 27, 2014

Here is the code:

import java.util.Scanner;
public class beersong
{
public static void main (String[] args)
{
int bottle;
int bottlenumber;
int bottlezero;

[Code] ....

And here is the problem:

return "nine";
^
error: return outside of methood

what do i do?

View Replies View Related

Can Call A Method In If Else Statement?

Dec 6, 2014

I am trying to get the program to ask the user to enter the project grade and if it is less than 65, then I want the program to display "fail" or if it is greater than 64 than I want it to display "passed". In this project I have to include a method and I am trying to call it in my if-else statement. I keep getting an error message saying "Project.java:143: error: incompatible types: void cannot be converted to int". I posted my code below. It's a long program, but this part is toward the bottom.
 
import java.util.Scanner;
public class Project {
public static void main(String[] args) {
  //call method
welcomeMessage();
 
[Code] ....

View Replies View Related

Average Return Method

Dec 7, 2014

For my project, I am trying to return the method average so the program can calculate the average after grades are enter. I'm not sure what I am doing wrong and how I should improve it. My code is below. This is my error message: "Project.java:71: error: '.class' expected".
 
import java.util.Scanner;
public class Project
{
public static void main(String[] args)

[code]....

View Replies View Related

Why Cannot Get Append Method To Return Itself

Mar 11, 2015

This is my class code below and the Junit test that is being run.

My problem is that when I try to make an append to my list I have to return the actual list but I cant figure out where I am going wrong.

Java Code:

package structures;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ListImplementation<T> implements ListInterface<T> {
private Node<T> listHead, listTail;
private int size;

[Code] .....

View Replies View Related

Return Method Involving Arrays

Mar 2, 2015

public class School {
public final int MIN_GRADE = 0;
public final int MAX_GRADE = 100;
private int [] grades;
public School( int students ) {
grades = new int[students];

[Code] ....

The output is :

The numbers in the array are: 1 2 3 4 6 7 8 9 10 13 14 15
The highest grade is: 11

For some reason it is not giving me the highest number in my array. Did I write the method wrong ? Is my array even passing through HighestGrade ?

View Replies View Related

Error When Trying To Return Array Via Method

Jan 14, 2014

I am getting these errors with this code, I can not figure why this error is occuring.

what is the issue with this code?

Java Code: package ABC;
import java.util.ArrayList;
import java.util.Scanner;
public class GetInputFromUser {
private ArrayList<String> name = new ArrayList<String>();

[code]....

View Replies View Related

Covariant Return In Overridden Method

Mar 5, 2015

For starters here is my code:

Java Code: class A {
int x=5;
}
class B extends A {
int x=6;
}
public class CovariantTest {
public A getObject() {

[Code] ....

And this is the output I get:

sub
5

I am unable to figure out how this is outputting 5 instead of 6. The getObject method of SubCovariantTest is obviously the one being called, and it returns a new B(). So why am I getting class A's x value? I thought since I was getting a B object returned that I would get B's x value.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved