0

I am new to java try figuring out but couldn't find a right answer. The program is to print letter on even place.

Input: 2 Hacker Rank

Output Hce Rn

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public char[] printeven(char[] arr1)
    {
      char[] result1;
      int index = 0;
      for(int i=1; i<arr1.length; i+=2)
        {
          result1[index] = arr1[i];
          index += 1; 
        }
      System.out.println(result1);
      return result1;

    }


    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
      Scanner scan = new Scanner(System.in);
      int T = scan.nextInt();
      String s1= scan.next();
      String s2= scan.next();
      scan.close();

      char[] array1 = s1.toCharArray();
      char[] array2 = s1.toCharArray();

      printeven(array1);
      printeven(array2);
    }
}

My guess was to remove static from main but than I get the error,

Solution.java:15: error: variable result1 might not have been initialized
          result1[index] = arr1[i];
          ^
Solution.java:18: error: variable result1 might not have been initialized
      System.out.println(result1);
                         ^

2 errors

Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
Rishabh Agarwal
  • 2,374
  • 1
  • 21
  • 27

4 Answers4

1

Simply make your method printeven static:

public static char[] printeven(char[] arr1).

Even if main is in the same class, you're in a static context and you can't access non-static members of Solution class.

You could also initialize a Solution and call the method:

Solution solution = new Solution();
solution.printeven(array1);
solution.printeven(array2);

Finally, you need to initialize your array before using it:

char[] result1 = new char[arr1.length];

Simon
  • 774
  • 4
  • 21
1

This is a basic concept of JAVA. You cannot reference a non-static method from a static method in JAVA. Also, you cannot remove static from the main method because, it's the main method which act as the entry point to your program and it should be kept as static.

In order to access your non-static method, just initialize an object of your class and then use it as follows.

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int T = scan.nextInt();
    String s1= scan.next();
    String s2= scan.next();
    scan.close();

    char[] array1 = s1.toCharArray();
    char[] array2 = s1.toCharArray();

    // initialize an object of your class
    Solution sol=new Solution();
    sol.printeven(array1);
    sol.printeven(array2);
}
Imesha Sudasingha
  • 3,462
  • 1
  • 23
  • 34
0

printeven is a non-static member function of Solution class. To access non-static members of a class you need to create an object of that class and access the member with through the created object. Like this -

MyClass myObject = new MyClass();
myObject.callMyClassFunction();

If you want to call the member function without creating an object of the class then you need to declare the member as static. Then you can simply do

MyClass.callMyClassFunction()

without creating any object.

About the error message -

non static method can not reference from static context

The main function is a static function and a static function can only access other static members. You can read more about static over here - https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Harshil Sharma
  • 2,016
  • 1
  • 29
  • 54
0

I have two different solution,

First,

import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int testCases = scan.nextInt();
        for(int i = 0; i < testCases; i++){
            char[] inputString = scan.next().toCharArray();

            // Print even chars
            for(int j = 0; j < inputString.length; j += 2){
                System.out.print(inputString[j]);
            }
            System.out.print(" ");

            // Print odd chars
            for(int j = 1; j < inputString.length; j += 2){
                System.out.print(inputString[j]);
            }
            System.out.println();
        }
        scan.close();
    }
}

Second,

import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int testCases = scan.nextInt();
        for(int i = 0; i < testCases; i++){
            char[] inputString = scan.next().toCharArray();
            StringBuilder oddString = new StringBuilder();
            StringBuilder evenString = new StringBuilder();

            for(int j = 0; j < inputString.length; j++) {
                if( (j & 1) == 0) {
                    evenString.append(inputString[j]);
                }
                else {
                    oddString.append(inputString[j]);
                }
            }

            System.out.println(evenString + " " + oddString);
        }
        scan.close();
    }
}

But still want to do this problem with multiple methods.

Rishabh Agarwal
  • 2,374
  • 1
  • 21
  • 27