user3008456
user3008456

Reputation: 33

Why am I getting these errors in Eclipse?

I am making a code that takes in cards from a user and puts them in a deck, the value of the card depends on the numbers e.g. 1 then 5 would be the 5 of hearts 3 then 13 would be the King of Spades. If a card is inserted correctly it should return true, if not it should throw an exception, it should check for room in the deck, if there isn't it should return false and finally if the card is already inserted it should return false. The errors are in the comments.

import java.util.Scanner;


public static void main(String[] args){ //red underline error on void saying "syntax error token", under the second bracket of String[] and on the last bracket after args.

private static addCards(){

String suit[] = {"Hearts", "Diamonds", "Spades", "Clubs"};
String value[] = {"ZZZZ", "Ace", "2", "3", "4", "5", "6", 
                  "7", "8", "9", "10", "Jack", "Queen", "King"};


String[] card = new String[52];  
String[] newSuit = new String[4];

Scanner input = new Scanner(System.in); //it says it expects a { instead of the semicolon here.

for (int i = 0; i < 52; i++){
    System.out.println("Please enter a suit");
    int inputSuit = input.nextInt();

    check = false;
    if(inputSuit = 1 || 2 || 3 || 4 ){
        check = true;
    }

    System.out.println("Please enter a card");
    int inputValue = input.nextInt();

    check1 = false;
    if(inputValue = 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 || 11 || 12 || 13){
        check1 = true;
    }

    try{
        check = true;
        check1 = true;
    }
    catch(card exception){
        ex.printStackTrace();
          System.exit(1);
    }

    switch (inputSuit) {
        case 0: newSuit[i] = suit[0]; break;
        case 1: newSuit[i] = suit[1]; break;
        case 2: newSuit[i] = suit[2]; break;
        case 3: newSuit[i] = suit[3]; break;
    }

    switch (inputValue) {
        case 1: card[i] = value[1]; break;
        case 2: card[i] = value[2]; break;
        case 3: card[i] = value[3]; break;
        case 4: card[i] = value[4]; break;
        case 5: card[i] = value[5]; break;
        case 6: card[i] = value[6]; break;
        case 7: card[i] = value[7]; break;
        case 8: card[i] = value[8]; break;
        case 9: card[i] = value[9]; break;
        case 10: card[i] = value[10]; break;
        case 11: card[i] = value[11]; break;
        case 12: card[i] = value[12]; break;
        case 13: card[i] = value[13]; break;
    }



    boolean isFull = true;
    for(String s : card) {
        if(s == null) {
            isFull = false;
            break;
        }
    }

}
} //"multiple markers in this line"

Upvotes: 0

Views: 90

Answers (5)

Ankit Rustagi
Ankit Rustagi

Reputation: 5637

Your using || incorrectly

    if(inputSuit = 1 || 2 || 3 || 4 ){

do this

    if(inputSuit == 1 || inputSuit == 2 || inputSuit == 3 || inputSuit == 4 ){

|| is used for performing boolean OR, general usage is

((Boolean Expression 1) || (Boolean Expression 2) || (Boolean Expression 3)...)

Similarly correct this

if(inputValue = 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 || 11 || 12 || 13){

Upvotes: 2

Prateek
Prateek

Reputation: 1926

You have more then one error in your code.

  1. No Class defined.
  2. Another method inside main method.
  3. Invalid use of || operator. Check @Ankit answer.
  4. Using = instead of ==. = is assignment operator, what you need is a comparison operator i.e. ==
  5. Really poor indentation.

Upvotes: 0

Adarsh
Adarsh

Reputation: 3641

  1. No public class YourClassName{}
  2. Main method has another method ? That is not allowed in java
  3. No return type for your method addCards()
  4. inputSuit = 1 || 2 || 3 || 4 is incorrect. You need to check separately for each number and == is the comparison operator. = is for assignment.
  5. Read up on writing exception catch blocks here

Upvotes: 0

k2col
k2col

Reputation: 2217

There's lots of stuff wrong I'm afraid. You haven't defined a class, which should correspond to the filename. You begin a main method but don't close it's parenthesis before starting another method. Your if statement isn't valid.

Perhaps you should take some time out to work through a beginners Java book or online tutorial, then come back to this code once you have a better understanding of Java syntax.

Upvotes: 1

Jason Sperske
Jason Sperske

Reputation: 30446

You are missing a public class [ClassName] {}

Upvotes: 1

Related Questions