Mohammed Azeem

Ranch Hand
+ Follow
since Aug 17, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
3
In last 30 days
0
Total given
0
Likes
Total received
7
Received in last 30 days
1
Total given
3
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mohammed Azeem

Lou Manners wrote:
Actually I overlooked this, although I'm not sure I understand the "uses a bean with the same method" part



Hello Lou, I'm not sure if your'e familiar with Javabeans - they're like regular Java classes, with private fields, getters and setters, other methods also, whose fields can be returned to placeholders in a JSP (Java Server Page - an evolution of an HTML document) and also methods can be called aswell. So I was simulating the behaviour calculatePayment() method of the javabean as a regular app - not web app.
1 week ago
Sorry, I seem to have solved the problem.

I had forgot some necessary lines of code - it seems the class field loanAmount was not set after all.

Sorry. It is solved.
1 week ago
I have class variables String loanAmount, String term and String rate.

They are set from console keyboard input and I can confirm their values by printing them out:



However, when I try to convert these strings into double values in this method:



I am getting a NullPointerException, it seems the arguments of the Double.valueOf() method are null, but they shouldn't be - they are class variables and have been set.

I am actually debugging (by testind the code as a regualar Java app)  a web app that uses a bean with the same method.

Very frustrating, as usual there's probably something staring me in the face that I've missed. Any comments welcome!

1 week ago
Thankyou Ritichie and Jhonson.

All very good points/suggestions.

Yes, I'm very guilty of using System.out to handle error messages and one day it will come back to bite me.

I am vaguely aware that all exceptions decent from Java class Exception and this might cause problems. For example FileNotFoundException extends IOException and that catching the superclass will miss out those that descend from it where we want a specific action to occur.
2 months ago
I have the answer.

Sorry it was so obbvious - write the string to a file with the html extension
2 months ago
Sorry I forgot to ask my question:

So what might be an outline solution to turning the StringBuilder value (the entire html of the webpage into a Java HTML object?
2 months ago
I know I could easily obtain an HTML document using JSoup.
I'm not trying to re-invent the wheel but want to gain further insights.

  • So I have code which successfully uses HTTP "GET" method to obtain the html of the page.
    That html is stored as a StringBuilder.


  • 2 months ago
    Thank you Stephan,

    I'll read up on  ENUMS - I'm aware of them but have neglected them.
    Yes, I'm in the bad habit of declaring fields public - inexplicable really.
    I'll study your use of the Executor service .
    Apologies for the delay in my reply, I code as a hobby in the evenings after my day job.

    After much re-study I am now able to identify the faults in the original program and offer a solution that works.

    The weather program was a way of modelling inter-thread cooperation:
  • Two threads sharing the same data (the simplest data structure – a class with field variables and getters/setters)
  • Establsihing a happens – before relationship between two tasks. One thread’s execution to be contingent upon the second thread completing a required task.
    In the weather program, the weather cannot be printed until the decider has decided what it is going to be.


  • This requires the two threads to share the same monitor (synonymous to ‘lock’).
    The lock can be provided by any object, but a convenient object is an instance of Java Object, since it is only its lock we’re interested in.

    If like me, you are highly visual, I have attached a sort of flow diagram that shows two threads sharing the same lock in a happens – before relationship.
    I explain it as follows:

    The first thread acquires the lock and its task (the WeatherPrinter) checks whether a
    Boolean flag has been set by the second thread running the WeatherDecider

    If not:
  • It calls wait() allowing the second thread to acquire the monitor’s lock.
  • The second thread’s task (WeatherDecider) can then decide the weather and set the flag to show it has been decided.
    The task then issues a notify() on the monitor and releases the lock.
  • The first thread re-aquires the lock (remember it’s the same lock)
  • It sees that the weather flag is set and proceeds to print the weather.


  • If yes:
  • It reads the weather string and prints it.


  • Is the data in WeatherData, guarded from being changed by objects other than WeatherData?  I believer so.
    The object reference weatherData is declared final, meaning that it will always refer to a single unique WeatherData object.
    Therefore, passing the weatherData reference always passes the same object.

    Finally, thank you for taking an interest in the post. Maybe I should payback by responding to other people's posts. But I implore you gently - please make your code readable, it's half the battle !

    Class WeatherData


    Class WeatherPrinter


    Class WeatherDecider


    Main method class


    Typical output:


    Good Evening Paul,

    I have a horrible sense that I've made a logical mistake in this pattern of inter-thread communication.
    To all readers, I will look into it again tomorrow and promise a full answer.

    First thoughts on you remarks Paul: the intended flow control was as follows:

  • The PrintWeather and DecideTheWeather have access to shared data.  PrintWeather must not proceed until the shared data is written to. So, it waits for decideTheWeather to do so.
  • line 7: main method acquires lock for weatherPrinter by calling weather.printTheWeather() - a synchronized method
  • line 17: wait() - the main thread voluntarily suspends itself, thereby relinquishing the lock on shared data - the boolean flag weatherHasBeenDecided. It needs to release itself so that the second thread can access the flag
  • line 24 -25 in WeatherDecider:  the shared data has been set, the second thread notifies all other threads
  • now, that the main thread is sure that shared data has been set it resumes its run.

  • .
    Hello, I'm applying what I've learned about guarded blocks with a simple app.

    PrintWeather object waits until DecideTheWeather, which is running in a new thread, has notified all other threads.
    However I can't work out why the second thread is not executing.

    Class SharedWeatherData defines :
  • shared data: static String weather and static boolean hasTheWeatherBeenDecided


  • Class DecideTheWeather:
  • decides the weather and updates the weather and boolean flag in SharedWeatherDatashared


  • Class PrintWeather:
  • prints the weather if the weather has been decided


  • Class WeatherProgram contains the main method.










    Yes. Fits in with the behaviour.

    Thankyou for yout time.
    7 months ago
    I tried out this example from Oracle's The Java Tutorials: Connecting to a URL



    The output is:

    <html>
    <head><title>301 Moved Permanently</title></head>
    <body>
    <center><h1>301 Moved Permanently</h1></center>
    <hr><center></center>
    </body>
    </html>

    However if I change the URL to include www  so that line 15 is now:


    There is no output?

    It's bugging me. Insights most welcome.

    7 months ago
    So Tim Holloway, you were right.

    I have resolved this issue through my own experimenting and happening on this stackoverflow  post.

    App Specific Internal Storage
  • Every app has a data directory, :/data/app_package/ which is private to it only. The path to this is obtained by calling getDataDir() on the app's context object, i.e context.getDataDir().  The docs for android class Context say that the data directory should not be used directly
  • A sub-directory of the data directory is the files directory  (:/data/app_package/files/)  obtained by context.getFilesDir(). Official android docs/guides say this where files private should be stored
  • Database files are stored in subdirectory :/data/app_package/databases. This is where SQLiteOpenHelper. getWritableDatabase()  expects to find them.
  • However, creating a subdirectory  :data/app_package/databases/  programmatically fails and throws an IOException


  • My Solution
    After reading docs for android class Context  (the context is like the "environment" e.g. storage, device, memory, wifi etc within which the app is created:
  • Method   context.getDatabasePath(String name)  abstracts the creation of the databases directory and the database file of the specified name within that directory
  • The AssetCopier, class I defined in my experiments (see below) , therefore copies the database from assets folder into the above location
  • The log message (equivalent to System.out.println) confirms that the db file is copied and returns its file path.




  • Then in the main acitivity - which I use just as a starting point.



    Logcat output:

    2022-08-30 15:03:55.065 15697-15697/com.mo.copydbfromassets E/De-bug Message ::  File copied.
    2022-08-30 15:03:55.065 15697-15697/com.mo.copydbfromassets E/De-bug Message ::  the path to the file is :/data/user/0/com.mo.copydbfromassets/databases/testDB.db
    1 year ago
    Yes, sorry I forgot.

    The type of a class, even if it's abstract/interface, can be the type of all classes descended from it.

    Must have been stressing a bit. Couldn't see it.
    1 year ago