icecreeper01 icecreeper01 - 17 days ago 6
Java Question

Java Program runs but gives no output

Hey guys i was working on a project and the output gives nothing. I already tried a bunch of things which all gave no output except for moving System.out.print above the brace which just printed out an infinite number of random numbers. It's a short code so here it is:

import java.util.Scanner;
import java.io.IOException;
import java.io.File;

public class ACSLPrintsJR {
public static int value(int num){
int [] array = {0,16,16,8,8,4,4,2,2,1,1};
return array[num];
}

public static void main(String[] args) throws IOException {
int top = 1;
int bottom = 1;
File file = new File("ACSLPRINTSJR.IN");
Scanner scan = new Scanner(file);
int num = scan.nextInt();
while (num != 0){
num = scan.nextInt();
if (num % 2 == 0)
top += 1 + value(num);
else
bottom += 1 + value(num);
}
System.out.println(top+"/"+bottom);
scan.close();
}

}


As i said there is no output and here is the content of the IN file

Input is:


8 7 2 0

0


Expected output is:


19/3

1/1


Current output:
Nothing

Answer

You need to read from scanner in the loop. Below is updated code for you.

public class ACSLPrintsJR {
    public static int value(int num) {
        int[] array = {0, 16, 16, 8, 8, 4, 4, 2, 2, 1, 1};
        return array[num];
    }

    public static void main(String[] args) throws IOException {
        File file = new File("ACSLPRINTSJR.IN");
        Scanner scan = new Scanner(file);
        int num;
        while (scan.hasNext()) {
            int top = 1;
            int bottom = 1;
            while ((num = scan.nextInt()) != 0) {
                if (num % 2 == 0)
                    top += value(num);
                else
                    bottom += value(num);
            }
            System.out.println(top + "/" + bottom);
        }
        scan.close();
    }
}