This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2016/6/25

why am I getting this error, please explain

1
2
divinity divinity

2016/6/25

#
hi I am practicing this object programming and I am getting this error, class, interface, enum expected. what can i do to fix it and explain it to me. here is the codes
public class CartItem 
{
    private String product;
    private int quantity;
    private double price;
}
//constructor
public CartItem()-the error is here. class, interface enum expected 
{
     
}
davmac davmac

2016/6/25

#
A constructor declaration needs to go inside the class body.
public class CartItem 
{
    private String product;
    private int quantity;
    private double price;

    //constructor
    public CartItem()
    {
      
    }
}
(The only thing allowed outside classes are classes, interfaces, and enums - which is what the error says, more or less.)
divinity divinity

2016/6/25

#
thank you davmac very much. and hey it is nice to kno u are still here
divinity divinity

2016/6/26

#
hey ah have another problem: am doing this object oriented online shopping program(just practicing) and i am stuck at this error. how can i fix those two error boolean pushForward = true; int userOption =0; int index=0; int total=0; //int input = 0; int integer=0; Integer item; } public void start() { int userOption; do { showMenu(); userOption = input.nextInt();the input.nextInt is where the next error is chooseOption(userOption); }while(userOption!=6); } public void showMenu() { System.out.println("Choose the correct option"); System.out.println("1: Add an item to your cart"); System.out.println("2: Remove an item from your cart"); System.out.println("3: View an item from your cart"); System.out.println("4: End shopping and go and check out"); System.out.println("5: Empty your cart"); System.out.println("6: Exit program"); System.out.println("Select a menu userOption"); } public void chooseOption(int userOption) { if(userOption < 1 || userOption > 6) { System.out.println("Enter a value between 1 and 6"); } else { switch(userOption) { case 1: //adding an item to the cart System.out.println("Please enter an item"); input = userinput.nextInt();this is the error, }
danpost danpost

2016/6/26

#
You have defined the 'input' field to be of 'int' type. You cannot execute the 'nextInt' method on an 'int' type value. The 'nextInt' method is of the java.util.Scanner class and needs a Scanner type object to execute on. There does not appear to be one created here.
divinity divinity

2016/6/26

#
so what should i do? can i use a double, float or any other type. i use the userinput. i had this for my scanner type, Scanner userinput = new Scanner(System.in); but i was still getting the error, ah even change it to scanner scan = new scanner(system.in) and still getting the error.
danpost danpost

2016/6/26

#
If you use:
Scanner userinput = new Scanner(System.in);
then, in the 'do' loop, you should be using:
userOption = userinput.nextInt();
If you are doing this and it is still not working, you should post then entire class with code tags around it (see the link below the reply box: Posting code? read this!).
divinity divinity

2016/6/26

