-1

I need to produce a BogOff class with a taxTables method that initialises an array of integers with increasing incomes, at least one in each of the income ranges shown in the table above, and uses a TaxChart object to produce a thin bar chart and the corresponding table.

Basically, I've done all of this, and it's working, however for the life of me, I cannot move it into a method named taxTable, I know this seems ridiculously basic considering what I have already written.

public class BogOff {



public static void main (String[] args) {

    int[] Values = { 25, 50, 100, 125, 150, 175, 200, 225, 250, 275, 300,
            325, 350, 375, 400, 425, 450, 500, 550 };
    /*initialized my array of integers to used as the values of income 
     I will input*/

    TaxChart graph = new TaxChart(Values); //calling my graph
    graph.Initialize();
    graph.Draw();
    graph.PrintTable();
    //my graph uses the 2 methods, Initialize and Draw from TaxChart to 
    //render the graph PrintTable is used to output my income, tax and 
    //income remaining
    }


}
Ben Bowen
  • 174
  • 1
  • 13
  • 1
    what did you try, and what were the errors shown? – Silly Freak Nov 06 '14 at 16:23
  • I guess you forgot to make the method `static` or to call it on a `BogOff` instance. A method like `public static void taxTable() { ... }` should work. – Tom Nov 06 '14 at 16:28
  • @SillyFreak I tried to rewrite it just as a method without the main, which of course doesn't work as I am running from this class, therefore needing the main method, I also tried making another method underneath the main method, which wouldn't run because there was no content in the main method. I guess what I'm looking for is a way to call a method containing the code above, inside the main method. – Ben Bowen Nov 06 '14 at 16:30
  • 1
    duplicate of this: http://stackoverflow.com/questions/4922145/non-static-method-cannot-be-referenced-from-a-static-context-error In the future, google your error messages first. This is one of the most common ones you'll ever find. – Jeroen Vannevel Nov 06 '14 at 16:31
  • That is a description that might lead us to any plausible or unplausible error. You really need to show us the exact code and error if you want us to help you. – Silly Freak Nov 06 '14 at 16:34

1 Answers1

1
public class BogOff {



public static void main (String[] args) {
    initAndDrawGraph();
}

public static void initAndDrawGraph() {

    int[] Values = { 25, 50, 100, 125, 150, 175, 200, 225, 250, 275, 300,
            325, 350, 375, 400, 425, 450, 500, 550 };
    /*initialized my array of integers to used as the values of income 
     I will input*/

    TaxChart graph = new TaxChart(Values); //calling my graph
    graph.Initialize();
    graph.Draw();
    graph.PrintTable();
    //my graph uses the 2 methods, Initialize and Draw from TaxChart to 
    //render the graph PrintTable is used to output my income, tax and 
    //income remaining
    }


}

Since main() is static, the other method called from main will either need to be static, or it can be a non-static method but called from an instance of BogOff, not just from main in BogOff.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • 2
    You have the rep. Vote to close it as a duplicate instead of providing the 5000th generic answer to this problem. There is no gain from spreading all this information and reiterating it. – Jeroen Vannevel Nov 06 '14 at 16:35
  • 2
    The the only other answer referenced so far talks about invoking an instance method in a static way from another class. To you and me and other people with experience in Java, that is directly related to the OP's question and this answer. But to someone not already familiar with static v instance, it might be difficult to extract what he needs out of that answer. He didn't quote an error message, his IDE might show something different. Feel free to downvote -- I know some enjoy that -- but I don't think this reiteration deserves it. – arcy Nov 06 '14 at 18:06
  • @arcy your answer was exactly what I needed, I was just having a bit of a derp moment and couldn't think how to get around it, the error message was that I couldn't invoke without a main method, I've fixed my work now with what you suggested, so thanks a lot. – Ben Bowen Nov 06 '14 at 23:43
  • 1
    @BenBowen glad to help; suggest, in future SO questions, that instead of posting the original code and asking how to get something done, that you post the (smallest possible) code with the problem and the copy-pasted error message. Questions tend to get more attention when they are specific to the problem exhibited, when they have code that WE can copy and see the error, and when they include the actual error (not a description or a paraphrase of the error). – arcy Nov 07 '14 at 16:16
  • @arcy From my point of view, the question is lacking the problem, and thus is not answerable. That the problem was static vs instance was your guess, and as Ben clarified in a comment, you actually got it wrong (the main method was missing). That is what makes it a bad answer: you did not answer the question, because the question as it is has no answer. – Silly Freak Nov 09 '14 at 11:49
  • 2
    @SillyFreak, when inexperienced Java programmers are facing some kinds of fundamentals, it is difficult for them to ask clear questions because they don't know the terminology and principles they're asking about. They can still ask better questions than this, and I gave the OP suggestions about that. But your assertions that it is "not answerable", and "has no answer", and that I "got it wrong" and wrote a "bad answer", looks silly given that the OP said my answer (without any revisions) is "exactly what [he] needed". – arcy Nov 09 '14 at 12:48
  • @arcy I don't want to prolong this argument, clearly we can both move on. But I have to mention that what the OP got from you was working code, which did of course help him, but doesn't necessarily make it a good answer. In the end, the check mark shows that the answer was helpful to the OP, and the downvote shows it wasn't considered particularly good; I think that's an appropriate outcome here. – Silly Freak Nov 09 '14 at 22:50
  • 1
    Obviously you think it appropriate to downvote the answer, but you cannot articulate what is wrong with it. When you tried, you said that are just plain illogical. I guessed, correctly and without seeing his 'clarifying' comment, that he had not put together all the rules about static methods and main methods, and provided him "exactly what [he] needed". This 'clarifying' comment gave the error message, but that wasn't all he needed. So, would anyone care to tell me just what is wrong with the answer? – arcy Nov 11 '14 at 02:49