14 Replies - 3137 Views - Last Post: 26 July 2010 - 10:40 AM Rate Topic: -----

#1 Guest_Tommy*


Reputation:

toUpperCase & toLowerCase problem

Posted 25 July 2010 - 05:21 PM

Hi.
My program can't seem to change the cases... The GNumber is supposed to be uppercased and username lowercased. Any help will be greatly appreciated.
package labs;

public class StudentData {
	private String gNumber;
	private String userName;
	public String getGNumber(){return gNumber;}
	 public String getUserName() {return userName;}

	StudentData(){
		gNumber ="";
		userName ="";
		}

	 public boolean setGNumber(String g)
	 {
	  if (validGN(g))
	  {
	   gNumber=g;
	   return true;
	  }
	  return false;
	 }

	 private boolean validGN(String gn)
	 {
		 gn = gn.toUpperCase();
	  int length;
	  boolean digit=false;
	  length=gn.length();
	  if(length==9){
			if (gn.substring(0,3).equals("G00"))


	  for(int i=3; i<gn.length(); i++)
	   if(Character.isDigit(gn.charAt(i)))
	    digit=true;
	  }
	  return (digit);
	}
	 public boolean setUserName(String u)
	 {
		 if (validUName(u))
		  {
		   userName=u;
		   return true;
		  }
		  return false;
	 }
	 public boolean validUName (String u)
	 	{
		 u = u.toLowerCase();
		 int nameLength;
		 boolean letter = false;
	 		nameLength = u.length();
	 	if (nameLength>= 4 && nameLength<=8)
	 	{
	 		for(int i=0; i<u.length(); i++)
	 		if(Character.isLetter(u.charAt(i)))
	 			letter=true;
	 	}

	 	return(letter);

	 	}




}



package labs;
import javax.swing.JOptionPane;
public class Lab13 {
public static void main (String [] args)
{
	StudentData student = new StudentData();
	String input="";
	String output = "Summary of entries:\n";
		do {
			input = JOptionPane.showInputDialog( "Enter G#: (format G00123456");
		}while (!student.setGNumber(input));
		do {
			input = JOptionPane.showInputDialog( "Enter username: (format jprogram");
			}while (!student.setUserName(input));
			output += "Student G#: "+student.getGNumber() + "\nStudent Username: "+
			student.getUserName();
			JOptionPane.showMessageDialog(null, output);
		}
	}




Is This A Good Question/Topic? 0

Replies To: toUpperCase & toLowerCase problem

#2 sh1n3   User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 164
  • Joined: 22-April 10

Re: toUpperCase & toLowerCase problem

Posted 25 July 2010 - 06:26 PM

well 1 thing I think is happening is that when you run the for loop in the ValidGN the boolean variable keeps changing and if the last char is a digit it will return true whatsoever the cases before it.
So I'd suggest you to add a break statement if the boolean digit=false, here's what it would look like:
private boolean validGN(String gn)
	    {
	      gn = gn.toUpperCase();
	      int length;
	      boolean digit=false;
	      length=gn.length();
	      if(length==9){
	            if (gn.substring(0,3).equals("G00"))
	      for(int i=3; i<gn.length(); i++) {
	        if(Character.isDigit(gn.charAt(i))) {
	         digit=true;
	        }else {
                 break;
                }
              }
              }
	      return (digit);
	    }



EDIT : Added a lot more braces, to make the code look pretty

This post has been edited by sh1n3: 25 July 2010 - 07:04 PM

Was This Post Helpful? 1
  • +
  • -

#3 Guest_Tommy*


Reputation:

Re: toUpperCase & toLowerCase problem

Posted 25 July 2010 - 06:39 PM

View Postsh1n3, on 25 July 2010 - 05:26 PM, said:

well 1 thing I think is happening is that when you run the for loop in the ValidGN the boolean variable keeps changing and if the last char is a digit it will return true whatsoever the cases before it.
So I'd suggest you to add a break statement if the boolean digit=false, here's what it would look like:
private boolean validGN(String gn)
	     {
	         gn = gn.toUpperCase();
	      int length;
	      boolean digit=false;
	      length=gn.length();
	      if(length==9){
	            if (gn.substring(0,3).equals("G00"))
	      for(int i=3; i<gn.length(); i++)
	       if(Character.isDigit(gn.charAt(i)))
	        digit=true;
	      }else {
                break;
              }
	      return (digit);
	    }