#
Scanner userinput = new Scanner(System.in);
        
        ArrayList<Integer>intList = new ArrayList<Integer>();
        
        boolean pushForward = true;
        int userOption =0;
        int index=0;
        int total=0;
        int input = 0;
        int integer=0;
        Integer item;
        
    }
    
    public void start()
    {
        int userOption;
        
        do
        {
            showMenu();
            userOption = userinput.nextInt();this is where the error is.
            chooseOption(userOption);
            
        }while(userOption!=6);
    }
    public void showMenu()
    {
        System.out.println("Choose the correct option");
        System.out.println("1: Add an item to your cart");
        System.out.println("2: Remove an item from your cart");
        System.out.println("3: View an item from your cart");
        System.out.println("4: End shopping and goa nd check out");
        System.out.println("5: Empty your cart");
        System.out.println("6: Exit program");
        System.out.println("Select a menu userOption");
    }
    public void chooseOption(int userOption)
    {
        if(userOption < 1 || userOption > 6)
        {
            System.out.println("Enter a value between 1 and 6");
        }
        else
        {
            switch(userOption)
            {
                case 1:
                    //adding an item to the cart
                    System.out.println("Please enter an item");
                    int input = userinput.nextInt(); also getting it here to
                    Integer item = new Integer(input);
            }
        }
divinity divinity

2016/6/26

#
the error that i am getting is, say it cannot find symbol, ah forget to mention
danpost danpost

2016/6/27

#
divinity wrote...
the error that i am getting is, say it cannot find symbol, ah forget to mention
Did you add the following line at the top of the class?
import java.util.Scanner;
If that was not it, what exactly cannot it find? You forgot to add the new Integer object to your integer list at the end of the 'case 1' block of code.
divinity divinity

2016/6/27

#
yes danpost i do have the import.util.scanner dont know why it is not recognizing it
danpost danpost

2016/6/27

#
divinity wrote...
yes danpost i do have the import.util.scanner dont know why it is not recognizing it
danpost wrote...
Did you add the following line at the top of the class?
import java.util.Scanner;
If that was not it, what exactly cannot it find?
Did you see this line in my last post? What symbol can it not find?
divinity divinity

2016/6/27

#
case 1:
                    //adding an item to the cart
                    System.out.println("Please enter an item");
                    int input = userinput.nextInt();
                    Integer item = new Integer (input);
                    break;
                    
                case 2:
                    //remove item from the list
                    if(intList.contain(item)) the intList here also it cannot find.
                    {
                        intList.remove(item);it cannot find the (intList) the intList in the remove item
                        System.out.println(item+" has been removed");
                    }
                    else
                    {
                        System.out.println(item +"  was not found on the list");
                    }
                     break;
                
                 case 3:
                     // to view your item in the list
                     System.out.println("intList");
                     break;
                 
                 case 4:
                     //end shopping and add up total in cart
                     int i=0;
                     for(i = 0; i < intList.size(); i++)it cannot find the intList in the for loop 
                     {
                         item = intList.get(i);
                         double total = total + intValue(); the intValue and total it cannot find
                     }
                
danpost danpost

2016/6/27

#
I think the problem is where you are declaring your fields; but it is difficult to say because you still are only showing a portion of the class instead of the entire class (from the first import statement to the last closing bracket).
divinity divinity

2016/6/27

#
hi okay danppost, here is the whole thing
import java.util.ArrayList;
import java.util.Scanner;

public class Shopping 
{
    public static void main(String[]args)
    {
        
        ArrayList<CartItem>shoppingCart = new ArrayList<CartItem>();
        
        Scanner userinput = new Scanner(System.in);
        
        ArrayList<Integer>intList = new ArrayList<Integer>();
        
        boolean pushForward = true;
        int userOption =0;
        int intValue=0;
        int total=0;
        int input = 0;
        int integer=0;
        Integer item;
        
    }
    
    public void start()
    {
        int userOption;
        
        do
        {
            showMenu();
            userOption = userinput.nextInt();
            chooseOption(userOption);
            
        }while(userOption!=6);
    }
    public void showMenu()
    {
        System.out.println("Choose the correct option");
        System.out.println("1: Add an item to your cart");
        System.out.println("2: Remove an item from your cart");
        System.out.println("3: View an item from your cart");
        System.out.println("4: End shopping and goa nd check out");
        System.out.println("5: Empty your cart");
        System.out.println("6: Exit program");
        System.out.println("Select a menu userOption");
    }
    public void chooseOption(int userOption)
    {
        if(userOption < 1 || userOption > 6)
        {
            System.out.println("Enter a value between 1 and 6");
        }
        else
        {
            switch(userOption)
            {
                case 1:
                    //adding an item to the cart
                    System.out.println("Please enter an item");
                    int input = userinput.nextInt();
                    Integer item = new Integer (input);
                    break;
                    
                case 2:
                    //remove item from the list
                    if(intList.contain(item))
                    {
                        intList.remove(item);
                        System.out.println(item+" has been removed");
                    }
                    else
                    {
                        System.out.println(item +"  was not found on the list");
                    }
                     break;
                
                 case 3:
                     // to view your item in the list
                     System.out.println("intList");
                     break;
                 
                 case 4:
                     //end shopping and add up total in cart
                     int i=0;
                     for(i = 0; i < intList.size(); i++)
                     {
                         item = intList.get(i);
                         double total = total + intValue();here is a error also
                     }
                
                    
            }
        }
    }
    
}
There are more replies on the next page.
1
2