Java Java Help with Methods: Solve Your Method Struggles

  • Thread starter Thread starter smilesofmiles
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
The discussion centers on the challenges faced in Java programming, specifically with method implementation. The user successfully created a program that prints an alternating rectangular pattern using a for loop without methods but struggles to refactor the code into three methods. Key issues identified include mismatched method parameters and variable scope problems. The original method declarations did not align with how they were called in the main method, particularly regarding the number of parameters and their types. Additionally, the methods were incorrectly declared as void while attempting to return values. After receiving guidance, the user revised their code to properly define methods for obtaining user input and to ensure that all methods returned the correct data types. The final implementation includes methods for getting the number of rows and columns, as well as strings for the pattern and separator, all of which are correctly passed to the `createPattern` method. This restructuring resolved the initial issues, allowing the program to function as intended.
smilesofmiles
Messages
18
Reaction score
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
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:
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);
    }
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top