usauae Posted November 21, 2023 Report Posted November 21, 2023 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 } Quote
dasari4kntr Posted November 21, 2023 Report Posted November 21, 2023 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()); } } Quote
usauae Posted November 21, 2023 Author Report Posted November 21, 2023 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 Quote
dasari4kntr Posted November 21, 2023 Report Posted November 21, 2023 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..? Quote
Sye Posted November 21, 2023 Report Posted November 21, 2023 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); } } 1 Quote
usauae Posted November 21, 2023 Author Report Posted November 21, 2023 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.