0

hy im having a little problem with a loop with jgrasp here is my basic program

import javax.swing.JOptionPane;

public class loops1
{

	public static void main (String[]args)
	{ 
      String name
		do
		{
			String name =  JOptionPane.showInputDialog ("who is the cleverest person in the room?");
			
		}
		while (name != "erin");
		
	} 
}

ok so i declared the varible name outside so it can pick it up but it keeps giving me an error message saying name has already been declared What am i doing wrong?

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Dark.Paladin
0

Because you declare it twice:

[B]String[/B] name
do
		{
			[B]String[/B] name =  ..... ;
		}

You declare it once outside the loop and use it like any other variable:

String name = "5";

name="asdfasfd";

name += "ssss";

Also you must add a ';' at the end of the declaration and initialize it. Otherwise you will get a NullPointerException.

Also use the equals method for comparing strings:

while (name.equals("erin"));
0

Also you must add a ';' at the end of the declaration and initialize it. Otherwise you will get a NullPointerException.

"name" is definitely assigned before any use. Unless the code is changed so that is no longer true, it's better to use the declaration without initialization.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.