Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-13-2007, 01:29 PM
Member
 
Join Date: Nov 2007
Posts: 20
Phobos0001 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-13-2007, 01:32 PM
Member
 
Join Date: Nov 2007
Posts: 20
Phobos0001 is on a distinguished road
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 )
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-13-2007, 05:51 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
I'm not 100% 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:

Code:
int length = s.length();
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-13-2007, 09:56 PM
Member
 
Join Date: Nov 2007
Posts: 20
Phobos0001 is on a distinguished road
thanks for the reply mate,

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

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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-14-2007, 12:54 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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){}
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-14-2007, 04:42 AM
Member
 
Join Date: Nov 2007
Posts: 20
Phobos0001 is on a distinguished road
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

thanks a ton, much apriciated
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-14-2007, 09:16 AM
Member
 
Join Date: Nov 2007
Posts: 20
Phobos0001 is on a distinguished road
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
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
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-14-2007, 09:51 AM
Member
 
Join Date: Nov 2007
Posts: 20
Phobos0001 is on a distinguished road
ok i think ive finaly got it and all i should need to do now is put this into the main program....

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

thanks again for the help everyone, hopefully i can get this finnished soon
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-14-2007, 11:22 AM
Member
 
Join Date: Nov 2007
Posts: 20
Phobos0001 is on a distinguished road
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
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..
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

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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 11-14-2007, 04:44 PM
Member
 
Join Date: Nov 2007
Posts: 20
Phobos0001 is on a distinguished road
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..
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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
while loop michcio New To Java 5 01-27-2008 02:56 AM
Using java.util.Scanner to search for a String in a String Java Tip Java Tips 0 11-20-2007 06:59 PM
I can't seem to pass the value of a string variable into a string array mathias Java Applets 1 08-03-2007 12:52 PM
A loop that doesn't loop MichYer New To Java 2 07-30-2007 10:44 AM
terminating a while loop with a string tkdvipers New To Java 3 07-10-2007 01:23 AM


All times are GMT +3. The time now is 09:44 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org