How to avoid code repetition initializing final properties? - java

public class Code{
//many properties
//...
final String NEWLINE;// ohh a final property!
void creation() //this method is for avoid repetition of code
{
//final initialization can't be put here =(
Source= new StringBuffer();
//many other commons new's ..
//...
}
Code()
{
NEWLINE = System.getProperty("line.separator");
creation();
}
Code(String name, int numberr)
{
NEWLINE = System.getProperty("line.separator");
creation();
name=new Someting(name);
number = new Magic(number);
}
}

Here is your code with 4 different ways of initializing final variables.
inline
anonymous initializer block
initialized in the constructor
calling the default constructor explicitly
The resulting output is shown below.
public class Code {
// many properties
private String name;
private String number;
// ...
// 1.
final String NEWLINE_1 = "1" + System.getProperty("line.separator");
final String NEWLINE_2;
final String NEWLINE_3;
// 2.
{
System.out.println("initializer block invoked before Constructor");
NEWLINE_2 = "2" + System.getProperty("line.separator");
// final initialization CAN be put here =(
// Source = new StringBuffer();
// many other commons new's ..
// ...
}
Code() {
System.out.println("default constructor");
// NEWLINE_1 = "error"; can't do this
// NEWLINE_2 = "error"; can't do this
// 3.
NEWLINE_3 = "3" + System.getProperty("line.separator");
}
Code(String name, int number) {
// 4.
this();
System.out.println("constructor(name, number)");
name = new String("Someting(name)");
this.number = new String("Magic(number)");
}
public static void main(String[] args) {
Code code_1 = new Code();
System.out.println(code_1.NEWLINE_1 + ":" + code_1.NEWLINE_2 + ":" + code_1.NEWLINE_3);
Code code_2 = new Code("crowne", 2);
System.out.println(code_2.NEWLINE_1 + ":" + code_2.NEWLINE_2 + ":" + code_2.NEWLINE_3);
}
}
initializer block invoked before Constructor
default constructor
1
:2
:3
initializer block invoked before Constructor
default constructor
constructor(name, number)
1
:2
:3

All initializers are added by the compiler to the beginning of each constructor. This includes:
instance variable initialization
initialization blocks { .. }
So you don't have to include this everywhere just place it either as an instance-variable initialization:
private final String NEWLINE = System.getProperty("line.separator");
or in initialization block:
{
NEWLINE = System.getProperty("line.separator");
}
Of course, in this precise example, you should make the field static.

Just do:
final String NEWLINE = System.getProperty("line.separator");
See: JLS 8.3.2. Initialization of Fields.
See also: JLS 12.5 Creation of New Class Instances for execution order.

If they are being initialized the same way every time, you can put the code outside of the constructors. Java allows you to do:
final String NEWLINE = System.getProperty("line.separator");
You can also have all constructors other than the no-argument one call the no-argument constructor. For example:
Code(String name, int number)
{
this();
name=new Someting(name);
number = new Magic(number);
}

One more, if the initialization is complex and you must do it during construction, provide a static method the returns the result, as in:
Code()
{
NEWLINE = newLineValue();
creation();
}
Code(String name, int number)
{
NEWLINE = newLineValue();
creation();
name = new Something(name);
number = new Magic(number);
}
private static String newLineValue()
{
return System.getProperty("line.separator");
}
In this case, newLineValue() is trivial so I wouldn't use it here, but if this actually had a significant amount of work then it could be useful. You can also pass in parameters from the constructor.

Related

Java: pass in a string and store it