nope. that doesn't work. :( Gives me an error. Says I can't use break outside of loop or switch.
Was This Post Helpful? 0

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: toUpperCase & toLowerCase problem

Posted 25 July 2010 - 06:43 PM

View PostTommy, on 25 July 2010 - 07:39 PM, said:

View Postsh1n3, on 25 July 2010 - 05:26 PM, said:

well 1 thing I think is happening is that when you run the for loop in the ValidGN the boolean variable keeps changing and if the last char is a digit it will return true whatsoever the cases before it.
So I'd suggest you to add a break statement if the boolean digit=false, here's what it would look like:
private boolean validGN(String gn)
	     {
	         gn = gn.toUpperCase();
	      int length;
	      boolean digit=false;
	      length=gn.length();
	      if(length==9){
	            if (gn.substring(0,3).equals("G00"))
	      for(int i=3; i<gn.length(); i++)
	       if(Character.isDigit(gn.charAt(i)))
	        digit=true;
	      }else {
                break;
              }
	      return (digit);
	    }


nope. that doesn't work. :( Gives me an error. Says I can't use break outside of loop or switch.

Indent your code properly... your error will become obvious

This post has been edited by pbl: 25 July 2010 - 06:44 PM
Reason for edit:: error not errir

Was This Post Helpful? 0
  • +
  • -

#5 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7183
  • View blog
  • Posts: 14,970
  • Joined: 16-October 07

Re: toUpperCase & toLowerCase problem

Posted 25 July 2010 - 07:03 PM

Consider the following:
void foo(String s) {
	s = s.toUpperCase();
	System.out.println("2: " + s);
}

String s = "Widget";
System.out.println("1: " + s);
foo(s);
System.out.println("3: " + s);



Results:
1: Widget
2: WIDGET
3: Widget



What's going on? In the method, you're playing with a reference. However, the "s = s.toUpperCase();" essentially replaces the variable s with a completely new object. The changes do not get passed back; you've severed the connection.

In your code, transform in one place, validate in another.
e.g.
public boolean setGNumber(String g) {
	g = g.toUpperCase(); // change the value here
	if (validGN(g)) {
		gNumber=g;
		return true;
	}
	return false;
}

private boolean validGN(String gn) {
	// gn = gn.toUpperCase(); not here
	int length = gn.length();
	if(length!=9) { return false; }
	if (!gn.substring(0,3).equals("G00")) { return false; }

	for(int i=3; i<length; i++)
		if(!Character.isDigit(gn.charAt(i))) { return false; }
	}
	return true;
}


Was This Post Helpful? 1
  • +
  • -

#6 Guest_Tommy*


Reputation:

Re: toUpperCase & toLowerCase problem

Posted 25 July 2010 - 07:07 PM

I'm sure its pretty obivious but right now my brain is lacking serious O2 :dots: I can't seem to see whats wrong with the indention. :no:
Was This Post Helpful? 0

#7 Guest_Tommy*


Reputation:

Re: toUpperCase & toLowerCase problem

Posted 25 July 2010 - 07:14 PM

View Postbaavgai, on 25 July 2010 - 06:03 PM, said:

Consider the following:
void foo(String s) {
	s = s.toUpperCase();
	System.out.println("2: " + s);
}

String s = "Widget";
System.out.println("1: " + s);
foo(s);
System.out.println("3: " + s);



Results:
1: Widget
2: WIDGET
3: Widget



What's going on? In the method, you're playing with a reference. However, the "s = s.toUpperCase();" essentially replaces the variable s with a completely new object. The changes do not get passed back; you've severed the connection.

In your code, transform in one place, validate in another.
e.g.
public boolean setGNumber(String g) {
	g = g.toUpperCase(); // change the value here
	if (validGN(g)) {
		gNumber=g;
		return true;
	}
	return false;
}

private boolean validGN(String gn) {
	// gn = gn.toUpperCase(); not here
	int length = gn.length();
	if(length!=9) { return false; }
	if (!gn.substring(0,3).equals("G00")) { return false; }

	for(int i=3; i<length; i++)
		if(!Character.isDigit(gn.charAt(i))) { return false; }
	}
	return true;
}


