This site requires JavaScript, please enable it in your browser!
Greenfoot back
8bitcarrotjuice
8bitcarrotjuice wrote ...

2013/9/3

Scanner Problems

8bitcarrotjuice 8bitcarrotjuice

2013/9/3

#
Hello everyone, I have recently been learning with eclipse and have run into a problem. The application I am trying to make is a external score keeper that you enter your data in and it processes it. I have all the other code in my notepad(I write the code while I'm outside) but I have recently run into a problem. The problem is that when I am trying to get the user to enter the names, it shows an extra 'null|'. The '|' I know why there is, and would be glad if I would not be able to have it there, but do not know why the null is there... Here is the code:
import java.util.Scanner;
import java.lang.System;
public class MainClass {

	private static Scanner scan;

	public static void main(String[] args) {
		int players,rounds,colum,line,count;
		scan = new Scanner(System.in);
		System.out.print("How many players? ");
		System.out.println();
		players=scan.nextInt();
		System.out.print("How Many Rounds? ");
		System.out.println();
		rounds=scan.nextInt();
        
		int scores[]=new int[players*rounds];
		int finalscores[]=new int[players];
		String names[]=new String[players];
		count=0;
		for(int ount=0; ount<players; ount++) {
			int player=ount+1;			
			System.out.println("Enter the name of player "+player+". Press Space & Enter to proceed.");
			System.out.println();
			char symbol='|';
			while(symbol!=' ')	{
			names[ount]+=symbol;
			symbol=scan.findWithinHorizon(".",0).charAt(0);			
			}
			System.out.println(names[ount]);
		}
		}
	}
and output:
How many players? 
2
How Many Rounds? 
1
Enter the name of player 1. Press Space & Enter to proceed.

Adam
 
null|Adam
Enter the name of player 2. Press Space & Enter to proceed.

Dave 
null|Dave
Thanks in advance!
Gevater_Tod4711 Gevater_Tod4711

2013/9/3

#
The problem is that your String array is initialised with null types. When you now try to add a char using String += Char in line 27 of your code the first part of the string is null. To fix this bug you could either initialise the array with empty strings using a for loop or if you also want to remove the '|' chars at the beginning of the string you couls use the substring method after the strings are created like this:
//add this code after line 29 of your code:
for (int i = 0; i < names.length; i++) {
    names[i] = names[i].substring(5);
}
This will remove the 'null|' at the beginning of each string.
danpost danpost

2013/9/3

#
As usual, you need to take into account any and all situation or errors (or unwanted behavior) will most assuredly creep up. For instance, what happens if the user keys in alpha character(s) for either (or both) of the first two questions? or the same player name is entered more than once?
8bitcarrotjuice 8bitcarrotjuice

2013/9/4

#
This code
import java.util.Scanner;
import java.lang.System;
public class MainClass {

	private static Scanner scan;

	public static void main(String[] args) {
		int players,rounds,colum,line,count;
		scan = new Scanner(System.in);
		System.out.print("How many players? ");
		System.out.println();
		players=scan.nextInt();
		System.out.print("How Many Rounds? ");
		System.out.println();
		rounds=scan.nextInt();
        
		int scores[]=new int[players*rounds];
		int finalscores[]=new int[players];
		String names[]=new String[players];
		count=0;
		for(int ount=0; ount<players; ount++) {
			int player=ount+1;			
			System.out.println("Enter the name of player "+player+". Press Space & Enter to proceed.");
			System.out.println();
			char symbol='|';
			while(symbol!=' ')	{
			names[ount]+=symbol;
			symbol=scan.findWithinHorizon(".",0).charAt(0);			
			}
			for (int i = 0; i < names.length; i++) {  
			    names[i] = names[i].substring(5);  
			} 
			System.out.println(names[ount]);
		}
		}
	}
gives me this error
Exception in thread "main" java.lang.NullPointerException
	at MainClass.main(MainClass.java:31)
Help please?
davmac davmac

2013/9/4

#
The 'names' array initially has elements which are all null. You have a loop (lines 21 - 34) which initialises the elements one by one, but inside that you have another loop (lines 30 - 32) which goes through each element - including the null ones - and tries to take a substring. Hence you get a NullPointerException, because you are calling a method on a null reference. (By the time the execution reaches line 30, only names is not null; names through are still null). I think Danpost gave you bad advice in this case. The best solution to your original problem is to initialise the strings in the names array to the empty string, i.e. after line 19:
for (int i = 0; i < names.length; i++) {
    names[i] = "";
}
or even just do it within the loop you already have, i.e. after line 25:
names[ount] = "";
danpost danpost

2013/9/4

#
davmac wrote...
I think Danpost gave you bad advice in this case.
@davmac, please explain what was bad advice and why it was bad advice. Please note, my post was a general observation to steer 8bitcarrotjuice away from later problems.
davmac davmac

2013/9/4

#
@danpost, my sincere apologies - the bad advice came from Gevater_Tod4711, not from you! (I just meant that making the code change that was suggested resulted in this NullPointerException occurring at runtime; most of what Gevater_Tod4711 was actually fine, though I strongly recommend initialising the strings instead of using substring to remove the 'null' part)
You need to login to post a reply.