This site requires JavaScript, please enable it in your browser!
Greenfoot back
JasonZhu
JasonZhu wrote ...

2014/4/26

Reading many Intergers in one line of input

JasonZhu JasonZhu

2014/4/26

#
I'm writing a program where it calculates the final temperature of a pie based off the variables I input. For instance: Input : 425 60 -0.01 3 1 I need a way to separate the numbers into variables that I can use. This is my attempt:
1
2
3
4
5
6
7
8
9
10
11
12
13
System.out.println("Input variables Tp, Tr, k, Q, m followed by an space between each.");
String l = in.nextLine();
String[] arr = l.split(" ");
ArrayList<Integer> aList = new ArrayList<Integer>();
for (String s : arr){
    aList.add(Integer.parseInt(s));
}
int Tp = aList.get(0);
int Tr = aList.get(1);
double k = aList.get(2);
int Q = aList.get(3);
int m = aList.get(4);
double total = 0;
However, I get this error after the first line prints out: java.lang.NumberFormatException: For input string: "" Help appreciated!
danpost danpost

2014/4/27

#
I got the same error with this code. But, it was because "-0.01" is not an int value and cannot be parsed by Integer to an int. You need to use 'Double.parseDouble' for that value:
1
2
3
4
5
6
7
String[] arr = l.split(" ");
int Tp = Integer.parseInt(arr[0]);
int Tr = Integer.parseInt(arr[1]);
double k = Double.parseDouble(arr[2]);
int Q = Integer.parseInt(arr[3]);
int m = Integer.parseInt(arr[4]);
double total = 0;
JasonZhu JasonZhu

2014/4/27

#
Makes sense, however, I get the error before I even input my line containing the variables.
danpost danpost

2014/4/27

#
How are you declaring and assigning the 'in' variable?
JasonZhu JasonZhu

2014/4/27

#
Scanner in = new Scanner(System.in);
JasonZhu JasonZhu

2014/4/27

#
Nobody knows why this is?
danpost danpost

2014/4/27

#
JasonZhu wrote...
Scanner in = new Scanner(System.in);
It seems like once this line is executed, Greenfoot stops executing code. I put a System.out.println immediately after it, and nothing came up on the terminal.
JasonZhu JasonZhu

2014/4/28

#
I'm still getting an error...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public void coolingPiles()
{
    Scanner in = new Scanner(System.in);
    System.out.println();
    System.out.println("Input the number of cases:");
    int cases = in.nextInt();
    int turns = 0;
    while(turns<cases){
        System.out.println("Input variables Tp, Tr, k, Q, m followed by an space between each.");
        String l = in.nextLine();
        String[] arr = l.split(" ");
        int Tp = Integer.parseInt(arr[0]);   //error occurs here
        int Tr = Integer.parseInt(arr[1]); 
        double k = Double.parseDouble(arr[2]); 
        int Q = Integer.parseInt(arr[3]); 
        int m = Integer.parseInt(arr[4]); 
        double total = 0
        for(int i=0;i<Q;i++){
            total += (Tp + (k*(Tp - Tr)));
            Tr += Q;
        }
        System.out.println(("Case #") + turns + (" ") + total);
        turns++;
    }
}
JasonZhu JasonZhu

2014/4/28

#
I figured out the problem. You need to create another instance of a Scanner to prompt Java to wait for user input. What was happening was that it was splitting an empty string then parsing nothing because I haven't had the chance to even input any values yet.
danpost danpost

2014/4/28

#
JasonZhu wrote...
I figured out the problem. You need to create another instance of a Scanner to prompt Java to wait for user input. What was happening was that it was splitting an empty string then parsing nothing because I haven't had the chance to even input any values yet.
Please show your corrected code for others.
JasonZhu JasonZhu

2014/4/28

#
I will show 2 solutions based on input values on a single line. One with a fixed amount of integer values and one with unknown amount of integer values. In both cases, rather than complicating the matter by accepting the input as a String and using the split() method to split the values between any spaces (" ") then having to parse the Strings back into Integers like I had done on my posts above, this is what I did: Fixed number of Integer input values. The following is a remake of my above code because I realized that I needed a fixed number of input variables:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public void coolingPiles()
{
    Scanner in = new Scanner(System.in);
    System.out.println();
    System.out.println("Input the number of cases:");
    int cases = in.nextInt();
    int turns = 0;
    while(turns<cases){
        System.out.println("Input variables Tp, Tr, k, Q, m followed by an space between each.");
        int Tp = in.nextInt();
        int Tr = in.nextInt();
        double k = in.nextDouble();
        int Q = in.nextInt();
        int m = in.nextInt();
        double total = 0
        for(int i=0;i<m;i++){
            total += (Tp + (k*(Tp - Tr)));
            Tr += Q;
        }
        System.out.println(("Case #") + (turns+1) + (" ") + total);
        turns++;
    }
}
This code is a great example where you'd have a fixed number of input values. An equation with a specific number of unknown variables. Unknown number of Integer input values. In this example, if I didn't have lines 8-10 and lines 12 and 13 used the Scanner "in" declared at the beginning, the code wouldn't prompt Java to wait for user input, but rather, it would continue processing through the code and I would end up getting an error when Java hits line 15 because my ArrayList would be completely empty due to no addition of inputted values. Here, you needed to create additional Scanners to avoid that outcome:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public void kindergartenPolicing()
{
    Scanner in = new Scanner(System.in);
    System.out.println("Input the number of cases:");
    int cases = in.nextInt();
    while(cases>0){
        System.out.println("Input student numbers followed by a space in between each number.");
        Scanner in1 = new Scanner(System.in);
        String input = in1.nextLine();
        Scanner in2 = new Scanner(input);
        ArrayList<Integer> intStorage = new ArrayList<Integer>();           
        while(in2.hasNextInt()){
            intStorage.add(in2.nextInt());
        }
        int largest = intStorage.get(0);
        for(int i = 0;i < intStorage.size();i++) {
            if(intStorage.get(i)>largest){
                largest = intStorage.get(i);
            }
        }
        boolean missing = false;
        int missingNum = 0;
        for(int i = 1;i<largest;i++){
            if(!intStorage.contains(i)){
                missing=true;
                missingNum = i;
            }
        }
        if(missing==true){
            System.out.println("Student " + missingNum + " is missing!!");
        }else{
            System.out.println("No one is missing.");
        }
        cases--;
    }
This is an example where the number of inputs are unknown. The number of students per class may be variant depending on the class numbers. Hope this helped. Don't bother too much about the way of I've coded things, especially if you can see a more efficient way of getting the same outcome. I'm trying to show how the Scanner can/must be used to avoid errors and achieve an outcome.
You need to login to post a reply.