Dogling's adventures into the lands of java.

Discussion in 'Hardware and Software' started by Dogling, 30 Sep 2017.

  1. Sup guys, I'm learning how to code in java with uni but I have no prior experience of coding. I'm struggling with finding certain information to use because I just don't know what to search.

    Basically I'm trying to make a simple game where the user inputs a 1, they win, put in any other number they lose. (Might try and randomise the winning number out of 10 but I don't even know where to start with that one.)

    Here's what I have so far, the game works but it's just poorly executed, I don't know how to make the if and else statements work properly.

    import java.util.Scanner;
    import java.util.*;
    import java.io.*;

    public class Oi {

    public static void main(String[] args) {

    int winningNumber;

    System.out.println("Please Enter 1 or 2");
    Scanner scanner = new Scanner( System.in );
    winningNumber = scanner.nextInt();

    if (winningNumber <=1)
    {
    System.out.println("You've won mate gratz");
    }
    else if (winningNumber >=2)
    {
    System.out.println("fuck you suck dick");
    }






    }

    }


    The <=1 and >=2 were just cop outs because = wouldn't work
     
  2. lol it's == cheers sunny

    fs
     
    • Funny Funny x 1
    • Useful Useful x 1
    • List
  3. I have no idea how to solve your problem, but check this website out: https://www.codecademy.com/

    I used it a few years ago to try coding, and its actually pretty effective, and easy to use.

    Also, try pasting the code onto this site: http://jshint.com/
    It should show you all the errors etc.

    Good luck with your coding adventures!
     
  4. Follow up question though.

    I've tried doing to error handling, how would I make it return something if the user put in something that wasn't 1 or 2 but wasn't a number

    I've tried

    else if (winningNumber <2)

    else if (winningNumber ==0)


    for the numbers and it works.

    How about characters?

     
  5. I wouldn't know...

    You might want to ask Gaw about his as i'm pretty sure he knows java.
     
  6. follow up question to the thread :P
     
  7. Dan Chief Detective at GM Police HQ - Jagex #1 Fan!

    Can you do something like != 1 gives a loss instead of specifying it as a number? Just anything that isn't the winning number is a loss?
     
  8. as in;

    else if (winningNumber != 1)
    {
    System.out.println("dude what the fuck did I say");

    ||
    (or* hee hee)

    if (winningNumber !=1)
    {
    System.out.println("you suck dick bro");

    Putting in a string still fucks it up
     
  9. spidEY シスコン

    Hello world!

    Code:
    import java.util.*;
    import java.lang.*;
    import java.io.*;
    
    class Oi
    {
    	public static void main (String[] args) throws java.lang.Exception
    	{
    		System.out.println("Please Enter 1 or 2");
    		
    		Scanner scanner = new Scanner(System.in);
    		int number;
    
    		try {
    			number = Integer.parseInt(scanner.nextLine());
    		} catch (NumberFormatException e) {
    			System.out.println("Are you a retard? That's not a number");
    			return;
    		}
    		
    		if (number == 1) {
    			System.out.println("You've won mate gratz");
    		} else if (number == 2) {
    			System.out.println("fuck you suck dick");
    		} else {
    			System.out.println("loser");
    		}
    	}
    }
    
     
    • Useful Useful x 4
    • Agree Agree x 1
    • List
  10. Sim Burner of props and hunter of hunters


    Close your scanner. Even better, try with scanner.
     
    • Agree Agree x 1
    • Useful Useful x 1
    • List
  11. That still gives me

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Oi.main(Oi.java:13)

    When typing in a string
     
  12. spidEY シスコン

    You can catch multiple types of exceptions in one try/catch block:

    Code:
    try {
        number = Integer.parseInt(scanner.nextLine());
    } catch (InputMismatchException | NumberFormatException e) {
        System.out.println("Are you a retard? That's not a number");
        return;
    }
    
     
  13. so...

    what?

    It still doesn't work with that, I don't know what half that means.
     
  14. spidEY シスコン

    Post the entire code you're trying to run. I can't reproduce it with what I wrote. :P
     
  15. import java.util.Scanner;
    import java.util.*;
    import java.io.*;

    public class Oi {

    public static void main(String[] args) {

    int winningNumber;

    System.out.println("Please Enter 1 or 2");
    Scanner scanner = new Scanner( System.in );
    winningNumber = scanner.nextInt();

    if (winningNumber ==1)
    {
    System.out.println("You've won mate gratz");
    } else if (winningNumber ==2)
    {
    System.out.println("fuck you suck dick");
    } else if (winningNumber <2)
    {
    System.out.println("dude what the fuck did I say");
    } else if (winningNumber ==0)
    {
    System.out.println("dude what the fuck did I say");
    }




    }

    }
     
  16. spidEY シスコン

    Methods in Java can throw exceptions. If you don't handle them in your code, the application crashes.

    Code:
    import java.util.Scanner;
    import java.util.*;
    import java.io.*;
    
    public class Oi {
        public static void main(String[] args) {
            int winningNumber;
    
            System.out.println("Please Enter 1 or 2");
          
            Scanner scanner = new Scanner(System.in);
          
            try {
                winningNumber = scanner.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("Error! Let's bail!");
                return;
            }
    
            if (winningNumber == 1) {
                System.out.println("You've won mate gratz");
            } else if (winningNumber == 2) {
                System.out.println("fuck you suck dick");
            } else if (winningNumber < 2) {
                System.out.println("dude what the fuck did I say");
            } else if (winningNumber == 0) {
                System.out.println("dude what the fuck did I say");
            }
        }
    }
    
    Why the hell are they using Java to tech people how to code? :confused:
     
  17. Sim Burner of props and hunter of hunters

    When you call scanner.nextInt(), scanner throws an "exception" if what it reads isn't an int - you've told it you want to read one, but there isn't one there. When you're using something that might cause exceptions, you can surround it in a try-catch block like this:


    Code:
    try {
               winningNumber = scanner.nextInt();
    } catch (InputMismatchException e) {
               System.out.println("Are you a retard? That's not a number");
               return;
    } finally {
               scanner.close()
    }
    
    The finally block just ensures that you stop reading from the source - which frees it up for other programs to use.
     
  18. Sim Burner of props and hunter of hunters

    By the way, how do you know when to expect an exception? Answer: the documentation should have it. For example, the documentation for Scanner.nextInt().
     
  19. Ayy it worked cheers,

    and no but now I do cheers fam


    Dunno but I'm liking it so far haha
     
  20. Mysteryem The Dividing Line

    I would go through Oracle's tutorials https://docs.oracle.com/javase/tutorial/ . I would suggest going through things in order, though there is a tutorial on using Scanner at https://docs.oracle.com/javase/tutorial/essential/io/scanning.html

    If you're someone that learns better from watching videos rather than reading then I can't help you.

    Edit: If you can, stick to a text editor (something like notepad++ is fine) rather than using an IDE to start with. It's much better to get the fundamentals down properly without relying on an IDE to do lots of things automatically without your understanding early on.
     

Users Viewing Thread (Users: 0, Guests: 0)