Java program not outputting negative integers from array? -
one of java assignments take numbers file , seperate them 2 arrays. 1 named p (positive numbers) , n (negative numbers.) have working positive numbers negative numbers maintain outputting 0s. have no thought why! help?
import java.io.*; import java.util.*; public class prog404a { public static void main(string[] args) { scanner infile = null; seek { infile = new scanner(new file("prg404a1.dat")); } grab (filenotfoundexception e) { system.out.println("file not found!!"); system.exit(0); } int temp = 0; int p[] = new int[23]; int n[] = new int[23]; int = 0; while (infile.hasnext()) { temp = infile.nextint(); if (temp < 0) { n[i] = temp; } if (temp > 0) { p[i] = temp; } i++; } (int x = 0; x < i; x++) { system.out.println(p[x] + "\t" + n[x]); } } }
edit: never mind it's not working positive numbers either. few.
maybe not counting right?
you should using 2 counters, 1 positive, 1 negative numbers. otherwise, half of entries 0, because never set.
java arrays file-io integer negative-number
No comments:
Post a Comment