Sorry if this question has been asked before I haven't seen it asked on stack overflow. The code prompts the user for their name and module mark and then prints the names and module marks to the user at the end.
public class Main
{
public static void main( String args[] )
{
String name_array[] = new String[4];
int mark_array[] = new int[4];
System.out.println("Please enter in your name");
String name = BIO.getString();
System.out.println("Please enter in your module mark");
int mark = BIO.getInt();
name_array[0] += name;
mark_array[0] += mark;
int i = 1;
int z = 1;
while ( ! name.equals("END"))
{
while ( ! name.equals("END"))
{
System.out.println("Please enter in your name");
name = BIO.getString();
name_array[i] += name;
++i;
break;
}
while ( ! name.equals("END"))
{
System.out.println("Please enter in your module mark");
mark = BIO.getInt();
mark_array[z] += mark;
++z;
break;
}
if (i == name_array.length)
{
break;
}
}
for (int y = 0; y < name_array.length; ++y)
{
System.out.println(name_array[y] + ' ' + mark_array[y]);
}
}
}
The code prints null before my string which is entered. If I enter Harry Houdini as the name, nullHarry Houdini will be printed.
How do I avoid this?
nulland you are using+=to append something to it, it will cast thenullto be"null"and append the following value to"null", which finally results in something like"nullA"for example.