Why is there an error in the following java code? - java

I can't figure out the problem in this.
public class Trying {
public static void main(String[] args) {
new Trying().go("hi", 1);
new Trying().go("hi", "world", 2);
}
public void go(String... y, int x) {
System.out.print(y[y.length - 1] + " ");
}
}

A varargs argument, like String... y has to be the last variable in a method declaration. Change your declaration to:
public void go(int x, String... y) {

A varargs argument must be the last variable in a method declaration
public class Trying {
public static void main(String[] args) {
new Trying().go(1,"hi");
new Trying().go(2,"hi", "world");
}
public void go(int x,String... y) {
for(int i=0;i<x;i++){
System.out.println(y[i]);
}
}
}
For More

There is an attempt to declare Regular parameter after the varargs parameter which is illegal.
public void go(String... y, int x) //Error
Restriction of varags:
varargs must be declared last
2.There must be only one varargs parameter
change your method to public void go(int x, String... y)

Related

math.pow in method results to an error

could anyone please explain why the following method doesn't work and returns the following error message: "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: "
class Example01 {
public static void main(String[] args) {
System.out.println(myExp(2));
}
double myExp(int x) {
return Math.pow(x,2);
}
}
I thought it might be for using int variable in Math.pow but I tried it this way without setting up a method and it worked ok:
System.out.println(Math.pow(2,2));
Could someone please give a bit of a flavour on why the method fails to return the result?
Many thanks,
Vlad
change the method double myExp(int x) to static double myExp(int x).
method double myExp(int x) should be static.
You have to declare myExp static.
static double myExp(int x) {
return Math.pow(x,2);
}
or you can instantiate a Example01 class and call the function.
class Example01 {
public static void main(String[] args) {
Example01 ex = new Example01();
System.out.println(ex.myExp(2));
}
double myExp(int x) {
return Math.pow(x,2);
}
}
In java all the non-static method are instance method i.e. they require an instance or an object to be called upon.Either declare your myExp(int x) method static i.e. static double myExp(int x) or instantiate your class to create an object and then call myExp(int x) on it i.e.
class Example01 {
public static void main(String[] args) {
Example01 example = new Example01();
System.out.println(example.myExp(2));
}
double myExp(int x) {
return Math.pow(x,2);
}
}

Its not applicable for the arguments (int, String), but i have the correct format for it

I am just making a random class so that i can mess around and learn java with it. Ive kinda got this concept of arguments down.
public class OffensiveLine {
public static void main(String args[]){
String[] blocks = {"Swim Move", "Hello"};
LineMan jeff = new LineMan(80, 90);
int i = 1;
WideReciever.block(32, blocks[i]);
jeff.block();
}
}
public class WideReciever extends Speed{
static Date now = new Date(1);
public WideReciever() {
// TODO Auto-generated constructor stub
super();
}
public static void run(){
}
public static void block(int b, String[] wow){
int i = 1;
System.out.println(wow[i]);
}
}
public static void block(int b, String[] wow){
This method wants a String array.
However, you call it like this:
WideReciever.block(32, blocks[i]);
blocks is a String array, but blocks[i] is just a string.
You could either change the block method to take a string, or pass the array in:
public static void block(int b, String[] wow) {
System.out.println(wow[1]);
}
or:
WideReciever.block(32, blocks);
This
WideReciever.block(32, blocks[i]);
Is passing in an int and a String to a method which accepts an int and a String array. What you are likely trying to do is:
WideReciever.block(32, blocks);

How to call a variable in another method?

How to call a variable in another method in the same class?
public void example(){
String x='name';
}
public void take(){
/*how to call x variable*/
}
First declare your method to accept a parameter:
public void take(String s){
//
}
Then pass it:
public void example(){
String x = "name";
take(x);
}
Using an instance variable is not a good choice, because it would require calling some code to set up the value before take() is called, and take() have no control over that, which could lead to bugs. Also it wouldn't be threadsafe.
You make it an instance variable of the class:
public class MyClass
{
String x;
public void example(){ x = "name"; } // note the double quotes
public void take(){ System.out.println( x ); }
}
Since they are in different scopes you can't.
One way to get around this is to make x a member variable like so:
String x;
public void example(){
this.x = "name";
}
public void take(){
// Do stuff to this.x
}
public class Test
{
static String x;
public static void method1
{
x="name";
}
public static void method2
{
System.out.println(+x);
}
}

creating a generic static method in java

I wanted to create a static method which prints the contents of an array.I wrote one for String[] as below
public static void print(String[] a){
for(String x : a){
System.out.print(x+", ");
}
System.out.println();
}
I thought I could create a method which takes in a generic type ,and modified the code as below
public class ArrayPrinting<E> {
public static void printArray(E[] a){
for(E x : a){
System.out.print(x+", ");
}
System.out.println();
}
public static void main(String[] args) {
String[] a = {"A","B","C","D","E"};
}
}
But,this gives a compiler error
'Cannot make a static reference to the non-static type E'
So,how do I create such a method?or is it impossible ? Since this is a static method, I wonder how I can invoke the method without creating an instance. A call like
ArrayPrinting<E>.printArray(a) doesn't look right ..
Can someone help?
Try this
public class ArrayPrinting {
public static <E> void printArray(E[] a){
for(E x : a){
System.out.print(x+", ");
}
System.out.println();
}
public static void main(String[] args) {
String[] a = {"A","B","C","D","E"};
ArrayPrinting.printArray(a);
}
}
Ravi already covered the proper syntax for a generic method. I just want to point out that this particular method doesn't need to be generic:
public static void printArray(Object[] a) {
for (Object x : a) {
System.out.print(x + ", ");
}
System.out.println();
}
The reason this works is array covariance - a String[] is an Object[].
Class's generic type parameters are for class level variables and methods (instance variables and methods).So you can't use it.
You can handle it by declaring type parameter in the method itself:
public static <E> void printArray(E[] a){
.............
}
public class ArrayPrinting<E> {
public void printArray(E[] a){
for(E x : a){
System.out.print(x+", ");
}
System.out.println();
}
public static void main(String[] args) {
String[] a = {"A","B","C","D","E"};
new ArrayPrinting().printArray(a);
}
}

Can't send string to another class from if loop

I'm trying to pass a string from one class to another, but not succeeding. I realized during research and trial and error that I need to have "public static void main(String[] args) {}" to be able to use the if statement, but then getY() produces an error. What can I do differently?
public class Testing {
public static String z;
public static void main(String[] args) {
int x = 15;
if (x >= 10)
{
z = "Blabla";
}
public static String getZ() {
return z;
}
}
}
The other class is
class B {
public static void main(String args[]) {
String x = Klasatest2.getZ();
System.out.println(x);
}
}
Error:
Klasatest2.java:14: illegal start of expression
public static String getZ()
^
Klasatest2.java:14: illegal start of expression
public static String getZ() {
^
Klasatest2.java:14: ';' expected
public static String getZ() {
^
Klasatest2.java:14: ';' expected
public static String getZ() {
^
4 errors
For starters, you can't declare a method inside of a method,
public static void main(String[] args) {
int x = 15;
if (x >= 10)
{
z = "Blabla";
}
public static String getZ() {
return z;
}
}
}
So you have to make sure that get getZ() method is declared OUTSIDE of main(string[] args)
Like this,
public class Test {
public static String z;
public static void main(String[] args) {
int x = 15;
if (x >= 10)
{
z = "Blabla";
}
}
public static String getZ() {
return z;
}
}
Also, you shouldn't have two main(String[] args) methods, as only one of them will be called unless for some reason you decide to call it yourself, which would be very strange.
So if you wanted the string to be set in class Test, you would need to call it's main method from your other class, possible like this.
Test.main(null);
Your application can only have one main(String args[]) method.
Try this:
public class Testing {
public static void main(String[] args) {
A a = new A("hy");
B b = new B(a.z);
}
public class A {
public String z;
public A (String z) {
this.z = z;
}
}
public class B {
public B (String y) {
System.out.println(y);
}
}
}

Categories

Resources