Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Parser question

Status
Not open for further replies.

txwylde

Programmer
Jan 25, 2001
60
US
Hello, I have been tasked to develop a Java application that will allow someone to sign in using string parsing.

The need to be able to type out their name, account and password separated by commas. The system will then display this information in another window.

Any help would be greatly appreciated.
take care
Bill
 
Look at the stringTokenizer class.

here is a quick example:

Code:
import java.io.*;
import java.util.StringTokenizer;

public class SimpleSignInExample
{
	public static void main(String[] args)
	{

		BufferedReader readConsole = new BufferedReader(new InputStreamReader(System.in));


		String userName 		= null;
		String userPassword 	= null;
		String userAccount		= null;

		String userInfo			= null;

		try
		{
			System.out.println("Hello and Welecome to ABC INC. ");
			System.out.print("Please enter your User Name: ");

			userName 		= readConsole.readLine();
			System.out.print("Please enter your Password: ");

			userPassword	= readConsole.readLine();

			System.out.print("Please enter your Account: ");
			userAccount		= readConsole.readLine();

			boolean validate = validateUser (userName + "," + userPassword + "," + userAccount);

			if (validate)
			{
				System.out.println("You are now connected to ABC Inc.");
			}
			else
			{
				System.out.println("Sorry the info you entered does not " +
								   "our records...Goodbye :( ");
				System.exit(2);
			}

		}
		catch(IOException e)
		{
			System.out.println( e );
		}




	}
	public static boolean validateUser(String userInfo)
	{
			boolean validate = false;

			StringTokenizer stringTokenizer = new StringTokenizer(userInfo, ",");

			if (stringTokenizer.countTokens() == 3)
			{
				while (stringTokenizer.hasMoreTokens())
				{

					if (stringTokenizer.nextToken().length() > 0 )
					{
						validate = true;
					}
				}
			}


			return validate;
	}

}
 
I appreciate your post. I need to have the string message say in a pop up window: Enter last name, account and password (separate each with a comma):

The user can thing type in:

Willie, 12345, wtb

The next screen will display:

Last Name: Willie
Account: 12345
Password: wtb


thanks!
Bill
 
here is the code I came up with.

mport javax.swing.JOptionPane;
import java.text.*;
public class SimpleSignInExample{
public static void main(String[] args){
String choice = "";
try{
while (!(choice.equalsIgnoreCase("x"))){

String userName = null;
String userPassword = null;
String userAccount = null;

String userInfo = null;

String inputString = JOptionPane.showInputDialog(null,
"Enter last name, account and password (separate each with a comma): ", JOptionPane.PLAIN_MESSAGE);
String message = "Last name: " + (userName) + "\n"
+ "Account: " + (userAccount) + "\n" + "Password: " + (userPassword) + "\n\n"
+ "To continue, press Enter.\n"
+ "To exit, enter 'x': ";
choice = JOptionPane.showInputDialog(null,
message, "Invoice", JOptionPane.PLAIN_MESSAGE);
}
}
catch(NullPointerException e){
System.exit(0);
}
System.exit(0);
}


I am getting an error of an illegal character. Any ideas?
 
In this line :
+ "Account: " + (userAccount) + "\n"
You have an illegal "\" .
 
I am still getting 3 errors.


}
^
C:\java\bin\SinpleSignInExample.java:3: class SimpleSignInExample is public, should be declared in a file named SimpleSignInExample.java
public class SimpleSignInExample{
^
C:\java\bin\SinpleSignInExample.java:15: cannot resolve symbol
symbol : method showInputDialog (<*>,java.lang.String,int)
location: class javax.swing.JOptionPane
String inputString = JOptionPane.showInputDialog(null,
^
3 errors

Process completed.


Any ideas?
 
- Is your SimpleSignInExample class saved in a file called &quot;SimpleSignInExample.java&quot; ?

- JOptionPane.showInputDialog does not accept the signature &quot;null, String, int&quot; - check the documentation for correct signatures.
 
Ok. I had one error where I mispelled the *.java name. I am down to two errors now.

--------------------Configuration: java <Default>--------------------
C:\java\bin\SimpleSignInExample.java:29: '}' expected
}
^
C:\java\bin\SimpleSignInExample.java:14: cannot resolve symbol
symbol : method showInputDialog (<*>,java.lang.String,int)
location: class javax.swing.JOptionPane
String inputString = JOptionPane.showInputDialog(null,
^
2 errors

Process completed.

 
import javax.swing.JOptionPane;
import java.text.*;
public class SimpleSignInExample{
public static void main(String[] args){
String choice = &quot;&quot;;
try{
while (!(choice.equalsIgnoreCase(&quot;x&quot;))){

String userName = null;
String userPassword = null;
String userAccount = null;
String userInfo = null;

String inputString = JOptionPane.showInputDialog(null,
&quot;Enter last name, account, and password (separate each with a comma): &quot;, &quot;Input&quot;, JOptionPane.PLAIN_MESSAGE);

String message = &quot;Last name: &quot; + (userName) + &quot;\n&quot;
+ &quot;Account: &quot; + (userAccount) + &quot;\n&quot;
+ &quot;Password: &quot; + (userPassword) + &quot;\n\n&quot;
+ &quot;To continue, press Enter.\n&quot;
+ &quot;To exit, enter 'x': &quot;;
choice = JOptionPane.showInputDialog(null,
message, &quot;Invoice&quot;, JOptionPane.PLAIN_MESSAGE);
}
}
catch(NullPointerException e){
System.exit(0);
}
System.exit(0);
}


Ok..I am down to one error left. :(

--------------------Configuration: java <Default>--------------------
C:\java\bin\SimpleSignInExample.java:30: '}' expected
}
^
1 error

Process completed.

 
import javax.swing.JOptionPane;
import java.text.*;
public class SimpleSignInExample{
public static void main(String[] args){
String choice = &quot;&quot;;
while (!(choice.equalsIgnoreCase(&quot;x&quot;))){

String userName = null;
String userPassword = null;
String userAccount = null;
String userInfo = null;

String inputString = JOptionPane.showInputDialog(null,
&quot;Enter last name, account, and password (separate each with a comma): &quot;, &quot;Input&quot;, JOptionPane.PLAIN_MESSAGE);

String message = &quot;Last name: &quot; + (userName) + &quot;\n&quot;
+ &quot;Account: &quot; + (userAccount) + &quot;\n&quot;
+ &quot;Password: &quot; + (userPassword) + &quot;\n\n&quot;
+ &quot;To continue, press Enter.\n&quot;
+ &quot;To exit, enter 'x': &quot;;
choice = JOptionPane.showInputDialog(null,
message, &quot;Input&quot;, JOptionPane.PLAIN_MESSAGE);
} //end while loop
System.exit(0);
}


Ok.. I took the try/catch out and still get an error:

--------------------Configuration: java <Default>--------------------
C:\java\bin\SimpleSignInExample.java:25: '}' expected
}
^
1 error
 
You are missing a &quot;}&quot; to end the class declaration.
 
Thanks for your help. My problem now is the Enter last name, account, and password (separate each with a comma):
needs to be on two separate lines.

It needs to be shown as:
Enter last name, account, and password
(separate each with a comma):

Secondly, When i ran my program the fields all show up as nulls. How do I get what I type in into the:

Last Name:
Account:
Password:

Thanks for your help!
 
The reason why this is all not working is because you are trying to incorporate a Swing GUI into a console application.

Your values are null, because while you may type in values into the option dialog, you don't actually retrieve them after you click &quot;Enter&quot;. To do this you need to write a proper Swing GUI with event handling and so on.

Why not take a look at a Swing tutorial :
and go back to the drawing board ...

Good luck !
 
Thanks for your help. I rewrote the app and it worked. I wasnt parsing the strings correctly. Once I parsed them right, it worked like a champ.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top