Java Integer class code not compiling - java

I am new to Java.
I am Learning Wrapper class now from Online Resources
The following code does not compile but according to the online material this is giving results
class Integ
{
public static void main(String[] args)
{
Integer I=new Integer.valueOf("1111",2);
System.out.println(I);
}
}
Can you please correct me where i am going wrong.

class Integ
{
public static void main(String[] args)
{
Integer i = Integer.valueOf("1111", 2);
System.out.println(i);
}
}

dont use the new operator, just do Integer.valueOf("1111",2);
public static void main(String[] args)
{
Integer myI = new Integer.valueOf("1111",2);
// ^^^
System.out.println(I);
}
do instead:
public static void main(String[] args)
{
Integer myI = Integer.valueOf("1111",2);
// ^^^
System.out.println(I);
}

You're not supposed to use new. Just remove it:
Integer I = Integer.valueOf("1111",2);

Related

Can lambda expressions in java be still compacted

I have this code.
public class TypeInterface{
public static void main(String [] args){
StringLengthLambda myLambda = s -> s.length();
System.out.print(myLambda.getLength("abc"));
}
interface StringLengthLamdba{
int getLength(String s);
}
}
Can this code be modified to
StringLengthLambda myLambda = s.length()
or
any other way to shorten this?
I can't think of any way to write a shorter lambda, but you could use a method reference:
StringLengthLambda myLambda = String::length;
You don't even need the package protected interface StringLengthLambda. You could just go with:
public static void main(String [] args){
Function<String, Integer> myLambda = String::length;
System.out.print(myLambda.apply("abc"));
}
If you want different "length-computation-methods" on "abc" you could even go with:
public class TypeInterface {
public static void main(String[] args) {
print(String::length);
}
private static void print(StringLengthLambda myLambda) {
System.out.print(myLambda.getLength("abc"));
}
#FunctionalInterface
interface StringLengthLambda {
int getLength(String s);
}
}

I wrote a method to count positive numbers, but how to call that in main method

public class positiveCount {
private static int countPositive(int[] elems) {
int positive = 0;
for (int i=0;i<elems.length;i++) {
if (elems[i] > 0){
positive++;
}
}
return positive;
}
//This gives me the number of the positive numbers.
public static void main(String[] args) {
}
}
in the main method, i want to enter a list, for example {3,4,5,-2,-3,0},how to call the method positveCount
If the main method is in the same class :
public static void main(String[] args) {
countPositive(new int[] {3,4,5,-2,-3,0});
}
Else (works also for case 1) :
public static void main(String[] args) {
PositiveCount.countPositive(new int[] {3,4,5,-2,-3,0});
}

For_Loop class is not working as expected on eclipse

My For loop is not working and my print statement (i) is showing error in eclipse
Following is the code.
public class For_Loop {
public static void main(String[] args) {
for (int i = 0; i < 10; i++);
System.out.println(i);
}
}
Remove the ; at end of for
public class For_Loop {
public static void main(String[] args) {
for (int i=0;i<10;i++){
System.out.println(i);
}
}
}
Please remove the semicolon from for loop.
public class For_Loop {
public static void main(String[] args) {
for (int i=0;i<10;i++)
System.out.println(i);
}
}

Declaration for an array being called into main from a method (Java)

I am fairly new to java, and am having what I assume is a simple problem with my program.
For the method arrayTest2, I cannot import it into main due to an error on compilation:
"Cannot find symbol, symbol: variable dataStorage".
I have tried also tried the declarations:
arrayTest2(dataStorage[][])
and
arrayTest2(dataStorage[5][5])`
but they don't work.
Any help would be greatly appreciated.
Cheers
import io.*;
public class TrialArray
{
public static void main(String [] args)
{
arrayTest();
arrayTest2(dataStorage);
}
public static void arrayTest()
{
int[][] dataStorage = new int[5][5];
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
}
public static void arrayTest2(int[][] dataStorage)
{
dataStorage[2][2] = 3;
System.out.printf("THIS DOESNT");
}
}
The problem here is the scope: Something defined in one function is not visible in another. What you will normally do to solve this is to return the value. Something like this:
import io.*;
public class TrialArray
{
public static void main(String [] args)
{
int[][] dataStorage = arrayTest();
arrayTest2(dataStorage);
}
public static int[][] arrayTest()
{
int[][] dataStorage = new int[5][5];
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
return dataStorage;
}
public static void arrayTest2(int[][] dataStorage)
{
dataStorage[2][2] = 3;
System.out.printf("THIS DOESNT");
}
}
Alternatively you could have your dataStorage field as a global variable, this is however potentially very confusing. To do that you'd define
public class TrialArray
{
private static int[][] dataStorage;
// ...
public static void arrayTest() {
dataStorage = new int[5][5];
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
}
// ...
}
on this line
arrayTest2(dataStorage);
You are passing parameter to method, that has one argument "dataStorage", but you don't declare it.
You try to pass dataStorage to your arrayTest-function, but dataStorage is not a field of the class, neither is it a local variable of main (aka dataStorage does not exist in main).
public static void main(String [] args) {
arrayTest();
arrayTest2(dataStorage); //<------- What is dataStorage?
}
Here is a little tutorial on variable scopes in Java. You probably want to return the array you created in arrayTest() and use it, but I am just guessing what you want to do.
You cant access variables in declared inside other methods.
To make it work, you would have to do this:
public class TrialArray
{
int[][] dataStorage;
public static void main(String [] args)
{
dataStorage = new int[5][5];
arrayTest();
arrayTest2(dataStorage);
}
public static void arrayTest()
{
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
}
public static void arrayTest2(int[][] dataStorage)
{
dataStorage[2][2] = 3;
System.out.printf("THIS DOESNT");
}
}

Using Objects in a similar way to Variables in Java

Can i use the following code? It's not throwing any error at the Object but at obj.i. Is this a legal way of using an object? Also, how many ways can i create an object other than using the normal syntax obj s = new obj();
public class Test {
static int i;
static Test obj;
obj.i = 10; //am getting a compilation error here "Syntax error on token "i", VariableDeclaratorId expected after this token"
public static void main(String[] args) {
System.out.println(i+" "+ obj);
}
}
You need to place a static block around the obj.i assignment statement for this to work:
public class Test {
static int i;
static Test obj;
static { obj.i = 10; }
public static void main(String[] args) {
System.out.println(i+" "+ obj);
}
}
This is not, you didn't initialize. Furthermore you might not want to use static.
public static void main(String [] args) {
int i = 10;
Test obj = new Test();
obj.setI(i);
System.out.println("my objects I = "+ obj.getI());
}
now in your Test object
public class Test {
private int i;
public void setI(int i) {
this.i = i;
}
public int getI() {
return this.i;
}
}

Categories

Resources