Ask coding questions

← Back to all posts
Java master help...
SkyyCivil (120)

I am a brand-new -- --fresh-out of-the-oven java coder and here are a few questions that i have(Put the number of the question you are answering):
1.i know how to prompt the user for input in C but how do i do it in java?

2.Do i have to do System.out.println? (kind of alot of work)

3.How do i make/declare varibles?

4.What do source files do?

5.Can someone type me an example that allows the user to say something and prints something back?

Answered by Vandesm14 (2434) [earned 5 cycles]
View Answer
Commentshotnewtop
Vandesm14 (2434)
  1. I'm no expert but I think this'll help: https://www.quora.com/How-do-I-get-input-as-string-in-JAVA
  2. Yes, this is one of the annoyances of Java. You have to type the long command to print.
  3. You can make variables like so: int x = 3; or double y = 3.4;
  4. Source files are the source code that is run in your project
mwilki7 (1113)

If you want to shorten the print command you can make a function for it.

public static void println(String str)
{
    System.out.println(str);
}

User input for console is used in the Scanner class.
Here is an example of using it:

import java.util.Scanner;

public class Main
{
    public static void println(String str)
    {
        System.out.println(str);
    }
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        println("What's your name?");
        String name = input.nextLine();

        println("How old are you?");
        int age = input.nextInt();
        
        println("Your are a " + age + " year old " + name);
    }
}
SkyyCivil (120)

im really not understanding. i hope you guys can help me more than the online guides!