CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2012
    Posts
    27

    Question Making a int read multiple ints (Scanners)?

    Im not too sure, I did some googling, and I think this method is called "Scanner", where one int is able to take more than 1 variable, however, whenever I try to use scanner it always says "Scanner class file not found"
    Code:
    import java.util.Scanner;
    
    public class SimpleMath 
    {
    public static void main( String args[] )
    { 
    Scanner input = new Scanner( System.in );
    
    int num1;
    int num2;
    int num3;
    int num4;
    int num5;
    
    System.out.print( "Enter five positive integers, with a single space in between each.");	
    }
    }
    ^ Taken off of a site, same method tho... Not my code

    So anyway, heres my question:

    I am trying to make a program where it first generates 4 random numbers (lets say for example reasons the numbers are 5 9 2 3)
    So the user now has to guess what these numbers are, however, heres how I done it (if there is a better approach, im open to ideas):
    there are 4 ints, each int is named after a placement in the number, like int one int two int three int four
    then, the user can type in an int called code
    so now what I want to do is have it so if they sat 5923 then it will take 5 from code and see if it matches int one, then 9 from code, see if it matches int two, and so on...

    (Also just a side issue, how would I make code incorporate spaces, so that way people can enter 5923 or 5 9 2 3, and it all works the same)
    Thanks for any help anyone can provide.

    My code:
    Code:
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    import java.util.Random;
    
    public class PINDecoder
    {
        public static void write (String text)
        {
            System.out.print (text);
        }
    
    
        public static void writeLine (String text)
        {
            System.out.println (text);
        }
    
    
        public static void write (int number)
        {
            System.out.print (number);
        }
    
    
        public static void writeLine (int number)
        {
            System.out.println (number);
        }
    
    
        public static void write (char character)
        {
            System.out.print (character);
        }
    
    
        public static void writeLine (char character)
        {
            System.out.println (character);
        }
    
    
        public static String readString ()
        {
            BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
            String var = "";
            try
            {
                var = br.readLine ();
            }
            catch (IOException ioe)
            {
                System.out.println (ioe.toString ());
            }
            return var;
        }
    
    
        public static int readInt ()
        {
            BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
            String tempVar = null;
            try
            {
                tempVar = br.readLine ();
            }
            catch (IOException ioe)
            {
                System.out.println (ioe.toString ());
            }
            int var = Integer.parseInt (tempVar);
            return var;
        }
    
    
        public static char readChar ()
        {
            BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
            char var = 0;
            try
            {
                var = (char) br.read ();
            }
            catch (IOException ioe)
            {
                System.out.println (ioe.toString ());
            }
            return var;
        }
    
    public static void main (String args[])
        {
            char decode = 0;
            char exit = 0;
            int counter;
            int code = 0;
            int trys = 0;
            
    Random rand = new Random();
                
            int one = rand.nextInt(10);
            int two = rand.nextInt(10);
            int three = rand.nextInt(10);
            int four = rand.nextInt(10);
    
    
            {
                write ("PIN (Personal Identification Number) Decoder");
                write ("");
                
                write ("You are given unlimited time to attempt to decode the randomly selected 4 digit code.");
                writeLine ("");
                writeLine ("After you get it right, it will tell you how many guesses it took you... Have fun...");
                write ("");
                while(exit != 'N' || exit != 'n') {
                writeLine ("");
                writeLine ("Type 'Crack' to begin decoding the 4 randomly selected numbers");
                writeLine ("");
                decode = readChar ();
                
                
                if (decode == 'C' || decode == 'c') {
                writeLine ("Generating PIN...");            
    try{
      Thread.currentThread().sleep(3000);
    }
    catch(Exception e){
    }
    
                writeLine (one + " | " + two + " | " + three + " | " + four);
                code = readInt();
                }
                    while (code != one && code != two && code != three && code != four)
                    {trys = trys + 1;
                    writeLine ("Incorrect. Try Again. (" + trys + ")");
                    code = readInt();
                    }
                    
                    if (code == one && code == two && code == three && code == four)
                    {
                    writeLine ("Code has been broken");
                    writeLine ("Decoded PIN: " + one + " | " + two + " | " + three + " | " + four);
                    }
    PS: I am still learning Java, I showed this code to a friend, and he told me it was weak, just a little FYI incase anyone cares...

  2. #2
    Join Date
    Jun 2013
    Location
    New York, USA
    Posts
    21

    Re: Making a int read multiple ints (Scanners)?

    I have a simple suggestion to make this program go along much easier. Rather than having 4 randomly generated numbers, why not have 1 4-digit number and compare the input to the int?

    Code:
    import java.util.*; // also calls scanner class
    public class PINDecoder
    {
        private int number; //private makes accessible to all methods
        private int userInput;
        private boolean win = false;
        
        
        static Scanner input = new Scanner(System.in); //creates input
        public PINDecoder()
        {
            if (win != true) {
                number = (int)(1000 + Math.random() * 9000); // creates range of 1000 - 9000
                
                System.out.println("Please choose a number");
                userInput = input.nextInt();
                
                if (userInput == number) {
                    win = true;
                }
                else {
                    System.out.println("Sorry, please try again");
                    System.out.println();
                    new PINDecoder();
                }
            }
            else if (win == true) {
                System.out.println("Congratulations, you won the game!");
            }
        }
        public static void main(String[] args) {
            new PINDecoder();
        }
    }
    Now I would recommend making a limit to the amount of tries you can do, otherwise the program has an endless loop and won't exit while running. You could do default close operations but that is involving JFrame which is a little more advanced.

  3. #3
    Join Date
    Jan 2012
    Posts
    27

    Re: Making a int read multiple ints (Scanners)?

    When I try running your code it sais:
    type Scanner was not found.

    I had this problem before, like I said, when I try using scanner for some reason it never picks it up, and always sais its not found

  4. #4
    Join Date
    Jun 2013
    Location
    New York, USA
    Posts
    21

    Re: Making a int read multiple ints (Scanners)?

    Well your code didn't import scanner like the tutorial suggested. I'm not sure why it didn't recognize scanner with my code but try switching java.util.*; with java.Scanner;. Otherwise I'm not quite sure why it won't work

  5. #5
    Join Date
    Jan 2012
    Posts
    27

    Re: Making a int read multiple ints (Scanners)?

    import java.util.Scanner; did not change to black, where as import java.util.* did... I dont think its inside my IDE, is there a way to install it? Or should I just completely re-install my Java IDE?

  6. #6
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: Making a int read multiple ints (Scanners)?

    This might sound like a stupid question but, what version of Java are you using? Scanner was introduced in 1.5.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured