Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Can't compile program!

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Can't compile program!

    import java.util.*;
     
    public class StudentInformation
    { 
        public static void main (String [] args)
        {
            Scanner scan = new Scanner (System.in);
     
            System.out.println("Please enter the first number between 0 and 9: ");
            int num1 = scan.nextInt();
     
            System.out.println("\nPlease enter the second number between 0 and 9: ");
            int num2 = scan.nextInt();
     
            System.out.println("\nPlease enter your operator: + for ADDITION, - for SUBTRACTION, * for MULTIPLICATION,");
            System.out.println("/ for DIVISION, or ^ for EXPONENTIATION: ");
            String operation = scan.next();
     
            if (num1 < 0 || num1 > 9 || num2 < 0 || num2 > 9)
            {
                System.out.println("\nInvalid Number");
            }
     
            String number1 = num1.toString();
            String number2 = num2.toString();
     
            switch (operation)
            {
                case "+":
                    System.out.println("The sum of " + number1 + " and " + number2 + " is " + (num1 + num2));
                    break;
                case "-":
                    System.out.println("The difference of " + number1 + " and " + number2 + " is " + (num1 - num2));
                    break;
                case "*":
                    System.out.println("The product of " + number1 + " times " + number2 + " is " + (num1*num2));
                    break;
                case "/":
                    System.out.println("The quotient of " + number1 + " divided by " + number2 + " is " + (num1/num2));
                    if(num2 == 0)
                        System.out.println("Cannot divide by 0");
                    break;
                case "^":
                    System.out.println("The expontentiation of " + number1 + " to the power of " + number2 + " is " + Math.pow(num1,num2));
                    break;
                default:
                    System.out.println(operation + " is not valid.");
            }
     
        }
    }

    I try to compile my code and get an error for

    String number1 = num1.toString();

    saying int cannot be dereferenced. I do not know how to fix this and any help would be great!
    Also when I tried to compile it, it said that the switch (operation) must be an int?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,106
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Can't compile program!

    Use one of the String class's methods to convert a int value to a String.
    Or a quick and dirty way to do it is concatenate an empty String: ""+num

    Or use the int value in the println:
    System.out.println("The sum of " + num1 + ...
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Spanky_10 (March 6th, 2013)

  4. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Can't compile program!

    I have the whole code better written now and the only thing I cannot figure out is how to change the num1 and num2 to strings that actually print out the digits in words.
    Output should be "nine" instead of 9.
    Could someone just show me how to do it. I learn so much better from examples.

    import java.util.Scanner;
     
    public class EnglishCalculator 
    {
        static int num1;
        static int num2;
        static char operator;
        static double result;
        static String str;
     
        public static void main(String[] args) 
        {
            Scanner input = new Scanner(System.in);
            System.out.println("enter the first number(0-9) :");
            num1 = input.nextInt();
            if (num1 < 0 || num1 > 9) 
            {
                System.out.println("Invalid number");
                return;
            }
     
            System.out.println("enter the second number(0-9) :");
            num2 = input.nextInt();
     
            if (num2 < 0 || num2 > 9) 
            {
                System.out.println("Invalid number");
                return;
            }
            System.out.println("Enter the operator :");
            operator = input.next().charAt(0);
     
            if (operator != '+' && operator != '-' && operator != '*' && operator != '/' && operator != '^') 
            {
                System.out.println("Invalid Operator");
                return;
            }
            switch (operator) 
            {
                case '+':
                str = "plus";
                result = num1 + num2;
                break;
     
                case '-':
                str = "minus";
                result = num1 - num2;
                break;
     
                case '*':
                str = "multiplied by";
                result = num1 * num2;
                break;
     
                case '/':
                if (num2 == 0) 
                {
                    System.out.println("Division by zero is not allowed");
                    return;
                }
                str = "divided by";
                result = num1 / num2;
                break;
     
                case '^':
                str = "to the power";
                result = Math.pow(num1, num2);
                break;
            }
     
            System.out.println(s1 + " " + str + " " + s2 + " is " + result);
        }
    }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,106
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Can't compile program!

    If the numeric values range from 0 to 9 you could create an array with the values: "zero" to "nine" as elements in the array and use the int's value as an index into the array.

    See this thread: How to convert a digit to words
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Spanky_10 (March 6th, 2013)

  7. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Can't compile program!

    I added an array to my code and thought I did it correct but I get an error saying " cannot find symbol -- myIntArray"

    import java.util.Scanner;
     
    public class EnglishCalculator 
    {
        static int num1;
        static int num2;
        static char operator;
        static double result;
        static String str;
     
        public static void main(String[] args) 
        {
            Scanner input = new Scanner(System.in);
            System.out.println("enter the first number(0-9) :");
            num1 = input.nextInt();
            if (num1 < 0 || num1 > 9) 
            {
                System.out.println("Invalid number");
                return;
            }
     
            System.out.println("enter the second number(0-9) :");
            num2 = input.nextInt();
     
            if (num2 < 0 || num2 > 9) 
            {
                System.out.println("Invalid number");
                return;
            }
            System.out.println("Enter the operator :");
            operator = input.next().charAt(0);
     
            int [] myIntArry = new int [10];
            myIntArray[0]= "zero";
            myIntArray[1]= "one";
            myIntArray[2]= "two";
            myIntArray[3]= "three";
            myIntArray[4]= "four";
            myIntArray[5]= "five";
            myIntArray[6]= "six";
            myIntArray[7]= "seven";
            myIntArray[8]= "eight";
            myIntArray[9]= "nine";
     
            if (operator != '+' && operator != '-' && operator != '*' && operator != '/' && operator != '^') 
            {
                System.out.println("Invalid Operator");
                return;
            }
            switch (operator) 
            {
                case '+':
                str = "plus";
                result = num1 + num2;
                break;
     
                case '-':
                str = "minus";
                result = num1 - num2;
                break;
     
                case '*':
                str = "multiplied by";
                result = num1 * num2;
                break;
     
                case '/':
                if (num2 == 0) 
                {
                    System.out.println("Division by zero is not allowed");
                    return;
                }
                str = "divided by";
                result = num1 / num2;
                break;
     
                case '^':
                str = "to the power";
                result = Math.pow(num1, num2);
                break;
            }
     
            System.out.println(myIntArray[num1] + " " + str + " " + myIntArray[num2] + " is " + result);
        }
    }

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,106
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Can't compile program!

    error saying " cannot find symbol -- myIntArray"
    Where is myIntArray defined? When I use the Find function of the browser it does not find a definition for a variable with that name.

    Look at the tutorial: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    see the section where it says: use the shortcut syntax to create and initialize an array:
    for the shortcut way to initialize an array.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    Spanky_10 (March 6th, 2013)

  10. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Can't compile program!

    Thank you! I finally got it! You can tell I'm definitely new to this

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,106
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Can't compile program!

    You're making progress.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Showrt program will compile but nothing happens when I run it.
    By learningjavanow in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 25th, 2012, 05:36 PM
  2. Please compile this program and see if it gives you an error
    By Programming_Hobbyist in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 2nd, 2012, 01:36 PM
  3. Trying to make a Car program and I can't seem to get it to compile
    By Necroshade in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 25th, 2011, 06:45 PM
  4. anagram program wont compile please help
    By Rusak in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 26th, 2011, 06:23 PM
  5. Anagram program wont compile
    By Rusak in forum Member Introductions
    Replies: 0
    Last Post: March 25th, 2011, 02:38 PM