Results 1 to 6 of 6
Thread: Public static method error
- 04-29-2009, 09:33 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
Public static method error
Hi, I'm really confused when i make print report method i'm getting errors if I take both methods out then it works fine here's my code:
like if I take out:
public void buildInstaces() and public void printReport() and let it run regular code it works fine no errors at all .. please help me solve this problem thanks.
Java Code:public class BookTest { static Book bookArray[] = new Book[4]; public static void main(String args[]) { //Book bookArray[] = new Book[4]; // i will call all the methods here } public static void buildInstances() { String dataArray [][] = {{"Where's my car", "Aston Kutcher","777","ORiely","Dallas","5.95","Fiction","123","456"}, {"Earthquakes","ChuckBerry","435","LABooks","LA","75.00","NonFiction"}, {"Doom","The Rock","918","Sans Publishing","LA","12.50","Fiction","1233","2323"}, {"Universal Studios","Walt","987","Dell","Houston","29.90","NonFiction"}}; }//build instances public void printReport() { //print report for (int i=0; i<bookArray.length; i++) { if (dataArray[i][6].equals("Fiction")) bookArray[i]=new Fiction (dataArray[i][0],dataArray[i][1], Integer.parseInt(dataArray[i][2]), new Publisher(dataArray[i][3],dataArray[i][4]), Double.parseDouble(dataArray[i][5]),i, new Background(Integer.parseInt(dataArray[i][7]), dataArray[i][8])); if (dataArray[i][6].equals("NonFiction")) bookArray[i]= new NonFiction(dataArray[i][0],dataArray[i][1], Integer.parseInt(dataArray[i][2]), new Publisher(dataArray[i][3],dataArray[i][4]), Double.parseDouble(dataArray[i][5]),"History"); }// end of FOR Loop for (int j=0; j<4;j++) { bookArray[j].calculateTotalCharge(Double.parseDouble(dataArray[j][5])+2*3); System.out.println(); System.out.println(); System.out.printf(bookArray[j].toString()); } //end of for with j }///print report // } //end of main } //end class BookTest
- 04-29-2009, 09:38 PM #2
Please post the errors you're getting. Also, the following isn't doing anything:
Luck,Java Code:public static void main(String args[]) { //Book bookArray[] = new Book[4]; // i will call all the methods here }
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-29-2009, 09:53 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
ok the error is : cannot find symbol variable dataArray
also i know the main method is not going to do anything but i'm just trying to run as it is @ this moment
i can add in main method like this:
buildInstances();
printReport();
- 04-29-2009, 10:10 PM #4
In your buildInstances() method you create String dataArray [][] = but it is only a local variable for that method... So when you call it in your printReport() method, it is no where to be found...
You need to make it static like your bookArray[]...Who Cares... As Long As It Works...
- 04-29-2009, 10:16 PM #5
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
ok when i type like this:
static String dataArray[][] = ......
then i compile it says : illegal start of expression
- 04-29-2009, 11:10 PM #6
You need to do it in the same place as well... Like so:
Java Code:static Book bookArray[] = new Book[4]; static String dataArray [][]; public static void main(String args[]) { //Book bookArray[] = new Book[4]; // i will call all the methods here } public static void buildInstances() { dataArray [][] = {{"Where's my car", "Aston Kutcher","777","ORiely","Dallas","5.95","Fiction","123","456"}, {"Earthquakes","ChuckBerry","435","LABooks","LA","75.00","NonFiction"}, {"Doom","The Rock","918","Sans Publishing","LA","12.50","Fiction","1233","2323"}, {"Universal Studios","Walt","987","Dell","Houston","29.90","NonFiction"}}; }//build instances
.Who Cares... As Long As It Works...
Similar Threads
-
error: non-static method newAnimal() cannot be referenced
By alpdog14 in forum New To JavaReplies: 3Last Post: 04-11-2009, 09:14 PM -
static method sparks error on overriding non-static method
By MuslimCoder in forum New To JavaReplies: 1Last Post: 02-10-2009, 10:03 AM -
Error: LengthCharAt.java:3: ';' expected public static void main (String[] args)
By antgaudi in forum New To JavaReplies: 9Last Post: 11-22-2008, 11:03 PM -
Non-Static method in static context error
By wizmang in forum New To JavaReplies: 4Last Post: 04-24-2008, 08:51 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks