How can I refactor this Class in Java?
As Federico commented, for a program this small it is neither necessary or advisable to do this. It is so small that splitting out parts of the code will not improve readability.
However, doing it for the purpose of learning to program in Java is not a bad thing. So lets assume that that is your goal.
I originally tried to use OOP and add the Scanner in it's own class but I struggled to get the input from the scanner in the main method, when the Scanner was in a different class.
Well, we would need to see what you did to understand your difficulty.
If the input requirements were complex, factoring them into a separate class (or even methods) might work. But if you attempt it here, you end up adding many lines of code for no benefit.
Using OOP is not a goal in its own right. It is a technique that you use to achieve other goals. However, learning to use OOP is a goal.
Ideally I would have also wanted to add the for loop in a separate method to clean up the code, but I wasn't able to access the websites.length field when I added it in a separate class.
You wouldn't put it in a separate class! Put it in a separate method of your Search class.
The problem you most likely encountered is that website was out of scope because it was declared as a local variable within the main method. To make website accessible in another method it either needs to be declared as a field, or you need to pass it as a parameter to the method. Here is an illustrative example of the first way:
public class Search {
private static String[] websites = {
"Google", "http://www.google.com",
"Facebook", "http://www.facebook.com",
"Ebay", "http://www.ebay.com",
"Netflix", "http://www.netflix.com"
};
public static void main(String[] args) {
System.out.println("Enter Your Search Query");
Scanner sc = new Scanner(System.in);
String userInput = sc.nextLine();
search(userInput);
}
private static void search(String userInput) {
for (int i = 0; i < websites.length; i++) {
if (userInput.equals(websites[i])) {
System.out.println("We Found The Value");
System.out.println(websites[i]);
System.out.println(websites[i + 1]);
break;
} else {
System.out.println("There is no matching file");
}
}
}
}
The above is not a good example of a useful refactoring. And it is not OO ... because we are using static methods and static fields. But it illustrates how you can share things between different methods (and even classes) by using fields.
And ... once again ... OO is not a goal in its own right. So the above is sufficient to meet your functional requirements. But if you wanted to make the above more OO you could:
Make the search method an instance method by removing the static.
Call it like this:
Search s = new Search();
s.search(userInput);
(Making websites an instance field would probably be counter-productive. If your requirements call for the searcher to check against a fixed set of websites, using a static for that is a natural way to do it.)
I would appreciate any help or ideas on how I could refactor this simple lines of code to improve performance or in line with Java best practices.
First of all "best practice" is an unhelpful term in the context of programming / software engineering. Please read No Best Practices! to understand what I mean.
Second, there is nothing you can do to improve performance for this code in any significant way. Starting up a Java JVM and loading your code will take 100s of milliseconds. Waiting the the user's input will take seconds. But the time to do the actual searching will be a couple of microseconds. Your code's performance is irrelevant, in this example.
Now you could make the code simpler and more readable by doing either of the following:
- Change
websites into 2 arrays, one containing the website names, and the other the corresponding URLs. (Mixing the search keys and search values into one array leads to problems.)
- Change
websites into HashMap<String, String> and you can then replace the loop with a get call.
But the flipside is that both of those will make initializing the websites more complicated. (In a real application, would be loading that information from a configuration file, or searching a database ...)
Also, as noted in the comments, you could separate the functionality of searching from printing the results. Read about the Separation of Concerns design principle.