New to Java

Hey, I’m Nathan (StonePickaxes) and I am new to java game development.

I am very excited to learn how to make games, but I am having a hard time getting started. I would appreciate it a ton if anyone could help me get started or maybe give me some pointers for how to write a basic java game (say, a 2d platformer or a space shooter).

Thanks so much in advance,

Nathan

EDIT-

What is a reliable way of taking user input from the command line? I am trying to write a text adventure game where the user presses 1, 2, 3, etc to determine what happens.

Thanks again.

try
		{
			int key = System.in.read();
			
			if (key == '1')
				System.out.println("1");
			else
				System.out.println("Not 1: "+key); //key is in ascii
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

Thanks.

I have a few questions:

  1. Why did you do “try”? Would it work without this or the “catch”?

  2. When I run this, even if I inputted a 1, it shows “Not 1: 13 Not 1: 10” What does that mean?

Thanks again.

  1. Most things in java need a try catch - Its to handle unexpected errors. In this case, if something happens to the System input when you press a character which causes it to fail. Basically you put the things you want to do in try and then if something fails, it will jump to the catch block, where your code can handle the error.

If you don’t want to put try-catch around things, you can make the method you are in throw IOException.

eg.


public void DoSomething() throws IOException
{
       //do Stuff here
}

but then you must put try-catch around the method when you call it.
If you don’t use try-catch, your program won’t run. It’s kind of annoying when you are beginning, but it’s actually really useful. :slight_smile:

  1. Did you just press 1 and then enter? it should work. Make sure you are on a new line before you press 1 though.
    http://www.asciitable.com/ shows that 10 is the newline character and 13 is the carriage return character.
    When you press enter, it creates a new line by adding \r\n (\r is carriage return and \n is new line).
    From the output you got, it seems like you didn’t press 1 which is strange because you obviously did.
    I haven’t used System.in much because it blocks execution until you enter a key, which is fine for text games but not much else, especially since you have to enter the key in the console and press enter.

I figured out why it wasn’t diaplaying a 1. Where it said if it equals 1, it should have been if it equals 48 or whatever the ascii value of 1 is.

Thanks so much for the help. If I have any more questions, do you mind if I PM you?

did you put the ’ ’ around the 1? 1 is not ‘1’
‘1’ means (char) 1 = 48, but yeah, use 48 if that’s easier.
Yes, you can PM me if you want.

Tis best to publicly post questions so if someone else has the same questions, they could find the answers.

Welcome to JGO and wish you best of lucks in your journey with Java. If you need to understand the big picture of things, this post should prove to be quite helpful. :slight_smile:

EDIT: Also, don’t forget to handle new line characters since when you hit ENTER after inputting a line in the command prompt, it includes the \r\n (Carriage return + Line feed) characters (on Windows) and only the Line feed (\n) character on Unix.

True, I forgot about that :slight_smile:

Hi

You should use java.util.Scanner.

I have used the scanner before, but how can I check what a scanner is equal to? I need to check if the input is equal to an integer or a string.

http://ra4king.is-a-geek.net/javadocs/java/util/Scanner.html

Thanks, I figured it out.

Is there any way to clear all of the text from the command prompt screen? I want to try to keep it clean.

No :slight_smile:

Ah, oh well.

Now I am trying to get the input from a method and use it in another method, and I can’t get it to work.

Here’s the code -

package source;

import java.util.Scanner;

public class Game {

	public static boolean running = true;
	public static int loc = 1;
	public static String input;

	public static void InputHandler() {
		Scanner rawInput = new Scanner(System.in);
		String input = rawInput.nextLine();

		if (input.equals("exit")) {
			System.out.println("Thanks for playing, now exiting...");
			running = false;
		}
		Game.InputRedirector();
	}

	public static void LocationIdentifier() {

		if (loc == 1) {
			Game.MainMenu();
		}

	}

	public static void InputRedirector() {

		if (!running) {
			loc = 0;
		}

		if (loc == 1) {
			Game.MainMenuInput();
		}

	}

	public static void main(String[] args) {
		while (running) {
			Game.LocationIdentifier();
			Game.InputHandler();
		}
	}
	
	public static void Instructions() {
		System.out.println("-Welcome to *name*, an adventure game where you can level up, find treasure, and explore.");
		System.out.println("-");
		System.out.println("-");
		System.out.println("-");
		System.out.println("-");
	}

	public static void MainMenu() {
		System.out.println("-Main Menu");
		System.out.println("-play -instructions -exit");
	}

	public static void MainMenuInput() {
		if (input.equals("play")) {
			loc = 2;
		}
		
		if (input.equals("instructions")) {
			Game.Instructions();
		}
	}

}

Is there a better way i can get the input and then use it in the individual location methods? Or am I just doing this whole thing wrong? In that case, what can be changed?

Thanks for everything guys.

How about passing the String as a parameter?

I don’t know what that means :’(

Can you copy my code and edit it to show me what that means?

Here’s an example of passing variables as parameters:


public static void myMethod(String myString) {
    System.out.println("You called myMethod with " + myString);
}

public static void main(String[] args) {
    myMethod("an ordinary string.");
}

:frowning:

I don’t know how I would go about doing that with my code. I am pretty new at this, as you can see haha.

Then I would suggest you keep learning Java and not stop and try to accomplish more advanced stuff as it will confuse you :wink: