Reputation: 21
I have a full completed program but am unsure of how to call the method in the main? My return value needs to be:
(word + " appears " + count + " times " );
and
("The average score for reviews containing the word horrible is " + average);
All of the code needs to stay within methodOne. I just need to know, how do I call the method and return the correct values?
Note : I am very new to Java so please refrain from using complex ideas
Current Code: import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException;
public class ReactionAnalysis {
public static void main (String [] args)
{
System.out.println("Call Method One:");
}
public static String methodOne() throws FileNotFoundException
{
Scanner keyboardInput = new Scanner(System.in);
System.out.println("Enter a word: ");
String word = keyboardInput.next();
File inputFile = new File("movieReviews.txt");
Scanner movieReview = new Scanner(inputFile);
String reviewText;
int reviewScore;
int count = 0;
double total = 0;
while (movieReview.hasNext())
{
reviewScore = movieReview.nextInt();
reviewText = movieReview.nextLine();
if (reviewText.contains(word))
{
count++;
total = total + reviewScore;
}
}
String str = (word + " appears " + count + " times " );
double average = (total / count );
String str2 = ("The average score for reviews containing the word horrible is " + average);
return str + str2;
}
}
Upvotes: 0
Views: 53
Reputation: 17085
There are many ways to improve this code, but to keep it simple and yet useful, I suggest you :
readReviews()
;String[] result = program.readReviews();
(I suggested a Result
object, but did not suit the OP);public class methodOne {
public static void main(String[] args) throws FileNotFoundException {
methodOne methodOne = new methodOne();
String[] result = methodOne.readReviews();
for(String res: result){
System.out.println(res);
}
}
public String[] readReviews() throws FileNotFoundException{
Scanner keyboardInput = new Scanner(System.in);
System.out.println("Enter a word: ");
String word = keyboardInput.next();
File inputFile = new File("movieReviews.txt");
Scanner movieReview = new Scanner(inputFile);
String reviewText;
int reviewScore;
int count = 0;
double total = 0;
while (movieReview.hasNext()) {
reviewScore = movieReview.nextInt();
reviewText = movieReview.nextLine();
if (reviewText.contains(word)) {
count++;
total = total + reviewScore;
}
}
double average = (total / count);
return new String[]{
word + " appears " + count + " times ",
"The average score for reviews containing the word horrible is " + average};
}
}
Upvotes: 0
Reputation: 100
There's not really a template for putting code into methods... you just make the method return what you want, and try to do that as efficiently as possible.
One way you can return multiple values is by returning an array (or you could use an ArrayList
, but that would be overkill in this situation.) You can do all the calculations inside the method, and when all is said and done return an array of String
s containing what you wanted them to. For example:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class methodOne {
public static void main(String[] args) {
Scanner keyboardInput = new Scanner(System.in);
System.out.println("Enter a word: ");
String word = keyboardInput.next();
File inputFile = new File("movieReviews.txt");
String[] data = wordCount(word, inputFile);
for(int i =0; i < data.length; i++) {
System.out.println(data[i]);
}
}
public static String[] wordCount(String word, File inputFile) {
String[] results = new String[2];
Scanner movieReview = null;
try {
movieReview = new Scanner(inputFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
String reviewText;
int reviewScore;
int count = 0;
double total = 0;
while (movieReview.hasNext()) {
reviewScore = movieReview.nextInt();
reviewText = movieReview.nextLine();
if (reviewText.contains(word)) {
count++;
total = total + reviewScore;
}
}
double average = total/count;
results[0] = word + " appears " + count + " times";
results[1] = "The average score for reviews containing the word" + word + " is " + average;
return results;
}
}
If you have any questions, feel free to let me know!
Upvotes: 1
Reputation: 602
Passing values into methods is fairly straightforward. For instance:
public class NewClass {
public static void main(String[] args) {
// passing two literal strings into the method
// and then invoking it
System.out.println(strCombo("Hello, ", "world!"));
}
// if you are going to call a method in main, it must be static
public static String strCombo(String str, String str2) {
return str + str2;
}
}
This can work for any data type, you would just need to set it to the right one or VOID if it doesn't return anything.
Hope this helps!
Upvotes: 0