Jump to content

Recommended Posts

Posted

ee chinna block of code lo I want to change the iteration from 3 times to a variable.

main method lo string arguments lo pass chesa but wanted to check if there are other better options.

       
public static void main(String[] args) {
Stack<String> loc = new Stack<>();
       
        for (int i = 0; i < 3; i++) {
            String element = scanner.next();
            loc.push(element);
        }
// Remaining work
}
Posted

this is another way... 

 

here instanceVariable is iteration count...

 

public class MyClass {

    private String instanceVariable;

    // Constructor with parameters
    public MyClass(String instanceVariable) {
        this.instanceVariable = instanceVariable;
    }

    // Getter method for instanceVariable
    public String getInstanceVariable() {
        return instanceVariable;
    }

    public static void main(String[] args) {
        // Creating an instance of MyClass with the desired value
        MyClass myInstance = new MyClass("Hello, World!");

        // Using the instance variable through a method of MyClass
        System.out.println(myInstance.getInstanceVariable());
    }
}
Posted
Just now, dasari4kntr said:

this is another way...

 

public class MyClass {

    private String instanceVariable;

    // Constructor with parameters
    public MyClass(String instanceVariable) {
        this.instanceVariable = instanceVariable;
    }

    // Getter method for instanceVariable
    public String getInstanceVariable() {
        return instanceVariable;
    }

    public static void main(String[] args) {
        // Creating an instance of MyClass with the desired value
        MyClass myInstance = new MyClass("Hello, World!");

        // Using the instance variable through a method of MyClass
        System.out.println(myInstance.getInstanceVariable());
    }
}

Thanks bruh trying it out

Posted
2 minutes ago, usauae said:

Thanks bruh trying it out

but in this approach...again you are passing count 3 in the main method only...

is that what you are looking for..?

Posted

You can extract the logic out into a separate method/function and take the inc/count as an input parameter that gets passed/defined in the main

 

import java.util.Scanner;
import java.util.Stack;

public class dummy {

    public static void main(String[] args) {
        process(4);
    }

    private static void process(int inc) {
        Stack<String> loc = new Stack<>();
        for (int i = 0; i < inc; i++) {
            Scanner myObj = new Scanner(System.in);  // Create a Scanner object
            String element = myObj.nextLine();
            loc.push(element);
        }
        System.out.println(loc);
    }
}
  • Upvote 1
Posted
22 minutes ago, dasari4kntr said:

but in this approach...again you are passing count 3 in the main method only...

is that what you are looking for..?

constructor lo manam marchukovachhu kada

basically it should iterate based on the input

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...