Java Help with Methods: Solve Your Method Struggles

  • MHB
  • Thread starter smilesofmiles
  • Start date
  • Tags
    Java
In summary: System.out.print(secondStr); } if (j != userColumns){ System.out.print(separator); } System.out.println(""); } } public static void main(String[] args) {
  • #1
smilesofmiles
19
0
I am having a difficult time with methods in java. I've been able to get my program to run correctly without methods. My for loop does as intended by printing an alternating rectangular pattern with no separator at the ends. However, after trying to decompose my code into 3 methods I've hit a wall. Any hints, suggestions or help would be greatly appreciated.
Code:
package patternmakerwithmethods;

import java.util.Scanner;

public class PatternMakerWithMethods {
        
public static void getNumbers(int userRows, int userColumns){
        Scanner in = new Scanner(System.in);
        userRows = in.nextInt();
        userColumns = in.nextInt();
                    
   } 

public static void getStrings(String starterStr, String secondStr, String separator){
        Scanner in = new Scanner(System.in);
        starterStr = in.nextLine();
        secondStr = in.nextLine();
        separator = in.nextLine();
}

public static void createPattern(int i, int j){
       
    for(i = 1; i <= userRows; i++){
        for(j = 1; j <= userColumns; j++){
            if(((i + j) % 2) == 0){
                System.out.print(starterStr);
            }
            else{
                System.out.print(secondStr);
            }
             if (j != userColumns){
                System.out.print(separator);
            }
        }   
            
        System.out.println("");
    
        }
    }
 public static void main(String[] args) {
     
     int userRows = getNumbers("Please enter an integer value between 1 and 10 inclusive for the amount of rows.");
     int userColumns = getNumbers("Please enter another value between 1 and 10 inclusive amount of columns.");
     String starterStr = getStrings("Please enter a string for the starting pattern.");
     String secondStr = getStrings("Please enter a second string for the pattern.");
     String separator = getStrings("Please enter a string separator.");
      
     createPattern(userRows, userColumns, starterStr,secondStr, separator);
    }
}
 
Technology news on Phys.org
  • #2
smilesofmiles said:
I am having a difficult time with methods in java. I've been able to get my program to run correctly without methods. My for loop does as intended by printing an alternating rectangular pattern with no separator at the ends. However, after trying to decompose my code into 3 methods I've hit a wall. Any hints, suggestions or help would be greatly appreciated.
Code:
package patternmakerwithmethods;

import java.util.Scanner;

public class PatternMakerWithMethods {
        
public static void getNumbers(int userRows, int userColumns){
        Scanner in = new Scanner(System.in);
        userRows = in.nextInt();
        userColumns = in.nextInt();
                    
   } 

public static void getStrings(String starterStr, String secondStr, String separator){
        Scanner in = new Scanner(System.in);
        starterStr = in.nextLine();
        secondStr = in.nextLine();
        separator = in.nextLine();
}

public static void createPattern(int i, int j){
       
    for(i = 1; i <= userRows; i++){
        for(j = 1; j <= userColumns; j++){
            if(((i + j) % 2) == 0){
                System.out.print(starterStr);
            }
            else{
                System.out.print(secondStr);
            }
             if (j != userColumns){
                System.out.print(separator);
            }
        }   
            
        System.out.println("");
    
        }
    }
 public static void main(String[] args) {
     
     int userRows = getNumbers("Please enter an integer value between 1 and 10 inclusive for the amount of rows.");
     int userColumns = getNumbers("Please enter another value between 1 and 10 inclusive amount of columns.");
     String starterStr = getStrings("Please enter a string for the starting pattern.");
     String secondStr = getStrings("Please enter a second string for the pattern.");
     String separator = getStrings("Please enter a string separator.");
      
     createPattern(userRows, userColumns, starterStr,secondStr, separator);
    }
}

Your main problem seems to be you're getting confused about how method declaration (i.e. the bit of code that says what the method does) relates to method usage. In your main method you're calling createPattern with 5 parameters userRows, userColumns, starterStr, secondStr and seperator but your createPattern method only takes 2 parameters i, j. The parameters, if any, you have in your method declaration must be matched, by number of parameters and the variable type not name of each parameter, by the variables passed to the method. For example if you have a method public int a(string c, int b) when calling the method the first parameter must be a string and the second must be an int.

For your getStrings and getNumbers methods you have similar issues and you also have the additional problem that you are assigning the value from the methods to a variable, but you've declared the method as having a return value of void, which means it does not return anything.

This could also be because you aren't understanding variable scope. Variable scope tells you where you can use a variable and it is based on a couple of things, first where you declare the variable and second the optional access modifier (e.g public, private, protected) that you give it. Variables declared in a method do not have an access modifier. Since your last three parameters that are being passed to your createPattern method are declared in main they only exist in main, which means that in any other method call the variable cannot be directly used.
 
Last edited:
  • #3
Wow! Thank you for the detailed response. Your explanation helped immensely. Here is what I ended up with.

Code:
package patternmakerwithmethods;

import java.util.Scanner;

public class PatternMakerWithMethods {
        
    public static int getNumberColumns(){
        Scanner in = new Scanner(System.in);
      
        System.out.println("Please enter an integer value between 5 and 10 inclusive for the amount of columns.");
        int userColumns = in.nextInt();
       
        return userColumns;
    } 
    
    public static int getNumberRows(){
        Scanner in = new Scanner(System.in);
       
        System.out.println("Please enter an integer value between 5 and 10 inclusive for the amount of rows.");
        int userRows = in.nextInt();
       
        return userRows;
    }

    public static String getStarterString(){
        Scanner in = new Scanner(System.in);
        
        System.out.println("Please enter a starter string.");
        String starterStr = in.nextLine();
        
        return starterStr;
    }

    public static String getSecondString(){
        Scanner in = new Scanner(System.in);
    
        System.out.println("Please enter a second string.");
        String secondStr = in.nextLine();  
    
        return secondStr;
    }

    public static String getSeparator(){
        Scanner in = new Scanner(System.in);
    
        System.out.println("Please enter a separator.");
        String separator = in.nextLine();
    
        return separator;
    }

    public static void createPattern(int userRows, int userColumns, String starterStr, String secondStr, String separator ){

        for(int i = 1; i <= userRows; i++){
            for(int j = 1; j <= userColumns; j++){
                if(((i + j) % 2) == 0){
                    System.out.print(starterStr);
                }
                else{
                    System.out.print(secondStr);
                }
                if (j != userColumns){
                    System.out.print(separator);
                }
            }   
            System.out.println("");
            }
    }
    
    public static void main(String[] args) {
     
        int userRows = getNumberRows();
        int userColumns = getNumberColumns();
        String starterStr = getStarterString();
        String secondStr = getSecondString();
        String separator = getSeparator();
        createPattern(userRows, userColumns, starterStr, secondStr, separator);
    }
}
 

What is Java Help with Methods: Solve Your Method Struggles?

Java Help with Methods: Solve Your Method Struggles is a guide or tutorial that provides assistance in understanding and using methods in the Java programming language. It aims to help users overcome any difficulties or struggles they may have with using methods in their code.

Why are methods important in Java?

Methods are important in Java because they allow for the creation of reusable blocks of code that perform a specific task. This makes code more organized, easier to read, and reduces the chances of errors or bugs. Methods also make it easier to make changes to code without having to modify every instance of it.

How can Java Help with Methods benefit me?

Java Help with Methods can benefit you by providing a better understanding of how methods work in Java, allowing you to write more efficient and organized code. It can also help you troubleshoot any issues you may encounter with your methods and provide tips and tricks for optimizing their use.

What are some common struggles with methods in Java?

Some common struggles with methods in Java include understanding their syntax and how to correctly call them, determining the correct return type, and managing parameters and arguments. Other challenges may include debugging methods and optimizing their use.

Where can I find Java Help with Methods: Solve Your Method Struggles?

Java Help with Methods: Solve Your Method Struggles can be found online through various programming websites, forums, and tutorial sites. It may also be available in the form of books or courses. Additionally, you can seek help from experienced programmers or attend workshops or seminars that cover this topic.

Similar threads

  • Programming and Computer Science
Replies
3
Views
778
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
Back
Top