473,408 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Quick Java Question

1
Hello all, one of the conditions for my assessment is that I shouldnt't be re-using code that is similar/same as previously used in my program. I've got "re-used" code everywhere, but not sure how to make it so that its not the same line of code with one word changed. An example below :

Expand|Select|Wrap|Line Numbers
  1.     void getInput()
  2.     {
  3.         System.out.print("Enter Australia player name: ");
  4.         australiaPlayerName = input.next();
  5.         System.out.print("Enter Africa player name: ");
  6.         africaPlayerName = input.next();
  7.     }
  8.  
How would I shorten that method so that I'm not repeating code?
Mar 18 '08 #1
1 986
JosAH
11,448 Expert 8TB
Hello all, one of the conditions for my assessment is that I shouldnt't be re-using code that is similar/same as previously used in my program. I've got "re-used" code everywhere, but not sure how to make it so that its not the same line of code with one word changed. An example below :

Expand|Select|Wrap|Line Numbers
  1.     void getInput()
  2.     {
  3.         System.out.print("Enter Australia player name: ");
  4.         australiaPlayerName = input.next();
  5.         System.out.print("Enter Africa player name: ");
  6.         africaPlayerName = input.next();
  7.     }
  8.  
How would I shorten that method so that I'm not repeating code?
Personally I would split that method in two:

Expand|Select|Wrap|Line Numbers
  1. String prompt(String prompt, Scanner input) {
  2.    System.out.print(prompt+": ");
  3.    return input.next();
  4.  
now you can do this:

Expand|Select|Wrap|Line Numbers
  1. Scanner input= ...;
  2. String australianPlayerName= prompt("Enter Australia player name", input);
  3. String africaPlayerName= prompt("Enter Africa player name", input);
  4. String vegetable= prompt("Type a vegetable you like", input);
  5. // etc. etc.
  6.  
So you can properly reuse the method without any dependency on players or
whatever.

kind regards,

Jos
Mar 18 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.