java - Parallel arrays: how to initialize the length of the arrays when the input file is unknown -


this first time posting on here , new java. have assignment due week , i'm having trouble getting started, though think know after start it. ok teacher wants use parallel arrays , write program create bar chart. if gives input file contains:

4  sidney washington london new york 4  8 10 3 

it print out:

   sidney **** wasington *********    london **********  new york ***` 

so have started write program. don't know how initialize length of arrays. giving how many elements in first line of file (so using example above length 4), not know number is, have write program read number. have

import java.util.*; public class barchart {     public static void main(string args[])     {     scanner scan = new scanner(system.in);     file file = input;     // read in input file     int n=input.readint();      // create array hold datalabels, , hold datavalues.     string[] datalabels = new string[n];     int[] datavalues= new int[n];` 

it int n part don't know how write in order scan first line of input file , use number.

just use appropriate constructor of scanner:

scan = new scanner(new file("put path , filename here"), "utf-8"); int n = scan.nextint(); 

also, i'd name variable n more descriptive, arraylength.

be sure, , don't lazy: use 1 charset specification! , of course use appropriate character set code file.

warning: assignment, you'll asked how , why used constructor - prepared answer it! not specifying character set common mistake, , found quite places out in wild too...


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -