Thread: Define accepted values from terminal to array

1. Member
Join Date
Mar 2011
Posts
19
Rep Power
0

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 02:02 PM.

2. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
6
Java Code:
`if(number<=9 && number>=0)`

3. Member
Join Date
Mar 2011
Posts
19
Rep Power
0
But where would I put that if sentence? Cause I cant get it to work properly.

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

5. Member
Join Date
Mar 2011
Posts
19
Rep Power
0
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 01:05 AM.

6. Member
Join Date
Mar 2011
Posts
19
Rep Power
0
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. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
6
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!

Posting Permissions

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