0

I'm really new to java and i'm taking an introductory class to computer science. I need to know how to Prompt the user to user for two values, declare and define 2 variables to store the integers, and then be able to read the values in, and finally print the values out. But im pretty lost and i dont even know how to start i spent a whole day trying.. I really need some help/guidance. I need to do that for integers, decimal numbers and strings. Can someone help me?

this is what ive tried

import java.util.Scanner;

class VariableExample
{ 
Scanner scan = new Scanner(System.in);

System.out.println("Please enter an integer value");
int a = scan.nextInt(); 
int b = scan.nextInt();

System.out.println("Please enter an integer value");
double c = scan.nextDouble(); 
double d = scan.nextDouble();

System.out.println("Please enter an integer value");
string e = scan.next();    
string f = scan.next();

System.out.println("Your integer is: " + intValue + ", your real number is: "
                            + decimalValue + " and your string is: " + textValue);
}

i told you... im really new

4
  • 2
    You forgot to declare the main method, and to put the code inside it. When code doesn't compile, and you wonder why, the first thing you should do is read and figure out the meaning of the error messages. Then google them if you don't understand them. Then post them. Ignoring error messages is a giant mistake. Commented Oct 25, 2014 at 19:36
  • I think you need to ask your instructor or TA. It's really important for them to know when lessons are not being explained properly in class. That said, what is the problem? You haven't said what it is you actually need help with. Commented Oct 25, 2014 at 19:37
  • 1
    You accepted this answer about an hour ago. Therefore, you know how to proceed with this. Don't you? Commented Oct 25, 2014 at 19:45
  • thank you for your help, i have fixed the problem thanks to all you guys Commented Oct 25, 2014 at 19:47

2 Answers 2

0

You forgot to declare entry point i.e. main() method, String S should be capital and in System.out.println() you used wrong variables:

class VariableExample { 
   public static void main(String... args) { // Entry point..
    Scanner scan = new Scanner(System.in);

    System.out.println("Please enter an integer value");
    int a = scan.nextInt();
    int b = scan.nextInt();

    System.out.println("Please enter an integer value");
    double c = scan.nextDouble();
    double d = scan.nextDouble();

    System.out.println("Please enter an integer value");
    String e = scan.next(); // String S should be capital..
    String f = scan.next();

    System.out.println("Your integer is: " + a + " " + b + ", your real number is: " + c + " " + d
            + " and your string is: " + e + "  " + f); // here you used wrong variables
   }
}

If still your problem not clear then let me know where you actually stuck.

Sign up to request clarification or add additional context in comments.

Comments

0

There are a couple of issues.

(1) You need to declare an "entry" point for your program. In Java, you must create a method with the exact signature:

public static void main(String args) {
// Your code here
}

(2) The "string" type in Java is capitalized.

(3) You are referencing variables that have been neither declared nor defined:

System.out.println("Your integer is: " + intValue + ", your real number is: "
                            + decimalValue + " and your string is: " + textValue);

In this case, you've never told Java what the value of intValue, etc is. It seems like you want to use the variables you have declared and defined like:

System.out.println("Your integer is: " + a + ", your real number is: "
                            + c + " and your string is: " + e);

(4) It looks like you're reading in two sets of variables for each prompt. Based on your prompt "Please enter an...", you really are expecting one input.

Altogether, I think your code should look like this:

class VariableExample { 
    public static void main(String args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("Please enter an integer value: ");
        int a = scan.nextInt();

        System.out.println("Please enter a double value: ");
        double c = scan.nextDouble();

        System.out.println("Please enter a string: ");
        String e = scan.next();

        System.out.println("Your integer is: " + a + ", your real number is: "
                            + c + " and your string is: " + e);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.