mohican93
mohican93

Reputation: 121

Printing a returning value

I've just started with Java, and so far been only playing around solving problems online, where you're not supposed to write the whole functional of a program, but only adjust a few lines of code to the already organized code.

However, I'm still struggling to organize my code in a compiling program in IntelliJ Idea, getting confused at,e.g. how methods invocations must be properly written. Here's what I'm getting stuck with: an example from codingbat.com:

- Given a string, return a new string made of every other char starting with the first, so "Hello" yields "Hlo".

I've come up with a solution online, but now I wanna run it in Idea, with main method, with Scanner/BufferedReader input from console etc. Looks like I'm missing something...

import java.util.Scanner;
public class Bat
{
    public static void main (String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        printString();
         }

    public String stringBits(String str) {
        String result = "";
        for (int i = 0; i<str.length();i += 2) {
            result += str.substring(i, i+1);
        }
        return result;
    }
    public static void printString () {
        System.out.println(result);
    }
}

I ask your help to solve it out. What to do to make it:

  1. Read a word from a console;
  2. create a new string;
  3. print it out.

Upvotes: 2

Views: 244

Answers (2)

CConard96
CConard96

Reputation: 914

Your result variable is only accessible from within the "stringBits" method. Since the method returns a string you can do the following to print it:

System.out.println(stringBits(string)); //Call in main method in place of printString();

Edited: My code wasn't a working example. Note that stringBits has to be a static method in order to work.

Upvotes: 1

luk2302
luk2302

Reputation: 57114

Two alternatives:

  • make stringBits static
  • create an instance of the class Bat and invoke the member method

First solution - easy, not much to change

import java.util.Scanner;

public class Bat {
    public static void main (String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        printString(stringBits(str));
    }

    public static String stringBits(String str) {
        String result = "";
        for (int i = 0; i < str.length();i += 2) {
            result += str.substring(i, i + 1);
        }
        return result;
    }

    public static void printString (String string) {
        System.out.println(string);
    }
}

Second solution - a bit more advances

import java.util.Scanner;

public class Bat {

    private String string;

    public Bat(String string) {
        this.string = string;
    }

    public static void main (String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();

        Bat bat = new Bat(str);
        bat.printStringBits();
    }

    private String stringBits() {
        String result = "";
        for (int i = 0; i < string.length(); i += 2) {
            result += string.substring(i, i + 1);
        }
        return result;
    }

    public void printStringBits() {
        System.out.println(stringBits());
    }
}

Upvotes: 2

Related Questions