Jump to content

Java Eclipse Error HELP

RobertVanGilder

Hey guys, I'm getting the error "ClassHeader expected instead" on the first float statement. Does anyone know why this is happening?

 

Here's the code

package ArraysTest;String[] items = {"pencil","pen","sharpie","notebook A","notebook B",                  "eraser","tippex","usb stick","glue","tape"                 };               float[] prices = { 								   0.75 , 1.50 , 1.20 , 1.90 , 2.50 ,                   0.50 , 1.75 , 5.00 , 1.25 , 2.00  ;                 			     }{{  String name = "";  do    {    name = input("Type the name of an item");        float price = getPrice(name);        if(price > 0)        { output(name + " = " + price); }        else       { output(name + " was not found"); }          } 	  	  while(name.length() > 0);             // type nothing to quit    }float getPrice(String name)           // search for name, return price{                                     // the price is not case-sensitive  for(int x=0; x < prices.length; x = x+1)  {    if(items[x].equalsIgnoreCase(name))    // not cases-sensitive    {      return prices[x];    }    }  return 0.00;} public String input(String prompt){ return javax.swing.JOptionPane.showInputDialog(null,prompt); }public void output(String message){  javax.swing.JOptionPane.showMessageDialog(null,message);  }}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Is that the source of the whole class? to me it seems really messy, there are random open and close tags "{" "}", there is no class header "public class className {" and also instead of having "javax.swing.JOptionPane.showInputDialog(null,prompt);" you can import "javax.swing.JOptionPane" and only call "showInputDialog(null,prompt);". How much java experience do you currently have? are you new to java? to me it seems like your kind of new to java and may need to read some books or watch some beginners videos.

Link to comment
Share on other sites

Link to post
Share on other sites

 

 

Hey guys, I'm getting the error "ClassHeader expected instead" on the first float statement. Does anyone know why this is happening?

 

Here's the code

...

 

Is that the source of the whole class? to me it seems really messy, there are random open and close tags "{" "}", there is no class header "public class className {" and also instead of having "javax.swing.JOptionPane.showInputDialog(null,prompt);" you can import "javax.swing.JOptionPane" and only call "showInputDialog(null,prompt);". How much java experience do you currently have? are you new to java? to me it seems like your kind of new to java and may need to read some books or watch some beginners videos.

 

I forgot to mention the main reason of the error. It's because you don't have a class header. A class header specifies the name of the class and what api's it may use.

The way that Java is structured is in the following form:

package x; //where the current class is located (e.g. you have the src folder and inside the source folder is a com-->mahname-->coolapp folder and the class is placed inside of the coolapp folder then you'd set the package to com.mahname.coolapp)import x.y.z; //any packages you need to import from api'spublic class className{ //className will be replaced with the name of the class. The class is basically the .java file name	public static void Main(String[] args){		//main code to be run on start	}}
Link to comment
Share on other sites

Link to post
Share on other sites

Is that the source of the whole class? to me it seems really messy, there are random open and close tags "{" "}", there is no class header "public class className {" and also instead of having "javax.swing.JOptionPane.showInputDialog(null,prompt);" you can import "javax.swing.JOptionPane" and only call "showInputDialog(null,prompt);". How much java experience do you currently have? are you new to java? to me it seems like your kind of new to java and may need to read some books or watch some beginners videos.

lol, yeah, I'm kinda new, and this is a source code that's given to me by my teacher so I can modify it and add Parallel arrays. :P

 

Here's the whole exercise

Bookstore - Items and Prices in Parallel Arrays  /**************************************************Kim works in the school bookstore, selling pens, notebooks, etc.They have prices written in a notebook.  When prices changethey must cross out old prices - that's messy.  They wanta computer program that stores and displays prices.Item names and prices are stored in PARALLEL ARRAYS.***************************************************/ String[] items = {"pencil","pen","sharpie","notebook A","notebook B",                  "eraser","tippex","usb stick","glue","tape"                 };               float[] prices = { 0.75 , 1.50 , 1.20 , 1.90 , 2.50 ,                   0.50 , 1.75 , 5.00 , 1.25 , 2.00                 };              void setup(){  String name = "";  do  {    name = input("Type the name of an item");    float price = getPrice(name);    if(price > 0)    { output(name + " = " + price); }    else    { output(name + " was not found"); }  } while(name.length() > 0);                 // type nothing to quit} float getPrice(String name)               // search for name, return price{                                         // the price is not case-sensitive  for(int x=0; x < prices.length; x = x+1)  {    if(items[x].equalsIgnoreCase(name))        // not cases-sensitive    {      return prices[x];    }  }  return 0.00;} public String input(String prompt){ return javax.swing.JOptionPane.showInputDialog(null,prompt); } public void output(String message){  javax.swing.JOptionPane.showMessageDialog(null,message);  }Parallel ArraysAn Array can only contain one type of data - a list of int values, or a list of float values, or a list of Strings.It is NOT POSSIBLE to store both Strings and float values in a single array.  So if we wish to storethe names and the prices of various items, we need two lists, not just one.  In this example, we havethe items array for storing the names of items (Strings) and the prices array for storing the prices of the items.It's important that the names and prices are "parallel", so if a name is in position 10, then the correspondingprice is also in position 10 - like this:positionITEMSPRICES0pencil0.751pen1.502sharpie1.20The items and prices arrays are called parallel arrays.  If  we need to store more information, for examplethe stock level (inventory number) of each item, we can create a third array - say inventory.This enables a Java program to store data in a similar fashion to a spreadsheet table.Programming PracticeDownload the program and run it.  Check that it works correctly.Add 5 more items, with names in the items array and prices in the prices array.Add a method that prints the NAMES of all the items that cost less than 2.00 .Change the program so that it inputs the name of the item to be sold,as well as the quantity to be sold - e.g. "pencil" , 3 -and then it finds the price and calculates the total cost, multiplying the price times the quantity.Add another array called inventory, containing the number of items currently in stock.If the quantity to be sold is larger than the number in the inventory array,the program should notify the user that there are not enough of the item in inventory.Write a method called totalValue.  This loops through the entire list, multiplying the pricetimes the inventory number for each item, and adding up all these calculations.  At the end, that is value of all the items in stock.

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

 

 

Here's my modified code, and it's full of errors:

package ArraysTest;
import java.util.*;
import javax.swing.JOptionPane;
 
public class arraystest{ 
 
public static void Main(String[] args){
 
 
String[] items = {"pencil","pen","sharpie","notebook A","notebook B",
                  "eraser","tippex","usb stick","glue","tape"
                 };
               
double[] prices = { 
  0.75 , 1.50 , 1.20 , 1.90 , 2.50 ,
                   0.50 , 1.75 , 5.00 , 1.25 , 2.00 };
 
 
{
  String name = "";
  do
  
  {
    name = System.out.println("Type the name of an item");
    
    float price = getPrice(name);
    
    if(price > 0)
    
    { System.out.println(name + " = " + price); }
    
    else
   
    { System.out.println(name + " was not found"); }
  
 
 while(name.length() > 0);             // type nothing to quit
  
  }}
 
float getPrice(String name)           // search for name, return price
{                                     // the price is not case-sensitive
  for(int x=0; x < prices.length; x = x+1)
  {
    if(items[x].equalsIgnoreCase(name))    // not cases-sensitive
    {
      return prices[x];
    }  
  }
  return 0.00;
 
public String input(String prompt)
{ return javax.swing.JOptionPane.showInputDialog(null,prompt); }
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Is that the source of the whole class? to me it seems really messy, there are random open and close tags "{" "}", there is no class header "public class className {" and also instead of having "javax.swing.JOptionPane.showInputDialog(null,prompt);" you can import "javax.swing.JOptionPane" and only call "showInputDialog(null,prompt);". How much java experience do you currently have? are you new to java? to me it seems like your kind of new to java and may need to read some books or watch some beginners videos.

Ok, so I've modified it a bit, but I still get a bunch of errors.

 

Here is the source code:

/**************************************************Kim works in the school bookstore, selling pens, notebooks, etc.They have prices written in a notebook.  When prices change they must cross out old prices - that's messy.  They wanta computer program that stores and displays prices.Item names and prices are stored in PARALLEL ARRAYS.***************************************************//**************************************************Download the program and run it.  Check that it works correctly.Add 5 more items, with names in the items array and prices in the prices array.Add a method that prints the NAMES of all the items that cost less than 2.00 .Change the program so that it inputs the name of the item to be sold,as well as the quantity to be sold - e.g. "pencil" , 3 -and then it finds the price and calculates the total cost, multiplying the price times the quantity.Add another array called inventory, containing the number of items currently in stock.If the quantity to be sold is larger than the number in the inventory array,the program should notify the user that there are not enough of the item in inventory.Write a method called totalValue.  This loops through the entire list, multiplying the pricetimes the inventory number for each item, and adding up all these calculations.  At the end, that is value of all the items in stock.***************************************************/package ArraysTest;import java.util.*;import javax.swing.JOptionPane;public class arraystest{ 	public static void Main(String[] args){		String[] items = {"pencil","pen","sharpie","notebook A","notebook B",                  "eraser","tippex","usb stick","glue","tape" };               double[] prices = {0.75 , 1.50 , 1.20 , 1.90 , 2.50 ,                   0.50 , 1.75 , 5.00 , 1.25 , 2.00 };{  String item = "";  do   {   	     item = input("Type the name of an item");         float price = getPrice(item);        if(price > 0)        { System.out.println(item + " = " + price); }        else       { System.out.println(item + " was not found"); }    } 	  	  while(item.length() > 0);             // type nothing to quit    }}public static float getPrice(String name)           // search for name, return price{                                     // the price is not case-sensitive  for(int x=0; x < prices.length; x = x+1)  {    if(items[x].equalsIgnoreCase(name))    // not cases-sensitive    {      return prices[x];    }    }  return 0.00;} public static String input(String prompt){ return javax.swing.JOptionPane.showInputDialog(null,prompt); }}

and I get "prices variable can't be resolved on this element: 

public static float getPrice(String name)           // search for name, return price{                                     // the price is not case-sensitive  for(int x=0; x < prices.length; x = x+1)  {    if(items[x].equalsIgnoreCase(name))    // not cases-sensitive    {      return prices[x];    }    }  return 0.00;} 

Thanks for your help

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

 

 

you're almost getting there. I would explain to you the problems but I'm currently at work and also I'm not the best at explaining, great job thought you've successfully fixed the problems that I've mentioned but the second problem is that you have a start tag which is not applied to anything so basically your starting nothing and java does not understand what you're trying to applied the content of the start tag to so it'll be like "wtf is going on, what is the start tag to be used for." 

If you look over here

{  String name = "";

you can see that you have a start tag but its not applied to anything so what you need to do is either add a "if" statement to the start tag if you're checking for a certain element or move the start tag outside of the main and create a new void and give it a name so it'll kinda be a group containing functions which can be called by the name you gave it

e.g. you have a function which reads from the database and it needs to be done multiple times and to do this, you use the same method over and over again in this case you'd create a void and call it something like readFromDatabase and add the functions which read from the database to the void. this can be done as following:

void readFromDatabase(){	//connect to database	//read from it	//basically add all the methods which are used for reading from database}//after creating the void (group of code which gets executed by calling the group name) you can call it by simply writing down the void name like any other functions//e.g.public static void Main(String[] args){	readFromDatabase();}

I'd reccommend checking out some basic java tutorials before starting to code so you understand how java works and everything gets used

check out this guy I learned java from him, he's really good http://www.youtube.com/watch?v=SHIT5VkNrCg

Link to comment
Share on other sites

Link to post
Share on other sites

Ok, so I've modified it a bit, but I still get a bunch of errors.

 

Here is the source code:

/**************************************************Kim works in the school bookstore, selling pens, notebooks, etc.They have prices written in a notebook.  When prices change they must cross out old prices - that's messy.  They wanta computer program that stores and displays prices.Item names and prices are stored in PARALLEL ARRAYS.***************************************************//**************************************************Download the program and run it.  Check that it works correctly.Add 5 more items, with names in the items array and prices in the prices array.Add a method that prints the NAMES of all the items that cost less than 2.00 .Change the program so that it inputs the name of the item to be sold,as well as the quantity to be sold - e.g. "pencil" , 3 -and then it finds the price and calculates the total cost, multiplying the price times the quantity.Add another array called inventory, containing the number of items currently in stock.If the quantity to be sold is larger than the number in the inventory array,the program should notify the user that there are not enough of the item in inventory.Write a method called totalValue.  This loops through the entire list, multiplying the pricetimes the inventory number for each item, and adding up all these calculations.  At the end, that is value of all the items in stock.***************************************************/package ArraysTest;import java.util.*;import javax.swing.JOptionPane;public class arraystest{ 	public static void Main(String[] args){		String[] items = {"pencil","pen","sharpie","notebook A","notebook B",                  "eraser","tippex","usb stick","glue","tape" };               double[] prices = {0.75 , 1.50 , 1.20 , 1.90 , 2.50 ,                   0.50 , 1.75 , 5.00 , 1.25 , 2.00 };{  String item = "";  do   {   	     item = input("Type the name of an item");         float price = getPrice(item);        if(price > 0)        { System.out.println(item + " = " + price); }        else       { System.out.println(item + " was not found"); }    } 	  	  while(item.length() > 0);             // type nothing to quit    }}public static float getPrice(String name)           // search for name, return price{                                     // the price is not case-sensitive  for(int x=0; x < prices.length; x = x+1)  {    if(items[x].equalsIgnoreCase(name))    // not cases-sensitive    {      return prices[x];    }    }  return 0.00;} public static String input(String prompt){ return javax.swing.JOptionPane.showInputDialog(null,prompt); }}

and I get "prices variable can't be resolved on this element: 

public static float getPrice(String name)           // search for name, return price{                                     // the price is not case-sensitive  for(int x=0; x < prices.length; x = x+1)  {    if(items[x].equalsIgnoreCase(name))    // not cases-sensitive    {      return prices[x];    }    }  return 0.00;} 

Thanks for your help

you're getting the static error because you're creating static element (in this case its the Scanner) when by default it isn't, to resolve this problem what you can do is create the buffer outside of the main but within the class but do not apply a value so it'll be Scanner input; and then within the main apply the value by saying that the input is a new scanner so within main goes input = new Scanner(System.in);

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks so much for your help pal, reply whenever you want :)

 

I fixed the static error and now I think I know what the teacher wants me to do here: 

Basically he wants to add to the String item a value from the array String items, but it doesn't recognize it...

 

Also the for loop at the end doesn't recognize the two arrays..That's the error: Prices cannot be resolved to a variable

String[] items = {"pencil","pen","sharpie","notebook A","notebook B",                  "eraser","tippex","usb stick","glue","tape" };               double[] prices = {0.75 , 1.50 , 1.20 , 1.90 , 2.50 ,                   0.50 , 1.75 , 5.00 , 1.25 , 2.00 };{  String item = "";  do   {
public static float getPrice(String item)           // search for name, return price{                                     // the price is not case-sensitive  for(int x=0; x < prices.length; x = x+1)  {    if(items[x].equalsIgnoreCase(item))    // not cases-sensitive    {      return prices[x];    }    }  return 0.00;} 

 

Link to comment
Share on other sites

Link to post
Share on other sites

From the first post  it is clear that you need to take some time to study and understand the concepts behind Object-Oriented Programming and Java before jumping into any programming.

import java.util.*;import javax.swing.JOptionPane;public class ArraysTest{   static String[] items = {"pencil","pen","sharpie","notebook A","notebook B",                    "eraser","tippex","usb stick","glue","tape" };  static double[] prices = {0.75 , 1.50 , 1.20 , 1.90 , 2.50 ,                   0.50 , 1.75 , 5.00 , 1.25 , 2.00 };  public static void main(String[] args){    String item;    do {      item = input("Type the name of an item");      double price = getPrice(item);      if(price > 0) {        System.out.println(item + " = " + price);      } else {         System.out.println(item + " was not found");      }    } while(item.length() > 0);             // type nothing to quit    }  public static double getPrice(String name) {  // search for name, return price                                      // the price is not case-sensitive    for(int x=0; x < prices.length; x = x+1)      if(items[x].equalsIgnoreCase(name))    // not cases-sensitive        return prices[x];       return 0;  }   public static String input(String prompt) {    return javax.swing.JOptionPane.showInputDialog(null,prompt);   }}

This code should compile and run without any problem.

The problem about items and prices is that you are declaring them inside the main function but trying to use them elsewhere. In order for the arrays to be visible to all the functions in a class they need to be declared in the class rather than in a function and, since you are not instantiating any object (you are rather trying to access them from a static context), they, too, need to be static.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×