• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How can I call doOperation method and handlingException method?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Saloon Keeper
Posts: 1277
38
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

First off, it seems that you intended this to be a constructor for the class Activity:



Three problems: First, we don't mark constructors as static, secondly, you forget to take Operator as a parameter.
Lastly, your Activity class doesn't have a data member of any type named operator yet.

From the rest of your code, it appears you mean to have the operator member be a String.

If you do, you should be using .equals() to test value equality rather than ==, which is just used on Certification Exams for tricky questions to see if you understand String Pooling behavior.
Don't ever use == to compare two strings for equality in Real Life.

 
Jesse Silverman
Saloon Keeper
Posts: 1277
38
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, please post again to the same thread instead of editing your post if anyone has already replied to your post, otherwise it is confusing to everyone reading the thread seeing your edited version instead of the one that the replies were actually commenting on.

Your solution will evolve towards correctness thru-out the thread.  This is normal and good.
 
Akash bhamri
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, thanks I got your point but the constructor initialized and terminate the code and I want it to go handlingException and doOperation method how can I do that so that after initialized it will check the condition which is I provided in those methods?
 
Marshal
Posts: 74004
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:Also, please post again to the same thread instead of editing your post . . .

I am refuseing the edit.


Also, you will never get the == operator to work on object references. Use the equals() method, as Jesse has already said. The same applies to !=.
 
Akash bhamri
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell Ritchie Thanku but the constructor initialized and terminate the code and I want it to go handlingException and doOperation method how can I do that so that after initialized it will check the condition which is I provided in those methods?
 
Campbell Ritchie
Marshal
Posts: 74004
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch (again).

Thank you for trying the code button, but the post needed a little correction; I moved the [/code] part to the end, and it looks a lot better now

Unfortunately, you are mixing indentation conventions. Either put all the {s at the end of the line, or put them all on a line by themselves. I can't follow the organisation of your code. Please put an empty line (enter key 2×) between successive methods. I think you will find out you haven't matched the { and }; the compiler won't be able to understand the code and you will get strange error messages.
Why are you catching NullPointerException, and then trying to do the same as you would for any other exception? What sort of exception do you expect from that code? The only exception I can envisage is if you pass null, in which case it is probably better to allow the exception to propagate. It is never a good idea to have an exception thrown and caught in the same method.
Why have you got the Scanner on line 45, because you are not using it?
Please work out the different possibilities for the boolean expression on line 17, and whether the expression evaluates to true or false.
Staff note (Jesse Silverman) :

For questions like this, it strikes me that we should likely confirm whether the poster is a student in a course, trying to do something entirely on their own, etc.

If it is the former, we might suggest re-reading their notes or re-watching certain lessons, if it is the latter, we might refer them to the Java Tutorials or other materials.

In this case, it was unclear whether they jumped into trying to code when they should have been doing one of those things, or were just not used to how carefully you need to apply the knowledge you do have towards implementing something, i.e. programming is very intolerant when you don't pay attention to details.

Like many people, I tend towards jumping into showing "Here are three things you got wrong and how to fix them:" mode, but figuring out where they are coming from and what their current requirements are might serve some better.

In particular, the ideal responses for someone who is trying to complete a class assignment versus someone who is in their early steps of "self-taught Java Programmer" might look a bit different.

 
Akash bhamri
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Description
In Java, we can use more than one catch block with the try block. Generally, multiple catch block is used to handle different types of exceptions, which means each catch block is used to handle different types of exceptions.

If you use multiple catch blocks for the same type of exception, then it will give you a compile-time error because Java does not allow you to use multiple catch block for the same type of exception. A catch block is always preceded by the try block.

Write a program to demonstrate Multiple Exceptions.


You have to implement the following methods under Source class:

handleException (Activity a) - In this function you have to check for exceptions.
doOperation (Activity a) - this function should implement the string operation between string1 and string2 for the operator operator.
If operator = '+', concat the strings string1 and string2.
e.g. for string1 = "hello" and string2 = "world", then result = "helloworld"
If operator = '-', replace the contents of string2 in string1 with empty string.
e.g. If string1 = "helloworld" and string2 = "world", then result = "hello"
Tasks:

In the function handleException (Activity a):

Check that the value of either string1 or string2 variable is null, then throw appropriate exception for NullPointerException and return "Null values found".
Check if the value of operator variable is not equal to these string operators ((+ or -) using logical AND operator. If the condition is true then throw and return the default exception with the Operator as the return message.
If no exception is found return "No Exception Found".
In the function doOperation (Activity a):

perform the string operations, using switch statement and return the correct value.
 
Campbell Ritchie
Marshal
Posts: 74004
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this discussion is a continuation of your previous thread, so I shall merge the discussions.

That explains why you have the strange range of methods; I don't think most of us would design methods like that.
 
Campbell Ritchie
Marshal
Posts: 74004
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot see that you are following the instructions, I am afraid. It tells you what to return from the method, even if we wouldn't have written such a method there, and it tells you a bit about the if in line 17.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic