How do I display a matrix in a method? - java

For some reason, I can't get my method to display a previously mentioned array. The only issue is in my first line. The error code is: "illegal start of expression". What am I doing wrong?
public static void display(int matrix[][]) {
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
System.out.print(matrix[row][col] + " ");
}
System.out.println();
}
}

your method has to be inside the class, but outside any other method.
if this method is within the main method, then your code will look something like this:
public class name
{
public static void main(String[] args)
{
//code
yourmethod(parameter); //*somewhere in your code*
}
public static void yourmethod(parameters p)
{
//code
}
}
if it's within a regular class with no main method
public class name
{
public name()
{
//code
}
public void yourmethod(parameters p)
{
//code
}
//other methods
}

The error is not in that method, surely you missed a } before

Related

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.

Why this basic java programme is not compiling

I am getting an error while compiling this programe:
class ArraysInMethods {
public static void main(String[] args) {
int array[]={1,6,2,5,3,8,9,0,5};
Add5(array);
for(int y : array){
System.out.println(y);
}
}
public void Add5(int x[]){
for(int counter=0; counter < x.length; counter++){
x[counter]+=5;
}
}
}
I am getting the following ERROR
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static method Add5(int[]) from the type ArraysInMethods
at ArraysInMethods.main(ArraysInMethods.java:6)
there you go, make your Add5 method static:
public static void Add5(int x[]){
for(int counter=0; counter < x.length; counter++){
x[counter]+=5;
}
}
basically it means that only static methods can be called from within static methods and main method in Java is static.
EDIT:
Why do you have to do that? Because static members of class exist without instance of a class whether non-static members don't. If you'd call static method without creating an instance of a class (valid) that would try to call a non-static method it wouldn't work because non-static method doesn't exist without an instance of a class.
Static methods and variables are at class level and can be called with the reference of class name while non static methods are at object level and can be called using the object of the class.
There is 2 things you can do.
Change the method to static
class ArraysInMethods {
public static void main(String[] args) {
int array[]={1,6,2,5,3,8,9,0,5};
Add5(array);
for(int y : array){
System.out.println(y);
}
}
public static void Add5(int x[]){
for(int counter=0; counter < x.length; counter++){
x[counter]+=5;
}
}
}
Make the object of the class and call the method of the class using the object.
class ArraysInMethods {
public static void main(String[] args) {
int array[]={1,6,2,5,3,8,9,0,5};
new ArraysInMethods().Add5(array);
for(int y : array){
System.out.println(y);
}
}
public void Add5(int x[]){
for(int counter=0; counter < x.length; counter++){
x[counter]+=5;
}
}
}
static method can call to static methods in java.
Add5() is not static method so compile time exception came.
package com.org.test; class ArraysInMethods {
public static void main(String[] args) {
int array[]={1,6,2,5,3,8,9,0,5};
Add5(array);
for(int y : array){
System.out.println(y);
}
}
public static void Add5(int x[]){
for(int counter=0; counter < x.length; counter++){
x[counter]+=5;
}
}
}

Getting Certain Method Into Main Method in 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);

cannot cast java.util.concurrent.ForkJoinWorkerThread into java.lang.Thread- Complex structure

I understand the concept of fork/join, but almost all resources over the internet use, Fibonacci as an example, but my scenario is more complex. I sketched the program, and I have an exception as commented in the below code..
Class Test
{
public static void main(String[] args)
{
ForkJoinPool p= new ForkJoinPool(5);
p.invoke(new Train());
}
}
Class Train extends RecursiveAction
{
public Train(int d, int n)
{
//some intialization
}
public Train()
{
t= new Train[5];
new Vec().run_Vec(t);
}
#Override
protected void compute() {
for(int i= 1; i< 8; i++)
{
// x, and y are predefined
temp[x][y] = some calculation;
}
}
}
class Vec
{
public void run_Vec(Train[] t) {
for (int i = 0; i < 5; i++) {
t[i] = new Train(i*4, i/2);
t[i].fork(); // error java.lang.Thread cannot be cast to java.util.concurrent.ForkJoinWorkerThread
}
for (int i = 0; i < 5; i++) {
t[i].join();
}
}
}
}
I think your problem is due to calling fork() from the main thread. When you call p.invoke(new Train()), your default train constructor actually calls the run_vec() and tries to fork(). Upon reading the javadocs, there are examples that fork() is called within compute(). You need to be calling fork from a thread started by p.invoke().
Based on this Java article I made the following code snippet: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/RecursiveAction.html
You should only call fork (spawn) within a "running" Thread. This means you must pass on the Train array within the compute method:
package ...;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
class ConcurrentTest {
public static void main(String[] args) {
ForkJoinPool p= new ForkJoinPool(5);
p.invoke(new Train());
}
public static class Train extends RecursiveAction {
private Train[] t = null;
public Train(int d, int n) {
//some code
}
public Train() {
t= new Train[5];
}
#Override
protected void compute() {
if(t != null) {
new Vec().run_Vec(t);
for(int i= 1; i< 8; i++) {
System.out.println("Test.Train.compute(): " + i);
}
}
}
}
public static class Vec
{
public void run_Vec(Train[] t) {
for (int i = 0; i < 5; i++) {
t[i] = new Train(i*4, i/2);
System.out.println("Clazz: " + t[i].getClass());
t[i].fork(); // error java.lang.Thread cannot be cast to java.util.concurrent.ForkJoinWorkerThread
}
for (int i = 0; i < 5; i++) {
t[i].join();
}
}
}
}

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