:genius: lol.
Was This Post Helpful? 0

#8 Guest_Tommy*


Reputation:

Re: toUpperCase & toLowerCase problem

Posted 25 July 2010 - 07:42 PM

oh. One more question. What if I wanted to validate lettersPLUS space in username...Would I have to write IgnoreSpace somewhere?
Was This Post Helpful? 0

#9 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7183
  • View blog
  • Posts: 14,970
  • Joined: 16-October 07

Re: toUpperCase & toLowerCase problem

Posted 26 July 2010 - 03:44 AM

Depends on where the space is. You may just want trim, i.e. " Space outside " to "Space outside".
Then:
s = s.trim();


If you want to do something like "in to" to "into", that's a little more involved. You probably want to spit, trim, and concat again. Or use a regex. Or loop through the thing and ignore all the stuff you don't want.
Was This Post Helpful? 1
  • +
  • -

#10 Guest_Tommy*


Reputation:

Re: toUpperCase & toLowerCase problem

Posted 26 July 2010 - 04:25 AM

If I had something like this.
public static Stylist fillStylist()
	{
//how could I do the do{} while for this when the object is set to username?
Because If I try to create new Object the others set to  oneStylists don't work. 
		Stylist oneStylist = new Stylist(JOptionPane.showInputDialog("Enter stylist's name:"));
		
	
		double wage, hours;
		do {
			wage = Double.parseDouble(JOptionPane.showInputDialog("What is "+oneStylist.getSName()+"'s hourly wage?"));
		} while (!oneStylist.setWage(wage));
		do {
			hours = Double.parseDouble(JOptionPane.showInputDialog("How many hours did "+oneStylist.getSName()+" work this week?"));
		} while (!oneStylist.setHours(hours));
		return oneStylist;
	}

Was This Post Helpful? 0

#11 Guest_Tommy*


Reputation:

Re: toUpperCase & toLowerCase problem

Posted 26 July 2010 - 04:52 AM

Because this would be wrong.....
//this would be wrong.
do{
		Stylist oneStylist = new Stylist(JOptionPane.showInputDialog("Enter stylist's name:"));
		} while(!oneStylist.setSName(oneStylist)) ;


Was This Post Helpful? 0

#12 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7183
  • View blog
  • Posts: 14,970
  • Joined: 16-October 07

Re: toUpperCase & toLowerCase problem

Posted 26 July 2010 - 05:10 AM

We've somehow jumped from students to Stylists? Strangely reminesent of Kathleen's post where she insisted tht parallel arrays were required. At least you ain't doing that. :P

Something like this should work:
Stylist getStylist() {
	Stylist stylist = new Stylist("Bob"); // assuming a name might be required.
	while(true) {
		String name = JOptionPane.showInputDialog("Enter stylist's name:").trim();
		if (stylist.setSName(name)) { break; }
	}
	
	while(true) {
		String result = JOptionPane.showInputDialog("What is "+stylist.getSName()+"'s hourly wage?").trim();
		if (stylist.setWage(Double.parseDouble(result)) { break; }
	}
	return styist;
}



You'll want to tell the user why you're asking the same question again if they fail. I intentionally left out the hours bit for you to do.

This post has been edited by baavgai: 26 July 2010 - 05:12 AM

Was This Post Helpful? 0
  • +
  • -

#13 bcranger   User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: toUpperCase & toLowerCase problem

Posted 26 July 2010 - 05:14 AM

Did you get your cases straight for your username and GNnumber?

Where in the small world did you get Stylist? It is a completely separate topic...

And I believe your do/while conditions are wrong...
Was This Post Helpful? 0
  • +
  • -

#14 Guest_Tommy*


Reputation:

Re: toUpperCase & toLowerCase problem

Posted 26 July 2010 - 10:37 AM

Kathleen and I are in the same class. She told me about this site. lol. We are doing this lab in the course right now.
Was This Post Helpful? 0

#15 Guest_Tommy*


Reputation:

Re: toUpperCase & toLowerCase problem

Posted 26 July 2010 - 10:40 AM

We have a final coming up soon and I just wanted to know if the same thing could be done with the stylist class.
Was This Post Helpful? 0

Page 1 of 1