Method Invocation - New to This

I am writing a program to match two lines of text. For example, "google" and "chrome" become "gcohorgmlee" (you can assume that the letters are the same length).

public class Collate { String result; String a; String b; public void main(String[] args) { System.out.printf("Enter 1st word: "); Scanner in1 = new Scanner(System.in); a = in1.next(); System.out.printf("Enter second word: "); Scanner in2 = new Scanner(System.in); b = in2.next(); } public String collate(String a, String b) { String accumulator; this.a = a; this.b = b; for (int i = 0; i < a.length(); i++) { result += a.charAt(i); result += b.charAt(i); } return (result); } } 

I am stuck, however, on how to call collate() . I am very new to Java and know almost nothing, so some pointers and help would be greatly appreciated!

+4
source share
5 answers

The simplest answer: just use collate(a, b) and assign the value back to the variable or print it on the screen ...

The longer answer ends with the same, but begins by indicating that your program will not actually be running at the moment ...

public void main(String[] args){ not a valid entry point for your program. Instead, it should be public static void main(String[] args){

Now, if you do this, you will encounter a number of compiler errors in which some non-static parts of your application will not be accessible from a static context ...

The simplest solution would be to provide a constructor the Collate class, which can be called from main ...

 public class Collate{ String result; String a; String b; public void main(String[] args){ new Collate(); } public Collate() { System.out.printf("Enter 1st word: "); Scanner in1 = new Scanner(System.in); a = in1.next(); System.out.printf("Enter second word: "); Scanner in2 = new Scanner(System.in); b = in2.next(); String collation = collate(a, b); System.out.println(collation); } public String collate(String a, String b){ String accumulator; this.a = a; this.b = b; for(int i = 0; i < a.length(); i++) { result += a.charAt(i); result += b.charAt(i); } return (result); } } 

Update

I seem to be creating a good excitement of responsibility. Although he can argue that the responsibility of classes is to ask the user what they want to do and what to do, it can also be argued that the class itself must contain its task, for example ...

 public class Collate{ String result; String a; String b; public void main(String[] args){ System.out.printf("Enter 1st word: "); Scanner in1 = new Scanner(System.in); a = in1.next(); System.out.printf("Enter second word: "); Scanner in2 = new Scanner(System.in); b = in2.next(); Collate collate = new Collate(); String collation = collate.collate(a, b); System.out.println(collation); new Collate(); } public String collate(String a, String b){ String accumulator; this.a = a; this.b = b; for(int i = 0; i < a.length(); i++) { result += a.charAt(i); result += b.charAt(i); } return (result); } } 

Since there is a significant lack of context, it is impossible to come to a specific decision ...

+2
source

Do not subtract anything from the other answers, here I take.

Here is what I changed:

  • Class variables removed. In fact, they do not really matter outside of the collate call.
  • Removed in2 . You really only have to have 1 Scanner .
  • Removed accumulator . It was not used.
  • Changed main to static , otherwise it will not be considered as a Java entry point.
  • Changed collate to static , since it no longer depends on any class instance variables, and it static allows main directly call it.
  • Initialized result to an empty string.

the code:

 public class Collate { public static void main(String[] args) { Scanner in1 = new Scanner(System.in); System.out.printf("Enter 1st word: "); String a = in1.next(); System.out.printf("Enter second word: "); String b = in1.next(); System.out.println(collate(a,b)); } public static String collate(String a, String b) { String result = ""; for (int i = 0; i < a.length(); i++) { result += a.charAt(i); result += b.charAt(i); } return result; } } 

A more efficient option would be to use StringBuilder :
(although this is unlikely to be needed for code written outside the production environment, and even for most production code)

 public static String collate(String a, String b) { StringBuilder result = new StringBuilder(); for (int i = 0; i < a.length(); i++) { result.append(a.charAt(i)); result.append(b.charAt(i)); } return result.toString(); } 
+2
source

Right now, collate uses String variables related to a specific instance of the collate class, but it really is not necessary; all the information necessary to carry out their work is transmitted as parameters. Since the instance variables (fields) Collate.a and Collate.b are not useful, you can eliminate them, and the result variable is also useful only in the collate method, so you should declare it there instead (this uses accumulator , so use this variable , you need to set it to the empty string "" to start). (In fact, your current version will not work if you call collate several times because it will just add characters to the end of the previous result.)

You can make collate static , which means that it doesn’t need information related to any particular collate , and is called directly from main :

 public static String collate(String a, String b) { String accumulator = ""; for (int i = 0; i < a.length(); i++) { accumulator += a.charAt(i); accumulator += b.charAt(i); } return accumulator; } // in main System.out.println(collate(a,b)); 
+1
source

So ... a couple of problems with this code.

Your member variables a and b not declared static ; therefore, they cannot be referenced from main (which should also be static ).

Use a local variable instead of a or b (so you don't confuse the names), and store them in main . You can also completely get rid of member variables.

 public static void main(String... args) { String firstWord; // takes the place of a String secondWord; // takes the place of b // code omitted for brevity } 

Now, to call the method in your class, you need to instantiate a Collate .

 Collate col = new Collate(); 

To call a method, you use an instance and call collate() with arguments that represent what you want to rearrange. I will leave filling the gaps as an exercise for the reader.

 System.out.println(col.<insert_method_here>(_which_was_a?, _which_was_b?)); 

Finally, the accumulator variable exists in the Collate , but the result not. Select accumulator to add. Get rid of your assignments and your member variables.

0
source

All in all, here is what you need

 package mypackage; import java.util.Scanner; public class Collate { public static void main(String[] args) { Scanner in1 = null; Scanner in2 = null; try { System.out.printf("Enter 1st word: "); in1 = new Scanner(System.in); String a = in1.next(); System.out.printf("Enter second word: "); in2 = new Scanner(System.in); String b = in2.next(); System.out.println(new Collate().collate(a, b)); } finally { in1.close(); in2.close(); } } public String collate(String a, String b) { String result = ""; for (int i = 0; i < Math.max(a.length(), b.length()); i++) { result += a.charAt(i); result += b.charAt(i); } return (result); } } 
-one
source

Source: https://habr.com/ru/post/1502329/


All Articles