Results 1 to 7 of 7
  1. #1
    KAS
    KAS is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default Define accepted values from terminal to array

    I have completed a program that ask the users for 20 numbers, and then counts how many each number has occurred. The thing I'm missing is that I need to define that user is only allowed to use the range 0-9,not negative numbers and not higher.

    How can I do this?

    Java Code:
    import java.io.StreamTokenizer;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            StreamTokenizer st = new StreamTokenizer(br);
            int[] history = new int[10];
            int[] elements = new int[20];
            int input;
    
            for (int x = 0; x < 20; x++) {
                do {        // les tall
                    System.out.println("Skriv et tall(0-9):");
                    input = st.nextToken();
                } while (input != StreamTokenizer.TT_NUMBER);
                elements[x] = (int) st.nval;
            }
            for (int i = 0; i < elements.length; i++) {
                int value = elements[i];
                history[value]++;
            }
            System.out.println("Siffer " + " Antall");
            for (int value = 0; value < history.length; value++) {
                System.out.printf("%3d%8d\n", value, history[value]);
            }
        }
    }
    Last edited by KAS; 04-01-2011 at 03:02 PM.

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Java Code:
    if(number<=9 && number>=0)

  3. #3
    KAS
    KAS is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    But where would I put that if sentence? Cause I cant get it to work properly.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    That parts for you to solve. Solar gave you the line to add, now think, where would it be useful?

  5. #5
    KAS
    KAS is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    I managed to figure it out, was easy enough. Thanks for the replies! Any suggestions how I could shorten down the amount of code? Is it possible to return back to the start of the for loop if user types an illegal value?

    Java Code:
            
             for (int x = 0; x < 20; x++) {
                do {        // les tall
                    System.out.println("Skriv et tall(0-9):");
                    input = st.nextToken();
                } while (input != StreamTokenizer.TT_NUMBER);
                elements[x] = (int) st.nval;
                if ((int) st.nval < 0 || (int) st.nval > 9) {
                    System.out.println("Du har tastet en ugyldig verdi.");
                    System.exit(0);
    //Instead of quitting program, maybe return to start of for loop?
    Java Code:
      
                } else {
                    continue;
                }
            }
    Complete code below:
    Java Code:
    import java.io.StreamTokenizer;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            StreamTokenizer st = new StreamTokenizer(br);
            int[] history = new int[10];
            int[] elements = new int[20];
            int input = 0;
    
            for (int x = 0; x < 20; x++) {
                do {        // les tall
                    System.out.println("Skriv et tall(0-9):");
                    input = st.nextToken();
                } while (input != StreamTokenizer.TT_NUMBER);
                elements[x] = (int) st.nval;
                if ((int) st.nval < 0 || (int) st.nval > 9) {
                    System.out.println("Du har tastet en ugyldig verdi.");
                    System.exit(0);
                } else {
                    continue;
                }
            }
            for (int i = 0; i < elements.length; i++) {
                int value = elements[i];
                history[value]++;
            }
            System.out.println("Siffer " + " Antall");
            for (int value = 0; value < history.length; value++) {
                System.out.printf("%3d%8d\n", value, history[value]);
            }
        }
    }
    Last edited by KAS; 04-02-2011 at 02:05 AM.

  6. #6
    KAS
    KAS is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    Final code

    Java Code:
    import java.io.StreamTokenizer;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            StreamTokenizer st = new StreamTokenizer(br);
            int[] history = new int[10];
            int[] elements = new int[20];
            int input = 0;
    
            for (int x = 0; x < 20; x++) {
                do {
                    do {        // les tall
                        System.out.println("Skriv et tall(0-9):");
                        input = st.nextToken();
                    } while (input != StreamTokenizer.TT_NUMBER);
                    elements[x] = (int) st.nval;
                } while ((int) st.nval < 0 || (int) st.nval > 9);
            }
            for (int i = 0; i < elements.length; i++) {
                int value = elements[i];
                history[value]++;
            }
            System.out.println("Siffer " + " Antall");
            for (int value = 0; value < history.length; value++) {
                System.out.printf("%3d%8d\n", value, history[value]);
            }
        }
    }

  7. #7
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Looks good, and you got your "continue;" correct, one thing I see that is unnecessary is creating two file reading objects:
    Java Code:
    InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
    You could do the same thing with just:
    Java Code:
     BufferedReader br = new BufferedReader(System.in);
    Good luck!

Similar Threads

  1. help inputting values into an array
    By pds8475 in forum New To Java
    Replies: 3
    Last Post: 01-22-2011, 06:45 PM
  2. Replies: 14
    Last Post: 01-22-2011, 03:36 PM
  3. Multiplying Values of an Array
    By brmcdani in forum New To Java
    Replies: 3
    Last Post: 02-06-2010, 05:00 AM
  4. Replies: 8
    Last Post: 11-20-2009, 10:33 PM
  5. Same values in an array
    By hawaiifiver in forum New To Java
    Replies: 3
    Last Post: 02-24-2009, 09:33 PM

Posting Permissions

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