Results 1 to 10 of 10
  1. #1
    Phobos0001 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default i need assistance with a string triggered loop please!

    Greetings everyone,

    ive been learning java in TAFE for a few weeks now and im having a go at a simple program but im a bit stuck on how where to implament the loop and how to trigger it using a string. ive done some reading and there is a way to "count" the length of a string through command line interface, like this one...
    --------------------------
    import java.io.*;

    class StringLength{
    public static void main(String[] args){
    try{
    BufferedReader object=new BufferedReader (new InputStreamReader(System.in));
    System.out.println("Eneter string value:");
    String s=object.readLine();
    int len=s.length();
    System.out.println(len);
    }
    catch(Exception e){}
    }
    }
    -----------------------------------------

    however my program is using dialog boxes through "javax.swing.*"

    is there a way to do this too, the program i am doing is quite simple, just entering people's name and age, calculating their membership fee (the program is for gym membership),and a few other bits and pieces then displaying the ammount of people entered and the average age. my problem lies in getting the dialog boxes to pop up infinately untill you enter a blank name or a specific name like "quit"

    any help is apriciated, because my textboox "First book of java" by Garry j Bronson is not very informative about what i am after :(

    ill post up my current program seporately, it'll keep it a bit neater i think.
    well, thanks in advance and feel free to critisise, its a great way to learn :D

  2. #2
    Phobos0001 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    ok, this is what i have thus far on my program....
    ---------------------------------
    import javax.swing.*; //impors java gui for dialog boxes ect
    import java.text.*; //imports java texts for text formatting
    import java.io.*; //imports input output detection and information (i think i need this)

    public class test //defines class name and access
    {
    public static void main(String[] args) //begins body
    {
    String agebox, namebox; //assigning variables names and datatypes
    double fee, age, tax, total;

    fee = 20.50; //initialising variables that require manual initialisation

    DecimalFormat num = new DecimalFormat(",##.00"); //formatting the text

    namebox =JOptionPane.showInputDialog("Enter Member's Name, leave blank to exit member entry "); // user input for name
    agebox =JOptionPane.showInputDialog("Enter Member's Age: "); //user input for age
    age = Double.parseDouble(agebox); // assign age as a double for if-else chain

    if (age <= 7 ) //if-else chain for the member's fee
    {JOptionPane.showMessageDialog(null,"Warning Member's age is too low to leave unsurpervised",
    "WARNING!",
    JOptionPane.WARNING_MESSAGE);}
    else if (age <=17)
    { fee = 20.50;}
    else if (age <=21)
    { fee = 32.25;}
    else
    { fee = 45.75;} //end of if else chain

    tax = fee*10/100; //calculating tax and assigining it to a variable
    total = tax + fee; // adding fee and tax and assiginging it to a variable

    JOptionPane.showMessageDialog(null,
    "The member's name is " + namebox + "\nThe Member's age is " + agebox + "\nMember's fee is " + fee + "\nMember's tax is " + tax + "\nMember's fee including tax is " + total,
    "Gym Program",
    JOptionPane.INFORMATION_MESSAGE); //display's information of the current member

    System.exit(0);
    }
    }
    -----------------------------------------
    it will probably be a nasty unclean bit of code for you pro's but im still learning (hence the large ammount of comments heh heh )

  3. #3
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    I'm not 100&#37; sure what you are asking but it seems that you just need to know how to find the length of a String. Not matter where the String variable is, you can use its length() method to find out how long it is.

    Just like the example in your first post, it would be something like:

    Java Code:
    int length = s.length();

  4. #4
    Phobos0001 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    thanks for the reply mate,

    sorry im beying vauge ive been working on this for several hours and my brain is loosing the race :D

    basicaly i just need to figure out how to get an if-else chain to terminate the loop when i have a plank input dialog box (or a specific entry) and to continue the loop if it is not blank (or is not equal to a specific entry, such as "quit"

    thanks once again, i hope this clears up what i am trying to convey

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    try{
        BufferedReader object=new BufferedReader(
                              new InputStreamReader(System.in));
            boolean goAgain = true;
            do {
                System.out.println("Eneter string value:");
                String s=object.readLine();
                // Can "s" be null?
                int len=s.length();
                System.out.println(len);
                if(len == 0)
                    goAgain = false;
            } while(goAgain);
        object.close();
    }
    catch(Exception e){}

  6. #6
    Phobos0001 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    excelent, thanks for that code mate! much apriciated,

    ill try adapting it into my program with the joptionpane rather then system.out.plrintln.

    looks like what i am after.....even though i might not understand it all, but ill have a play with it and figure it out hopefully :D

    thanks a ton, much apriciated

  7. #7
    Phobos0001 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    ok, ive been playing around with the code ive been given (cheers for that by the way) and ive come up with a very simple test and got it working, and its exactly what i wanted, and here it is, incase anyone is interested
    Java Code:
    import javax.swing.*;
    import java.text.*;
    import java.io.*;	
    
    public class phrase
    {
      public static void main(String[] args)
      {
      	String agebox, namebox;
      	DecimalFormat num = new DecimalFormat(",##.00");
      	namebox =JOptionPane.showInputDialog("Enter Member's Name, leave blank to exit member entry ");	// user input for name
       	agebox =JOptionPane.showInputDialog("Enter Member's Age: ");
       	int len=namebox.length();
       			if (len != 0 )
      	  	  	{JOptionPane.showMessageDialog(null,"len != 0",
      			"WARNING!",
      			JOptionPane.WARNING_MESSAGE);}
      			else
      	{JOptionPane.showMessageDialog(null,"else len == 0",
      			"WARNING!",
      			JOptionPane.WARNING_MESSAGE);}   	
         System.exit(0);  
      }
    }
    so now all i need to do is intergrate that into the membership program i am writing in my 2nd post, i think it might have to be a do-while loop? it has to be an infinate loop untill the "len == 0"

    or would i just do an if-else to do this?
    such as this bit os pesudo-code i threw together?
    --------------------
    if len == 0 then display totals
    else show age dialog box
    ----------------

    or something more like this...
    -------
    do age dialog box
    while len !0 0
    --------

    im a bit at a loss but i'll get it eventualy, thankyou so much for the help so far everyone

  8. #8
    Phobos0001 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    ok i think ive finaly got it and all i should need to do now is put this into the main program....

    Java Code:
    import javax.swing.*;
    import java.text.*;
    import java.io.*;	
    
    public class phrase
    {
      public static void main(String[] args)
      {
      	String namebox,looped,done;
    namebox =JOptionPane.showInputDialog("namebox");	// user input for name
    int len=namebox.length();
    while (len !=0)
    {
    namebox =JOptionPane.showInputDialog("looped name");
    len=namebox.length();	
    }
    done =JOptionPane.showInputDialog("after loop");	// user input for name
      			
         System.exit(0);  
      }
    }
    feel free to correct me, congratulate me, or tell me where to go :D

    thanks again for the help everyone, hopefully i can get this finnished soon

  9. #9
    Phobos0001 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    first off, sorry for pasting in the wrong code above here, i tried to edit it however i dont have permissions, no matter though.

    ok now ive got the loop sorted out and the rest of the code is pretty solid, no compile errors, but i may have missed some "logical" errors, but the program works, so im happy.

    i do have one final thing to do, and i cant figure out why i cant get it to work..
    i want to build a report of the acumulated totals, such as total tax, total including tax, ect, ect.

    for that i need to write something like
    Java Code:
    ftax=tax++
    ftotal=total++
    is that right? if ftax and total are double data types and not intergers?

    also i want to work out the average age, would i go about that by doing something like this..
    Java Code:
    int counter,age
    counter=0;
    { 
    my input loop here
    tage=age++
    counter ++
    }
    average=tage/counter
    is this a bad approach to do it?..
    this is the program as it stands now, completed but the 2nd to last information_message does not accumulate the totals

    Java Code:
    import javax.swing.*;  //impors java gui for dialog boxes ect
    import java.text.*;	//imports java texts for text formatting
    import java.io.*;	//imports input output detection and information (i think i need this)
    
    public class test	//defines class name and access
    {
      public static void main(String[] args)	//begins body
      {
      	String agebox, namebox; //assigning variables names and datatypes
      	double fee, age, tax, total, ffee, fage, ftax, ftotal;
      	
      	fee = 20.50; //initialising variables that require manual initialisation
      	ffee = 0;
      	fage = 0;
      	ftax = 0;
      	ftotal = 0;
      	
      	DecimalFormat num = new DecimalFormat(",##.00"); //formatting the text
      	
    	namebox =JOptionPane.showInputDialog("Enter Member's Name, leave blank to exit member entry ");	// user input for name
    	int len=namebox.length(); //declaring len as lenght of the namebox string
    	while (len !=0)
    	{
       	agebox =JOptionPane.showInputDialog("Enter Member's Age: ");	//user input for age
      	age = Double.parseDouble(agebox);  	// assign age as a double for if-else chain             
                    
      	if (age <= 7 )	//if-else chain for the member's fee
      	  	  	{JOptionPane.showMessageDialog(null,"Warning Member's age is too low to leave unsurpervised",
      			"WARNING!",
      			JOptionPane.WARNING_MESSAGE);}  //warn operater of young age			
      	else if (age <=17)
      	{ fee = 20.50;}  	
      	else if (age <=21)
      	{ fee = 32.25;}  		
      	else
      	{ fee = 45.75;}	//end of if else chain
      	
      	tax = fee*10/100; //calculating tax and assigining it to a variable
      	total = tax + fee; // adding fee and tax and assiginging it to a variable
      	
      			   JOptionPane.showMessageDialog(null, 
                   "The member's name is " + namebox + "\nThe Member's age is " + agebox + "\nMember's fee is " + fee + "\nMember's tax is " + tax + "\nMember's fee including tax is " + total,
                   "Gym Program", 
                    JOptionPane.INFORMATION_MESSAGE);	//display's information of the current member
       	namebox =JOptionPane.showInputDialog("Enter Member's Name, leave blank to exit member entry ");	// user input for name
    	len=namebox.length(); //declaring len as length of the namebox string
    	fage=age++;
    	ffee=fee++;
    	ftax=tax++;
    	ftotal=total++; 	
       	}   	
       	JOptionPane.showMessageDialog(null, 
                   "Today's totals are..." + "\ntotal age is: " + fage + "\ntotal fee is " + ffee + "\ntotal tax is: " + ftax + "\ntotal including tax is: " + ftotal,
                   "Gym Program", 
                    JOptionPane.INFORMATION_MESSAGE);	//display's information of the current member
    	JOptionPane.showMessageDialog(null,"Thanks for using the program","beyatch",JOptionPane.INFORMATION_MESSAGE);
         System.exit(0);  
      }
    }
    thanks again everyone

  10. #10
    Phobos0001 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default Finnished

    well ive finaly finnished my program, so no further help required, incase anyone wants to see it and tell me how much it sux....here it is..
    Java Code:
    import javax.swing.*;  //impors java gui for dialog boxes ect
    import java.text.*;	//imports java texts for text formatting
    import java.io.*;	//imports input output detection and information, such as string length
    public class test	//defines class name and access rihts, public=everyone
    {
      public static void main(String[] args)	//begins body
      {
      	String agebox, namebox; //assigning variables names and datatypes
      	double fee, age, tax, total, ffee, fage, ftax, ftotal, average; 
      	int count;
      	fee = 20.50; //initialising variables that require manual initialisation
      	ffee = 0;
      	fage = 0;
      	ftax = 0;
      	ftotal = 0; 
      	count = 0;
      	average = 0;
      	DecimalFormat num = new DecimalFormat(",##.00"); //formatting the text  	
    	namebox =JOptionPane.showInputDialog("Enter Member's Name, leave blank to exit member entry ");	// user input for name
    	int len=namebox.length(); //declaring len as lenght of the namebox string
    	while (len !=0)
    	{
       	agebox =JOptionPane.showInputDialog("Enter Member's Age: ");	//user input for age
      	age = Double.parseDouble(agebox);  	// assign age as a double for if-else chain
      	if (age <= 7 )	//if-else chain for the member's fee
      	  	  	{JOptionPane.showMessageDialog(null,"Warning Member's age is too low to leave unsurpervised",
      			"WARNING!",
      			JOptionPane.WARNING_MESSAGE);}  //warn operater of young age			
      	else if (age <=17)
      	{ fee = 20.50;}  	
      	else if (age <=21)
      	{ fee = 32.25;}  		
      	else
      	{ fee = 45.75;}	//end of if else chain  	
      	tax = fee*10/100; //calculating tax and assigining it to a variable
      	total = tax + fee; // adding fee and tax and assiginging it to a variable  	
      			   JOptionPane.showMessageDialog(null, 
                   "The member's name is " + namebox + "\nThe Member's age is " + agebox + "\nMember's fee is " + fee + "\nMember's tax is " + tax + "\nMember's fee including tax is " + total,
                   "Gym Program", 
                    JOptionPane.INFORMATION_MESSAGE);	//display's information of the current member
       	namebox =JOptionPane.showInputDialog("Enter Member's Name, leave blank to exit member entry ");	// user input for name
    	len=namebox.length(); //declaring len as length of the namebox string
    	fage=age+fage;
    	ffee=fee+ffee;
    	ftax=tax+ftax;
    	ftotal=total+ftotal;
    	count++;
    	average=fage/count;
       	}   	
       	JOptionPane.showMessageDialog(null, 
                   "Today's totals are..." + "\ntotal age is: " + fage + "\ntotal fee is " + ffee + "\ntotal tax is: " + ftax + "\ntotal including tax is: " + ftotal + "\nAmmount of costimers today: " + count + "\nAverage age: " + average,
                   "Gym Program", 
                    JOptionPane.INFORMATION_MESSAGE);	//display's information of the current member
    	JOptionPane.showMessageDialog(null,"Thanks for using the program","beyatch",JOptionPane.INFORMATION_MESSAGE);
         System.exit(0);
         }
    }

Similar Threads

  1. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 01:56 AM
  2. Replies: 0
    Last Post: 11-20-2007, 05:59 PM
  3. Replies: 1
    Last Post: 08-03-2007, 11:52 AM
  4. A loop that doesn't loop
    By MichYer in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 09:44 AM
  5. terminating a while loop with a string
    By tkdvipers in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 12:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •