Getting Certain Method Into Main Method in Java - java

I've written up a code with several different methods. I can't seem to find the correct way to get the last method I have into the main method so it can print out the correct output.
CODE:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a number:");
int num = in.nextInt();
} // this is my main method
public void results (int num) {
for (int i = 1; i < num; i++) {
System.out.print(space(num - i));
System.out.println(method1(i));
}
for (int i = 0; i < num; i++) {
System.out.println(method2(num-i));
System.out.print(space(i));
}
} //this is the method that I want inside my main method
I thought I could simply put System.out.println(results(num)); into my main method but that doesn't work. Can anyone explain what I'm doing wrong and help me with this problem?

Your main method is static, but your results method is not. Either make results be static, or declare a new instance of your class to use inside of main.
public class MyClass
{
public static void main(String[] args) {
results(1);
}
public static void results (int num) {
}
}
or
public class MyClass
{
public static void main(String[] args) {
new MyClass().results(1);
}
public void results (int num) {
}
}

You can't pass what results returns to anything such as System.out.println, because results has a void return type. But results already is printing out information, so just call results.
results(num);

Related

How can i call the method from another class?

Hi i am trying to solve the problem I am facing
public class exam {
public static void main(String[] args) {
test1 a = new test1();
}
int zahl(int x, int y) {
int e;
if(x>y) {
e=x-y;
}else {
e=y-x;
}
if(e==0) {
return 0;
}
int z=0;
int i=1;
while(i<=e) {
z=z+i;
i++;
}
return z;
}
}
what I want to do is to call the zahl method to the test1 class
public class test1{
private exam b;
public void init() {
b = new exam();
}
void test() {
int result = b.zahl(2, 2);
assertEquals(1, result);
}
}
this is what I have tried, but it returns nothing, even though it's supposed to show me error.
You should probably be declaring your functions with the public tag i.e. public void test() if you intend to access them from other functions outside of that package. The usual Class naming convention in Java is with capital first letter, which makes your code more readable for you and others.
For your question, I don't think you are actually invoking the test() method of the test1 class. If you want that method to get called every time, you could place it inside the default Constructor.

Static modifier from C++, but in Java?

I'm searching for modifier in Java that has the same exact purpose as Static in C++ has. I mean, that variable is only initialized once in function, then every time we call that function again, values from the previous call are saved. That's how code looks in C++:
void counter()
{
static int count=0;
cout << count++;
}
int main()
{
for(int i=0;i<5;i++)
{
counter();
}
}
and the output should be 0 1 2 3 4, is there something that has the same purpose, but in Java?
looks like you are starting with java. To help you understand the concept i wrote the same code with comments for your understanding.
package yourXYZpackage;
public class yourABCclass{
//Declare in class body so your methods(functions) can access
//and the changes made will be global in C++ context.
static int count=0;
void counter()
{
count = count++;
System.out.println(count);
//thats how you can display on console
}
//this is the main method like C++
public static void main(String[] args){
for(int i=0;i<5;i++)
{
counter();
}
}
}
hope this will help..//
u need to create a constructor first.
public class answer2 {
static int count =0;
answer2() {
System.out.println(count);
count++;
}
public static void main(String[]args) {
for(int i=0;i<5;i++) {
new answer2();
}
}
}
just define static variables as a class member normally. java developers promote object oriented programming so even your main function is a method defined in another class whose name is same as your program name.
now for your question if you want to define a static variable:
public class abc
{
public static int count = 0;
}
public class xyz
{
public void increment()
{
abc.count++;
}
public static void main(String[] args)
{
System.out.println(abc.count);
increment();
System.out.println(abc.count);
}
}
Hope it helps.

Public method not returning the variable

Im new in Java why is my method not returning the variable I put. I want to use the return thing I dont want to use sysout but its not working.
public static void main(String[] args) {
counter("this is a test");
}
public static int counter(String sentence) {
int count = 0;
while (CODE DELETED){
count=count+1;
MORE CODE DELETED
}
CODE DELETED
return count;
}
It does return it, but you neither assign the value, nor do you use it.
Try this:
public static void main(String[] args) {
int result = counter("this is a test");
System.out.println("result = " + result);
}
The method is returning the value, but you are not doing anything with the returned value.
Maybe you misunderstand what "returning a value" means. It does not mean that the returned value is automatically printed to the console.
You'll have to put the returned value in a variable in the main method, and then you can for example print it:
public static void main(String[] args) {
int value = counter("this is a test");
System.out.println(value);
}
You can also print it directly, not storing it in a variable:
public static void main(String[] args) {
// Print whatever the call to the method 'counter(...)' returns
System.out.println(counter("this is a test"));
}

How to run method from other class

Here's my code:
public class Oddmain {
public static void main(String[] args) {
}
private static void comp() {
for (int i = 1; i <= 10; i += 2) {
System.out.print(i + " ");
}
}
}
I want it to print out only odd numbers between 1 and 10, which it does fine. The thing I'm curious about is making it in another class, and calling upon it in the main method. How would I have the main method call upon something from another class?
I tried using the run method:
public void run();
//my for loop here
But that didn't really work. Whenever I move:
private static void comp() {
for (int i = 1; i <= 10; i += 2) {
System.out.print(i + " ");
}
}
to the other class, it says this:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method comp() is undefined for the type Oddmain
at Oddmain.main(Oddmain.java:4)
I think that this means it want me to create the method comp() in this class (Oddmain), but I want to create in the class Oddcomp. This is something I can do, correct?
I'm really new to coding, so please explain stuff very thorough when posting, so I know why I'm doing what I'm doing, and not just copy/pasting.
The problem that you have here it's that you don't close main method.
public class Oddmain {
public static void main(String[] args){
}
private static void comp() {
for(int i = 1; i <= 10; i += 2){
System.out.print(i + " ");
}
}
}
And now you can call your method comp from your main method:
public static void main(String[] args){
comp();
}
I expect it will be helpful for you!
public class Oddmain {
public static void main(String[] args){
private static void comp() {
for(int i = 1; i <= 10; i += 2){
System.out.print(i + " ");
}
}
}
There is no way this code works fine, you cannot create a new method with in the main statement. I believe you meant:
public class Oddmain {
public static void main(String[] args){
comp();
}
private static void comp() {
for(int i = 1; i <= 10; i += 2){
System.out.print(i + " ");
}
}
}
To utilise the comp method in another class you can go about this in two ways...
NOTE: In both cases you must change the access modifier of your method from private if you wish to use the method in another class.
Private fields can only be accessed with in the class they are defined in.
1) Create an instance of Oddmain in your other class.
public class OtherClass {
public static void main(String[] args){
Oddmain s1 = new Oddmain();
Oddmain.comp();
}
}
2) Call the comp() method in a static manner, static methods are often called by using the class name the static method is defined in, e.g.
Oddmain.comp();
Static methods should often be called via the latter format. In both cases you are calling the comp method in Oddmain. Remember static fields belong to a class not an object.

Syntax error on token ";", "{" expected after this token

Just a simple class calls a class that prints an array. I get a syntax error in Eclipse. I also get an error that I don't have a method called Kremalation.
public class AytiMain {
public static void main(String[] args) {
AytiMain.Kremalation();
}
}
public class Kremalation {
String[] ena = { "PEINAW", "PEINOUSA", "PETHAINW" };
int i; // <= syntax error on token ";", { expected after this token
for (i = 0; i <= ena.lenght; i++)
System.out.println(ena[i]);
}
}
You have code (which is not declaring a variable and/or initializing it) ouside a method, which is:
for (i=0; i<=ena.lenght; i++)
System.out.println(ena[i]);
In Java, code MUST reside inside a method. You can't call a class, you have to call a method that is declared inside a class.
WRONG:
class ClassName {
for (...)
}
CORRECT:
class ClassName {
static void method() {
for (...)
}
public static void main(String[] args) {
ClassName.method();
}
}
You can not define method as class.
It should be
public static void kremalation()
{
String ena[]={"PEINAW","PEINOUSA","PETHAINW"};
int i;
for (i=0; i<=ena.lenght; i++)
System.out.println(ena[i]);
}
public class AytiMain {
public static void main(String[] args) {
AytiMain.Kremalation();
}
public static void Kremalation() {// change here.
String ena[]={"PEINAW","PEINOUSA","PETHAINW"};
int i;
for (i=0; i<=ena.lenght; i++)
System.out.println(ena[i]);
}
}
Two possible answers.
1) Remove public from second one if you would like to define it as class.
2) Move Kremalation inside closing brace and replace class with void and make it as static method.
You cannot have executable code directly inside a class.. Add a method and use instance of that class to call that method..
public class Kremalation {
public void method() {
String ena[]={"PEINAW","PEINOUSA","PETHAINW"};
int i;
for (i=0; i<=ena.lenght; i++)
System.out.println(ena[i]);
}
}
Now, in your main method, write: -
public static void main(String[] args) {
new Kremalation().method();
}
Two approaches to solve this problem.....
1st there are 2 classes in the same file :
public class AytiMain {
public static void main(String[] args) {
new Kremalation().doIt();
}
}
class Kremalation {
public void doIt(){ // In Java Codes should be in blocks
// Like methods or instance initializer blocks
String ena[]={"PEINAW","PEINOUSA","PETHAINW"};
int i;
for (i=0; i<=ena.lenght; i++)
System.out.println(ena[i]);
}
}
2nd change the class to method :
public class AytiMain {
public static void main(String[] args) {
AytiMain.Kremalation();
}
public static void Kremalation() { // change here.
String ena[]={"PEINAW","PEINOUSA","PETHAINW"};
int i;
for (i=0; i<=ena.lenght; i++)
System.out.println(ena[i]);
}
}

Categories