How would I create a method that has the input of a string and output of all the strings that were input into it like a super string?
for example in the main class:
a= "fido"
b= "rufus"
c= "dog"
superString(a);
superString(b);
superString(c);
System.out.println(superString()); should be "fidorufusdog"
so far I have=
public static String superString (String sb) {
StringBuilder ssb = new StringBuilder(32);
ssb = ssb.append(sb);
return ssb.toString();
}
My code below is what I am working on for a stock simulator:
public class Operators {
public static void operate(double price, double low, String company){
double percent = (price/low-1)*100;
double rpercent = Math.round(percent * 100.0) / 100.0;
StringBuilder sb = new StringBuilder(32);
if(rpercent <= 10) {
sb.append(company + " is trading at:");
sb.append("the current price is: " + price);
sb.append("the 52 week low is: " + low);
sb.append("percent of 52 week low is: " + rpercent);
}
}
}
The operate method is called in a for loop in my main method 506 times and I would like to take all 506 sb string and create a super string of all the results
I hope I do not underestimate the depth of the question or have your question wrong, but to me it sounds like you are asking for the static keyword?
class SomeClass {
static StringBuilder accumulator = new StringBuilder();
public String superString (String sb) {
SomeClass.accumulator.append(sb);
return ssb.toString();
}
}
This is simple usecase of the Java static keyword. Since accumulator is declared static there will be a single instance of the variable. And this single instance will be accessed by instances of the class SomeClass. For example:
SomeClass a;
SomeClass b;
a.superString("aaa");
b.superString("bbb");
// now accumulator.toString() returns "aaabbb"
Declare that string builder as the static member containing class and initialize it only once
static StringBuilder ssb;
public static String superString (String sb) {
if(ssb == null)
ssb = new StringBuilder(32);
ssb = ssb.append(sb);
return ssb.toString();
}
For this kind of probelm you've two choice :
Simple : create a static variable in the Java class, and manipulate it.
improve : create a design model which support your need.
Ex 1 :
public class Operators {
private static String text ="";
public static String superString(String sb) {
if (sb != null) {
text = text.concat(sb);
}
return text;
}
}
Ex 2 : you can use a Collecion or a List of strings.
This is poor OOP design. You would be much better off creating a class for Stock objects and overriding toString in the Stock class(or creating some other simple output method). Then add each instance of Stock to an array and call the each object's toString (or other output method you defined).

How to create two object reference variables in seperate classes in java

