Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    p0rnstar is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default small code, need help

    I need simple code,
    At first it should ask name (name should be between 2-15 letters) if its correct than ask for age ( between 18-65)
    if there is an error it should ask again. thanks

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    You first show the code what you tried and ask for suggestion/solution.
    Ramya:cool:

  3. #3
    o0oNorao0o's Avatar
    o0oNorao0o is offline Member
    Join Date
    Oct 2009
    Posts
    51
    Rep Power
    0

    Default

    Hope is that what you want
    The last part of your question i didn't do it sorry:o

    Java Code:
    import java.util.Scanner;
    public class name
    {
    	public static void main(String[]args)
    	{
    		Scanner keyboard = new Scanner (System.in);
    		String name;
    		
    		int age,name_length;
    		System.out.print("Type your name -should be betwwen 2-15 letters-  :");
    		name = keyboard.next();
    		name_length=name.length();
    		if (name_length>=2&&name_length<=15)
    		{
    			System.out.println("Your name : " +name);
    			
    		}
    		
    		System.out.print("Enter your age -should be betwwen 18-65 -  :");
    		age=keyboard.nextInt();
    		
    		if (age>=18&&age<=56)
    		{
    			System.out.println("Your age : " +age);
    			
    		}
    	}
    }

  4. #4
    p0rnstar is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    thnx brol :)

  5. #5
    852000 is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default I modified the code,hope help you

    import java.util.Scanner;
    public class name
    {
    public static void main(String[]args)
    {
    boolean flagA=true;
    boolean flagB=true;
    Scanner keyboard = new Scanner (System.in);
    String name;

    int age,name_length;
    while(flagA){
    System.out.print("Type your name -should be betwwen 2-15 letters- :");
    name = keyboard.next();
    name_length=name.length();
    if (name_length>=2&&name_length<=15)
    {
    System.out.println("Your name : " +name);
    flagA=false;
    }else{
    flagA=true;
    }
    }
    while(flagB){
    System.out.print("Enter your age -should be betwwen 18-65 - :");
    age=keyboard.nextInt();

    if (age>=18&&age<=56)
    {
    System.out.println("Your age : " +age);
    flagB=false;
    }else{
    flagB=true;
    }
    }
    }
    }

  6. #6
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    Quote Originally Posted by o0oNorao0o View Post
    Hope is that what you want
    The last part of your question i didn't do it sorry:o

    Java Code:
    import java.util.Scanner;
    public class name
    {
    	public static void main(String[]args)
    	{
    		Scanner keyboard = new Scanner (System.in);
    		String name;
    		
    		int age,name_length;
    		System.out.print("Type your name -should be betwwen 2-15 letters-  :");
    		name = keyboard.next();
    		name_length=name.length();
    		if (name_length>=2&&name_length<=15)
    		{
    			System.out.println("Your name : " +name);
    			
    		}
    		
    		System.out.print("Enter your age -should be betwwen 18-65 -  :");
    		age=keyboard.nextInt();
    		
    		if (age>=18&&age<=56)
    		{
    			System.out.println("Your age : " +age);
    			
    		}
    	}
    }
    This is not the best way of answering his question :)

  7. #7
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    I think validation is usually best using a DO WHILE

    not 'if's' !

    Java Code:
    Scanner input = new Scanner(System.in);
    String str;
    do
    {
       System.out.println("Do you like pie? Y/N");
       str=input.nextLine();
       if ( str.charAt(0).equals('Y'))
          break;
       else if ( str.charAt(0).equals('N'))
          System.out.println("What is wrong with you? choose Yes!");
    }
    while (! str.equals('Y')  ||  ! str.equals('N'));
    or
    Java Code:
    Scanner input = new Scanner(System.in);
    int i;
    do
    {
       System.out.printf("Menu\n1. chocolate\n2.pie\n3.ice-cream\n4.lollypop\n5.marshmallow\n");
       if (! input.hasNextInt())
       {
    
          System.out.printf("Invalid Option, Please enter a number in range.");
    
          input.nextLine();
    
       }
    }
    while (! input.hasNextInt()); 
    
    i = input.nextInt();
    Last edited by Newbie666; 01-21-2010 at 06:39 PM.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Newbie666 View Post
    I think validation is usually best using a DO WHILE

    not 'if's' !

    Java Code:
    Scanner input = new Scanner(System.in);
    String str;
    do
    {
       System.out.println("Do you like pie? Y/N");
       str=input.nextLine();
       if ( str.charAt(0).equals('Y'))
          break;
       else if ( str.charAt(0).equals('N'))
          System.out.println("What is wrong with you? choose Yes!");
    }
    while (! str.equals('Y')  ||  ! str.equals('N'));
    Guess what happens when the user doesn't type anything but just hits the <enter> key ...

    kind regards,

    Jos

  9. #9
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    Point, it would just print out again. (assuming the code is correct, I wrote it off the top of my head)
    What is wrong with you? choose yes!
    Do you like pie?



    My point is, loops help you achieve validation, if statements don't. OP needs to change code somewhat, and ignore some of the other code choices above.

  10. #10
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    Quote Originally Posted by Newbie666 View Post
    Point, it would just print out again. (assuming the code is correct, I wrote it off the top of my head)
    What is wrong with you? choose yes!
    Do you like pie?



    My point is, loops help you achieve validation, if statements don't. OP needs to change code somewhat, and ignore some of the other code choices above.
    It really doesn't matter which way you go. There are pros/cons to each. The above code should indeed have an else statement, but using if/else statements would be fine here. The key is to handle what happens if the user inputs something other than expected. Using an else forces you to think about this.

  11. #11
    p0rnstar is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    Java Code:
    import java.awt.Color;
    import javax.swing.*;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
      
    
    
    
    class RPGContents extends JPanel
    {
    	RPGContents()
    	{  
    			
     		String inputString  = JOptionPane.showInputDialog(null,"What is the name of your character?");
    		Scanner myScanner = new Scanner (inputString);
    		boolean questName=true;
    		boolean questAge=true;
    		
    		
    			int nameLength;
    			while(questName)
    			{
    			String name = myScanner.next();
    			nameLength=name.length();
    			if (nameLength>=3 == nameLength<=12)
    			{
    				
    			questName=false;
    			
    			}
    			else 
    			{
    				
    			questName=true;
    			
    			}
    			}
    			while(questAge)
    			{
    				
    			JOptionPane.showInputDialog(null,"Whats is the age of your character?");
    			int age = myScanner.nextInt();
    			
    			if (age>=18 == age<=56)
    			{
    				
    			questAge=false;
    			
    			}
    			else
    			{
    				
    			questAge=true;
    			} 
    			}
    			
    			//JLabel profile = new JLabel();
    			//this.add(profile); 
    			
    			
    			
    			
    			
    			}
    		
    		
    		
    }
    Looks like everything alright but im get error? any ideas?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by p0rnstar View Post
    Looks like everything alright but im get error? any ideas?
    You don't tell us what the errors are or what lines throw them, so I'm not sure who can give you ideas.

    I have to comment about your original post too. It's nothing more than a homework dump that blatantly asks for "the code". Please try to do better than this.
    Last edited by Fubarable; 01-22-2010 at 02:47 AM.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your code appears to be an unreliable mix of GUI and Scanner code. I think you need to decide up front will the program be a GUI, and if so, should you be using a Scanner object? If not, then should your class be subclassing JPanel?

  14. #14
    p0rnstar is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    yes thats the problem that i want to make GUI, but cant understand how to change it

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  16. #16
    p0rnstar is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    the problem is that i know how to make this project by scanner, so right now i have to change it to gui. as u see im trying to change while method, but cant get right result

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What specifically is the assignment? Word for word?

    I don't see much GUI code in your little bit of code other than a few JOptionPane calls mixed in with Scanner calls that shouldn't even be there. So what exactly are you trying to do?

  18. #18
    p0rnstar is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    If the player types name less than 3, or more than 12 letters, then ignore that name and ask them again.

    When the user has supplied a right character name, ask them to specify the age, using dialog again.

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So in essence, you're not creating a full-fledged GUI, but rather using a series of JOptionPanes instead of Scanner, correct?

    If so, and if you in fact have your program running with Scanner, then the first thing I would try to do would be to get rid of all references to Scanner in the little program and instead get all input from your JOptionPanes. Why not try that and then post back your attempt and any errors you encounter. The program structure should be very similar to the previous program, so this should be pretty straightforward.

    Best of luck and good night!

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by youngflames View Post
    Create a Java class file (source file) using BlueJ named House.java. In this class you will have the following three methods: ...
    young flames, please don't hijack someone else's thread. If you have your own question, then start your own thread.
    Last edited by Fubarable; 01-22-2010 at 04:24 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 01-11-2010, 06:48 AM
  2. small doubt
    By ravi kumar in forum Java Software
    Replies: 1
    Last Post: 08-02-2009, 03:50 PM
  3. small problem
    By barusk in forum Networking
    Replies: 4
    Last Post: 03-21-2009, 06:19 AM
  4. Urgent small code
    By karingulanagaraj in forum New To Java
    Replies: 7
    Last Post: 08-11-2008, 04:11 AM
  5. Small problem
    By ayoood in forum New To Java
    Replies: 2
    Last Post: 06-06-2008, 12:27 PM

Posting Permissions

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