Reputation: 69
I am trying to take input from the console, but nothing is getting printed. I debugged the code and it's correctly storing values in the array, but nothing is getting printed. I am new to java. Please help.
import java.util.Scanner;
public class noofdays {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] date = new int[10];
int i = 0;
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
date[i]=in.nextInt();
i++;
}
for(i=0;i<3;i++)
{
System.out.println(date[i]);
}
}
}
Upvotes: 1
Views: 214
Reputation: 5662
I don't find anything wrong with your code, it may just behave a little different than you expect. So here is how I would do it.
One thing first: class names should always start with a capital letter (not an error but rather a convention that helps to understand the code)
public static void main(String[] args) throws IOException{
int[] date = new int[10]; // as mentioned above, a fixed size array will limit you - but if 10 is what you want, then this is what you need
int i = 0;
System.out.println("Please enter " + date.length + " numbers"); // just some output to tell the user that the program has started and what to do next
Scanner in = new Scanner(System.in); // perfect
// if you absolutely want your array filled, check if you reached the end of your input to avoid IndexOutOfBoundsExceptions.
// in.hasNext() will check for ANY input, which makes it easier to handle unwanted user input
while(i < date.length && in.hasNext()){
if(in.hasNextInt()){ // here you check if the input starts with a number. Beware that "1 w 2" is valid too!
date[i] = in.nextInt();
i++;
}else{
// this is to advise the user of an input error
// but more importantly, in.next() will read the invalid input and remove it from the inputstream. Thus your scanner will continue to read the input until it ends
System.out.println("sorry \"" + in.next() + "\" is not a valid number");
}
}
System.out.println("your input:");
for(i = 0; i < date.length; i++){ // you don't need any advanced loops, it is perfectly fine to use indexed loops. Just try to make your break condition more dynamic (like checking the length of the array instead of a constant value)
System.out.println(date[i]);
}
}
This is neither a solution, nor the best way to do it. I am merely trying to show you how you can guide your user and handle unwanted input.
edit: in a nutshell, these things should be considered:
1 two 2.3 , 4 . @¹"
10
numbers, otherwise either use an array of a different size, or a list (if you don't know how many numbers you need)if(in.next().equalsIgnoreCase("q")
could do the trick )long
as well or even BigInteger
?And here are some example runs:
Please enter 10 numbers
1
2
3 4 5 6 7 8 9
10
your input:
1
2
3
4
5
6
7
8
9
10
Please enter 10 numbers
1 2 3 4 5 6 7 8 9 10
your input:
1
2
3
4
5
6
7
8
9
10
Please enter 10 numbers
1 2 3 4 r 5 6 7 8 9 10
sorry "r" is not a valid number
your input:
1
2
3
4
5
6
7
8
9
10
Please enter 10 numbers
1 2 3 4 5 6 7 8 9 10 11
your input:
1
2
3
4
5
6
7
8
9
10
Upvotes: 2
Reputation: 1088
You need to tell your loop where to stop waiting for input. If you want to enter a line of integers
you could just use nextLine()
and use that String
instead.
This example will take one line of input and output valid ints.
public static void main(String[] args) {
// use a List, unless you want to enter exactly 10 integers
List<Integer> date = new ArrayList<Integer>();
int i = 0;
String x = "";
Scanner in = new Scanner(System.in);
x = in.nextLine();
String [] tokens = x.split(" ");
for (int y = 0; y < tokens.length; y++) {
try {
date.add(Integer.parseInt(tokens[y]));
} catch (Exception e) {
// error handling
date.add(-1); // set a default value instead or just do nothing
}
}
in.close(); // don't forget to close your Scanner
for (i = 0; i < date.size(); i++) {
System.out.println(date.get(i));
}
}
Input:
1 2 3.5 foo 50 bar 1234
Output:
1
2
-1
-1
50
-1
1234
Upvotes: 1
Reputation: 114
because the loop will not stop:
while (in.hasNextInt()) {
date[i]=in.nextInt();
i++;
}
so the code cannot be executed:
for(i=0;i<3;i++)
{
System.out.println(date[i]);
}
may be you can use this:
public static void main(String[] args){
int[] date = new int[10];
int i = 0;
Scanner in = new Scanner(System.in);
for(i=0;i<3;i++) {
date[i]=in.nextInt();
}
for(i=0;i<3;i++)
{
System.out.println(date[i]);
}
}
Upvotes: 1
Reputation: 1
int[] date = new int[10]; int i=0;
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
date[i]=in.nextInt();
System.out.println(date[i]);
i++;
}
Upvotes: -2