I have a class called AgendaFunctions, a class called Main, and a class called ReadFiles. Main has a reference variable for Agenda Functions and ReadFiles. AgendaFunctions has an array of reference variables. I have code to instantiate the array, but i need to instantiate it from ReadFiles. If I instantiate it from main, it works fine. But if i call the method from ReadFiles, it doesn't work. I get a java.lang.NullPointerException error.
Here is the code for Main:
public class Main {
public static void main(String[] args) throws Exception {
ReadFiles fw = new ReadFiles(); fw.read();
agendafunctions link = new agendafunctions();
AgendaFunctions:
public class agendafunctions {
int amount = 20;
public void setamount(int data) {
}
static String input = "true";
agendaitem item[] = new agendaitem[amount];
int counter = 0;
public void instantiate() {
item[1] = new agendaitem();
item[2] = new agendaitem();
item[3] = new agendaitem();
}
public void createobject(String name, Boolean complete, String Comments) {
item[counter].name = name;
item[counter].complete = complete;
item[counter].comments = Comments;
counter++;
}
ReadFiles:
public class ReadFiles {
public void read() throws IOException {
agendafunctions af = new agendafunctions(); af.instantiate();
int readitem = 1;
BufferedReader data = new BufferedReader(new FileReader("C:/Agenda Dev Docs/data.txt"));
int filestoread = Integer.parseInt(data.readLine());
while (readitem <= filestoread) {
String name;
String complete;
String comments = null;
String line;
Boolean bc = null;
BufferedReader read = new BufferedReader(new FileReader("C:/Agenda Dev Docs/"+readitem+".txt"));
readitem++;
name = read.readLine();
complete = read.readLine();
comments = "";
while((line = read.readLine()) != null) {
comments = comments + line;
}
if(complete.equals("Complete")) {
bc = true;
} else if(complete.equals("Incomplete")) {
bc = false;
}
af.createobject(name, bc, comments);
}
}
If I call the method instantiate from ReadFiles, i get a NullPointerException. If I call it from Main, everything works. But further development requires me to call the method from ReadFiles. How would i fix this? Thanks.
You have this
int counter = 0;
public void instantiate() {
item[1] = new agendaitem();
item[2] = new agendaitem();
item[3] = new agendaitem();
}
public void createobject(String name, Boolean complete, String Comments) {
item[counter].name = name;
item[counter].complete = complete;
item[counter].comments = Comments;
counter++;
}
where item is an array with 20 indices, ie 20 elements, but your instantiate method only initializes elements at indices 1 through 3, missing 0 and 4 through 19.
In your ReadFiles#read() method, you do
agendafunctions af = new agendafunctions(); af.instantiate();
which instantiates one agendafunctions object and calls instantiate() which initializes elements at indices 1, 2, and 3 in your item array.
You then loop in the while and call
af.createobject(name, bc, comments);
a bunch of times on the same object.
The reason it fails the first time is because you haven't initialized the element in item at index 0. Arrays always start at 0, not 1.
Another cause for error (which you'll see if you fix the above problem), is that if your while loop loops more than 3 times, you'll again get a bunch of NullPointerExceptions because counter keeps growing, but you aren't initializing the elements that you'll then try to access at counter index.
item[counter].name = name; // if counter is 4, you'll get NullPointerException because
// the element there hasn't been initialized as 'new agendaitem();'
#SotiriosDelimanolis explained why you are getting NPE, to fix that you just can get rid of the instantiate() method and add item[counter] = new agendaitem(); as first line in your createobject method. Also you have to make sure your while loop does not exceed amount. To avoid these worries better use an ArrayList for agendaitem

how to make more than condition in toString method

I want to list all names that end with "Reda" and ignore case sensitivity, I have tried the condition in the toString method at the bottom, but it would not print any thing.
public class Customer {
public static void main(String[] args) throws IOException {
File a = new File("customer.txt");
FileWriter v = new FileWriter(a);
BufferedWriter b = new BufferedWriter(v);
PrintWriter p = new PrintWriter(b);
human Iman = new human("Iman", 5000);
human Nour = new human("Nour", 3500);
human Redah = new human("Redah", 0);
human iman = new human("iman", 200);
human MohamedREDA = new human("MohamedREDA", 3000);
human Mohamed_Redah = new human("Mohamed Redah", 2000);
human[] h = new human[6];
h[0] = Iman;
h[1] = Nour;
h[2] = Redah;
h[3] = iman;
h[4] = MohamedREDA;
h[5] = Mohamed_Redah;
p.println(Iman);
p.println(Nour);
p.println(Redah);
p.println(iman);
p.println(MohamedREDA);
p.println(Mohamed_Redah);
p.flush();
}
}
class human {
public String name;
public double balance;
public human(String n, double b) {
this.balance = b;
this.name = n;
}
#Override
public String toString() {
if (name.equalsIgnoreCase("Reda") && (name.equalsIgnoreCase("Reda"))) {
return name + " " + balance;
} else
return " ";
}
}
Please avoid putting condition in toString method. Remove the condition there
public String toString() {
return name + " " + balance;
}
and change your logic in Customer class
human[] h = new human[6];
h[0] = Iman;
h[1] = Nour;
h[2] = Redah;
h[3] = iman;
h[4] = MohamedREDA;
h[5] = Mohamed_Redah;
for (int i = 0; i < h.length; i++) {
if (h[i].name.toLowerCase().endsWith("reda")) { // condition here
p.println(h[i]);
}
}
And make use of loops do not duplicate the lines of code.Every where you are manually writing the lines.
Check Java String class and use required methods to add condition.
String redahname = ("Redah").toLowerCase(); //put your h[0] instead of ("Redah")
if(name.endsWith("redah")){ //IMPORTANT TO BE IN LOWER CASE, (it is case insenitive this way)
//your code here if it ends with redag
System.out.println(redahname);
} //if it does not end with "redah" it wont out print it!
You can use this, but can you please explain your question more? What exactly do you need?
try this
#Override
public String toString() {
if (name.toLowerCase().endsWith("reda"))) {
return name + " " + balance;
} else
return " ";
}
String.equals() is not what you want as you're looking for strings which ends with "Reda" instead of those equal to "Reda". Using String.match or String.endsWith together with String.toLowerCase will do this for you. The following is the example of String.match:
public class Reda {
public static void main(String[] args) {
String[] names = {"Iman", "MohamedREDA", "Mohamed Redah", "reda"};
for (String name : names) {
// the input to matches is a regular expression.
// . stands for any character, * stands for may repeating any times
// [Rr] stands for either R or r.
if (name.matches(".*[Rr][Ee][Dd][Aa]")) {
System.out.println(name);
}
}
}
}
and its output:
MohamedREDA
reda
and here is the solution using endsWith and toLowerCase:
public class Reda {
public static void main(String[] args) {
String[] names = {"Iman", "MohamedREDA", "Mohamed Redah", "reda"};
for (String name : names) {
if (name.toLowerCase().endsWith("reda")) {
System.out.println(name);
}
}
}
}
and its output:
MohamedREDA
reda
You shouldn't put such condition in toString() method cause, it's not properly put business application logic in this method.
toString() is the string representation of an object.
What you can do, is putting the condition before calling the toString() , or making a helper method for this.
private boolean endsWithIgnoringCase(String other){
return this.name.toLowerCase().endsWith(other.toLowerCase());
}
None of your humans are called, ignoring case, Reda, so your observation of no names printed is the manifestation of properly working logic.
Your condition is redundant: you perform the same test twice:
name.equalsIgnoreCase("Reda") && (name.equalsIgnoreCase("Reda"))
If you need to match only the string ending, you should employ a regular expression:
name.matches("(?i).*reda")
toString is a general-purpose method defined for all objects. Using it the way you do, baking in the business logic for just one special use case, cannot be correct. You must rewrite the code so that toString uniformly returns a string representation of the object.

java,returning null value from method

I need to return finalString value for input operator name.
where,internalPrestring is fixed for specific operator,internalDigit would be retrieved from getting operator name.then all of'em would be added to finalString.
but it is giving null, i can't understand the problem
import java.io.*;
import java.lang.*;
class CallManager
{
public static final String postString = "#";
StringBuilder stringBuilder;
String internalPreString;
String preString;
String middleString;
String finalString;
String operatorName;
int internalDigit;
//needs to set oprator name
public void setOperatorName( String getMeFromPreferences)
{
operatorName = getMeFromPreferences;
System.out.println("I got it " + operatorName);
}
//afeter having operator name need to set inrernal digit for each operator
public void setOperatorBasedInternalDigit(int getIntegerForOperator)
{
internalDigit = getIntegerForOperator;
System.out.println("I got it too " + internalDigit);
}
//it needs to get string from ocr
public void setString( String getMeFromOCR )
{
middleString = getMeFromOCR;
}
//preString creator for differnet operator
public String getOperatorBasedPreString(String operatorName)
{
if(operatorName.equals("Airtel"))
internalPreString = "787";
else if(operatorName.equals("Banglalink"))
internalPreString = "123";
else if(operatorName.equals("Grameen"))
internalPreString = "555";
else if(operatorName.equals("Robi"))
internalPreString = "111";
else if(operatorName.equals("TeleTalk"))
internalPreString = "151";
stringBuilder.append("*").append(internalPreString).append("*");
preString = stringBuilder.toString();
return preString;
}
//get operator name and retrive midlle string's digit size from it
public int getOperatorBasedInternalDigit( String operatorName)
{
if(operatorName.matches("^Airtel | Grameen | Robi$"))
internalDigit = 16;
else if(operatorName.matches("^Banglalink$"))
internalDigit = 14;
else if(operatorName.matches("^TeleTalk$"))
internalDigit = 13;
return internalDigit;
}
//check operator-based digit number with input middle string as a number then retrive final string
public String getString( String toBeInserted, int inetrnalDigit)
{
if(toBeInserted.length() == internalDigit)
{
int counter = 0;
char [] insertHere = new char[internalDigit];
for(int verifier = 0; verifier < internalDigit; verifier ++)
{
insertHere[verifier] = toBeInserted.charAt(verifier);
if(!Character.isDigit(insertHere[verifier]))
break;
counter ++;
}
if(counter == internalDigit)
{
stringBuilder.append(preString).append(toBeInserted).append(postString);
finalString = stringBuilder.toString();
//to see what i've got finally as input for using this call manager method.it would be removed too
System.out.println(finalString);
return finalString;
}
else
{
//this printing could be used in main program
System.out.println("number is less or more than desired ..... INVALID SCAN");
System.out.println(middleString);
//here i will call the method for scan the card again
//
//
return middleString;
}
}
else
{
//this printing could be used in main program
System.out.println("number is less or more than desired ..... INVALID SCAN");
System.out.println(middleString);
//here i will call the method for scan the card again
//
//
return middleString;
}
}
}
//tester class that CallManager works rightly or not
class CallManagerDemo
{
public static void main(String args[]) throws IOException
{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name of Operator");
CallManager clm = new CallManager();
clm.setOperatorName("Banglalink");
System.out.println(clm.internalPreString);
System.out.println(clm.preString);
}
}
You are having only four lines that deals with your CallManager class:
CallManager clm = new CallManager();
clm.setOperatorName("Banglalink");
System.out.println(clm.internalPreString);
System.out.println(clm.preString);
The reason why you are getting null :
You are using a default constructor right and there processing is
done in it. So this is not a problem
Now on next line you call setOperator method which has this code:
public void setOperatorName( String getMeFromPreferences)
{
operatorName = getMeFromPreferences;
System.out.println("I got it " + operatorName);
}
Now here you are only setting thw variable operatorName and nothing else. So all other variables are null as you not doing any processing or something that will initialize them to something.
So when you print clm.internalPreString and clm.preString you get null as they are not initialized. But try printing clm.operatorName and it will print the operator name that you passed and was initialzed inside your method setOperatorName.
So as you have defined so many method inside your class, use them so that all the variables are set as per your logic
UPDATE
public void setOperatorName( String getMeFromPreferences)
{
operatorName = getMeFromPreferences;
//call any methods for example and use the values returned from the method by storing it inside a variable
String mystring = getOperatorBasedPreString(String operatorName)
}
You have never set the values for the variables for those you are getting the NULL value.The terms get/set must be used where an attribute is accessed directly.Read Java Programming Style GuideLines For more clarity.Use appropriate getters and setter for getting and setting the value like you have done for operatorName.
Don't you think that you should call any of the function instead of the string variables using object.
You are just calling one function that is
public void setOperatorName(String getMeFromPreferences) {
operatorName = getMeFromPreferences;
System.out.println("I got it " + operatorName);
}
You are calling default constructor with out any variable setting there,
You had not initialized any String you are calling form object.
I think you should call any of the function e-g
public int getOperatorBasedInternalDigit(String operatorName)
OR
public String getString(String toBeInserted, int inetrnalDigit)
Then you will get some string as you are expecting ...
Hope this will help you.

Final variable may already have been assigned

I am writing a program that has two final variables that I wish to use, I need these to be set at the time that I actually run the class because they are likely to be different each instance.
I have the initialization the same as any other class variable I wish to use where I initialize a name and type but not a value.
public final String filename, filepath;
In the Constructor I set the values as follows
public myClass(String value) {
this.filename = value;
this.filepath = anotherPartOfValue;
}
When I do this, I get a warning that "The final field [x] may have already been assigned"
Is there a way to avoid this warning and still keep the final state and set the value in the constructor?
I am using eclipse btw.
Edit:
This is the exact code that gives me the error
import java.io.*;
public class Dirt {
private String[] tables;
private int numTables;
private final String filename, filepath;
public Dirt(String file) {
this.tables = new String[0];
this.numTables = 0;
for (int i = file.length(); i < 0; i--) {
if (file.charAt(i) == '/') {
this.filename = file.substring(i);
this.filepath = file.substring(1, i-1);
}
}
}
}
The problem is that you are assigning to the final variables in a loop. There's nothing to prevent the loop from looping more than once and the if condition being satisfied more than once. (What happens if there are two '/' characters in file? Or none?)
One way around this is to use temporary String variables in the constructor and then assign them to filename and filepath at the end:
public Dirt(String file) {
this.tables = new String[0];
this.numTables = 0;
String name = null;
String path = null;
for (int i = file.length(); i < 0; i--) {
if (file.charAt(i) == '/') {
name = file.substring(i);
path = file.substring(1, i-1);
// need a break here?
}
}
this.filename = name;
this.filepath = path;
}
It's ugly, but it's a straightforward way to know for sure that filename and filepath will definitely be assigned and definitely be assigned only once.
Completely compileable and runnable:
public static void main(String[] args) {
Foo f = new Foo("abc");
System.out.println(f.filename);
System.out.println(f.filepath);
}
static class Foo {
public final String filename, filepath;
public Foo(String value) {
this.filename = value;
this.filepath = value.substring(1);
}
}
And the "warning" you mention isn't just a warning. It's an error. But it's not coming from here.
Edit: Your newly posted code has the assignments in a loop. By definition, statements in a loop can execute multiple times, and therefore you have the potential for assigning the final variables multiple times.

Categories