Wednesday 15 February 2012

java - Partially Filled Array Null Values -



java - Partially Filled Array Null Values -

so i've been brainstorming while now, im on lastly , final step of homework. think im done, it's need help getting rid of these null values:

here's code:

public static char[] readarray(char[] words){ char[] letters = new char[words.length]; letters = myinput(); //get input message ( int = 0 ; < letters.length ; i++) letters[i] = words[i] ; //store message array of words homecoming words; } public static char[] myinput(){ // method take message user string mymessage; system.out.print("input message: "); scanner myin = new scanner(system.in); mymessage = myin.nextline();// read line of message homecoming mymessage.tochararray(); } public static void printoneinline(char[] words){ //for every word, print them in line (int = 0 ; < words.length ; i++){ if (words[i] == ' ') // words separated space creates new line system.out.println(); else system.out.print(words[i]); //print word } }

test case:

input = hello world output = hello world nul nul nul nul ...

i know array partially filled , because of i < words.length scheme tries display values of array 0 - 256. suggestions gladly appreciated. ps: new java

it's time simplify this: rid of readarray method, not add together value on top of myinput, , utilize myinput instead. if in main(), work fine:

char[] words = myinput(); printoneinline(words);

the rest of code fine - no other changes necessary (demo).

the instruction says, method readarray should homecoming number of characters stored in array.

you need alter readarray method follows:

public static int readarray(char[] words){ char[] letters = new char[words.length]; letters = myinput(); //get input message ( int = 0 ; < letters.length ; i++) words[i] = letters[i] ; //store message array of words homecoming letters.length; }

now printoneinline need alter - needs take length on side, , utilize in place of words.length know when stop:

public static void printoneinline(char[] words, int len) { //for every word, print them in line (int = 0 ; < len ; i++){ if (words[i] == ' ') // words separated space creates new line system.out.println(); else system.out.print(words[i]); //print character } }

java null

No comments:

Post a Comment