Click here to Skip to main content
16,004,806 members
Home / Discussions / Java
   

Java

 
QuestionPolygon Union Pin
bangbangbogi9-Nov-08 19:24
bangbangbogi9-Nov-08 19:24 
Question[Message Deleted] Pin
trump1016-Nov-08 11:47
trump1016-Nov-08 11:47 
AnswerRe: Help with java programming Pin
toxcct6-Nov-08 21:47
toxcct6-Nov-08 21:47 
GeneralRe: Help with java programming Pin
TannerB7-Nov-08 8:40
TannerB7-Nov-08 8:40 
GeneralRe: Help with java programming Pin
toxcct8-Nov-08 0:03
toxcct8-Nov-08 0:03 
GeneralRe: Help with java programming Pin
TannerB8-Nov-08 7:46
TannerB8-Nov-08 7:46 
GeneralRe: Help with java programming Pin
HosamAly19-Nov-08 6:23
HosamAly19-Nov-08 6:23 
QuestionNeed a tad of help [modified] Pin
TannerB3-Nov-08 8:43
TannerB3-Nov-08 8:43 
I've got this here code I just started today for an assigment, it is all working (so I'm not asking you to do it for me :P). What this code does is it considers any @ to be a backspace and deletes the character before it. I was supposed to use indexOf() and substring() but I thought using regular expressions would be simpler, and a learning experience at the same time. I think my problem right now is that I have stared at it for too long. What I want is for the loop in main to be ran atleast once but it seems weird to have the while(checkForError(input)); ran and then have it ran again in the method fixErrors. Any thoughts on what I could do to fix this would be welcome. Other comments and critiques are always welcome.

Thanks,
Tanner

EDIT: Update at bottom.

import javax.swing.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class bruce_tanner_A3Q3
{
	public static void main(String [] args)
	{
		String input;
		do
		{
			input = getString("Enter a string");
			printOutput(fixErrors(input));
		}while(checkForError(input));
	}

	//Returns true if specified string has an error ('@' in it) and false
	//if it does not.
	public static boolean checkForError(String input)
	{
		Pattern pattern = Pattern.compile("@");
		Matcher matcher = pattern.matcher(input);

		return matcher.find();
	}

	//Prompts the user for a string with the specified input
	//Returns the input string
	public static String getString(String input)
	{
		String tempString = JOptionPane.showInputDialog(input);
		return tempString;
	}

	//Prints the specified message
	public static void printOutput(String tempString)
	{
		System.out.println(tempString);
	}

	//Fixes any errors in the input string
	public static String fixErrors(String tempString)
	{
		if(checkForError(tempString))
		{
			Pattern pattern = Pattern.compile("[^@][@?+]");
			Matcher matcher = pattern.matcher(tempString);

			while(matcher.find())
			{
				tempString = matcher.replaceAll("");
				matcher = pattern.matcher(tempString);
			}

			pattern = Pattern.compile("@");				//This block gets rid of any extra @'s left over.
			matcher = pattern.matcher(tempString);
			tempString = matcher.replaceAll("");

			return tempString;
		}
		return "This string is error free";
	}
}


I'm not sure whether this was a flash of brilliance or not Wink | ;) but it works.

New main code:
public static void main(String [] args)
{
	String input;													//The main method of this program only contains the input string which get's
	for(input = getString("Enter a string"); checkForError(input); input = getString("Enter a string"))	//it's value from the method 'getString'. It then
	{																//
		printOutput(fixErrors(input));
	}
	printOutput("This string is error free");
}


New fixErrors:
public static String fixErrors(String tempString)
{
	Pattern pattern = Pattern.compile("[^@][@?+]");
	Matcher matcher = pattern.matcher(tempString);
	
	while(matcher.find())
	{
		tempString = matcher.replaceAll("");
		matcher = pattern.matcher(tempString);
	}

	pattern = Pattern.compile("@");				//This block gets rid of any extra @'s left over.
	matcher = pattern.matcher(tempString);
	tempString = matcher.replaceAll("");

	return tempString;
}


modified on Monday, November 3, 2008 2:57 PM

Questionexe file Pin
sunil_jvfrm1-Nov-08 1:55
sunil_jvfrm1-Nov-08 1:55 
AnswerRe: exe file Pin
toxcct1-Nov-08 3:52
toxcct1-Nov-08 3:52 
AnswerRe: exe file Pin
jrsk1-Nov-08 23:20
jrsk1-Nov-08 23:20 
AnswerRe: exe file Pin
lixiqiang4-Nov-08 20:24
lixiqiang4-Nov-08 20:24 
Questionproject doubt Pin
ramakrishnudu31-Oct-08 8:29
ramakrishnudu31-Oct-08 8:29 
QuestionSet Represntation, please help !!!!!!!!!!!!!!!!!!!!!!!!!!!!! Pin
sudany_zool29-Oct-08 16:19
sudany_zool29-Oct-08 16:19 
AnswerRe: Set Represntation, please help !!!!!!!!!!!!!!!!!!!!!!!!!!!!! Pin
daniel_at_work2-Nov-08 22:21
daniel_at_work2-Nov-08 22:21 
QuestionHow to find the coordinates of a JList item Pin
nkadivar29-Oct-08 11:50
nkadivar29-Oct-08 11:50 
QuestionCompiling Java Code Pin
Kevin Marois29-Oct-08 6:29
professionalKevin Marois29-Oct-08 6:29 
AnswerRe: Compiling Java Code Pin
nkadivar29-Oct-08 11:52
nkadivar29-Oct-08 11:52 
AnswerRe: Compiling Java Code Pin
toxcct29-Oct-08 22:31
toxcct29-Oct-08 22:31 
GeneralRe: Compiling Java Code Pin
leesoief4-Nov-08 20:28
leesoief4-Nov-08 20:28 
QuestionJava Servlets with AJAX Pin
ro88o29-Oct-08 5:22
ro88o29-Oct-08 5:22 
QuestionData access objects used in jsp and beans? [modified] Pin
pdoy070827-Oct-08 15:16
pdoy070827-Oct-08 15:16 
QuestionSingle Sign On Pin
vedabj27-Oct-08 1:18
vedabj27-Oct-08 1:18 
Question"Help" JTable problem Pin
slslooo26-Oct-08 20:54
slslooo26-Oct-08 20:54 
QuestionHelp on Search and Replace in a file using java Pin
go4rib24-Oct-08 20:21
go4rib24-Oct-08 20:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.