I tried to create a simple program. The main method is supposed to call out the secondary method to use Scanner in order to ask a person to type in a number, which will be assigned to int x. Then the secondary method supposed to return the int x to main method, which supposed to print it out. And for some reason I just can't do it. No matter what the main method refusing to accept the int x.
import java.util.Scanner;
public class Return{
static Scanner newNum= new Scanner(System.in);
public static void main(String[] args){
getInt();
System.out.println(x);
}
public static getInt( ) {
// TODO Auto-generated method stub
int x;
System.out.print("give a num");
x= newNum.nextInt();
return x;
}}
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?
I'm trying to return an array back to main. The array returned to main should contain the reversed random numbers array. I believe I have the array correctly reversed within the 'reverseArray' method. I'm trying to send this array back to main, but it appears to contain empty data (a bunch of zeros).
Java Code:
class ArrToMain { public static void main(String[] args) { final int NUMBER_OF_ELEMENTS = 1000; double[] numbers = new double [NUMBER_OF_ELEMENTS]; //Invoke initialize method initialize(numbers);
I am writing a program in the GUI Builder and found that when I add code to print user input from text fields to a text file, the main method is no longer recognized. It throws all kinds of errors.
I have been able to get the code to work separately in a project file. Also, have been able to print to the console.
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;} } }
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);
I want to return values from arrays to the main and the problem is i cant use my variables from my constructor, I use new variables in my functions and i know this is no good, when I used the variables from the constructor in my function. I have a compilations errors,also i want to create un object in main and with this object i want to call the functions.
Java Code:
package javaapplication4; import java.util.Scanner; public class JavaApplication4 { public static int[] MyInt; public static double[] MyDouble; public static String[] MyString; public static char[] MyChar;
[Code] .....
The code is working when i run it and i have the right result in my screen but i know this is all wrong with the variables.
I am making a java swing program. I have a main window that opens a JForm window when the user clicks a menu option. Then I have a button on the JForm that closes the window on click. However, I would also somehow like to make the JForm return values to the main window (according to the states of the selector components) when it is closed. How would I go about this? Currently my jForm is a seperate class called "NewGame", and inside the menu item mouse clicked method, I have the following code:
//open new game jform window NewGame newGameWindow = new NewGame(); newGameWindow.setVisible(true); newGameWindow.setLocationRelativeTo(null); //center window
Is there something I can add here to get the object's values upon the window's close? Or is there a way I can add that into the button clicked method on the actual jForm?
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.
I have to write a method called censor that gets an array of strings from the user as an argument and then returns an array of strings that contains all of the original strings in the same order except those with length of 4. For example if cat, dog, moose, lazy, with was entered, then it would return the exact same thing except for the words with and lazy.
Currently, my code so far just prints [Ljava.lang.String;@38cfdf and im stuck.
import java.util.Scanner; public class censorProgram { public static void main (String args[]){ Scanner input = new Scanner (System.in); System.out.println ("How many words would you like to enter?"); int lineOfText = input.nextInt();
[Code] ....
I have to make new string array in the method and return words without four letters in the main method
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..}
I am just trying to test this array, for a locker combination program that involves classes...but the array is printing out the whacky numbers for the location. When I try to call the method in the main, it does not work. How do I call a method that exist within a class into the main method?
public class locker { public static void main(String[] args) { CombinationLock();
My program is user input 20 char and the program will print the most common. So I use another int arr which count the number appears in the original array. i know its not so Effective but I don't know why it run but it stop in the middle. I got this code :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20 at Ex2.common(Ex2.java:39) at Ex2.main(Ex2.java:23)
import java.util.Scanner; class Ex2 { public static void main(String[] arg) { Scanner reader = new Scanner (System.in); char[] arr=new char[20]; System.out.println("Please enter 20 chars:"); for (int i=0;i<20;i++)
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?
public class Fibonacci { public static int fib(int n) { if (n < 2) {return n;} else {return fib(n-1)+fib(n-2);}
[code]....
it contains one method called fib() and one main method.If I would want to have the main method in another class than fib(), how would I write the two classes? Only cutting the main method from this class to another one doesn't work.My question is, is the reason it doesn't work because I then would have to have a constructor in the Fibonacci class, and create a Fibonacci object first which I then use the method on?
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
I'm trying to make a TBG (Text Based Game) in Java for Android. Now, when I try to run it, it says No Main Method to run this program. The compiler also checks the program and tell me there are no errors.
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)
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;
The idea is to create a program to add plants and retrieve plants from a Nursery. I created a class with the properties of the plants and also there is the class an Array list to keep track of the plants entered ( they will have to be printed later) (I am not too sure if adding a public class there is the best option.
The program will allow the user to pick and action and from there the code will do something. I don't want to have all the code inside 'main' The problem is in line 114.This is what I have so far.
Java Code:
package plant.nursery; import java.util.ArrayList; import java.util.Scanner; /**Class to create a plant for a nursery. public class PlantNursery
this is my program error occured run time,but compile sucessfully
{error msg-Error: Main method not found in class helloworldapp.HelloWorldApp, please define the main method as: public static void main(String[] args)} class HelloWorldApp { private int empno; private float salary; void setHelloWorldApp() { empno=12; salary=1200;