Results 1 to 20 of 23
Thread: small code, need help
- 01-21-2010, 09:30 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
- 01-21-2010, 09:43 AM #2
You first show the code what you tried and ask for suggestion/solution.
Ramya:cool:
- 01-21-2010, 09:46 AM #3
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); } } }
- 01-21-2010, 09:50 AM #4
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
thnx brol :)
- 01-21-2010, 01:03 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
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;
}
}
}
}
- 01-21-2010, 05:29 PM #6
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
- 01-21-2010, 06:26 PM #7
Member
- Join Date
- Jan 2010
- Posts
- 46
- Rep Power
- 0
I think validation is usually best using a DO WHILE
not 'if's' !
orJava 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'));
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.
- 01-21-2010, 07:03 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 01-21-2010, 07:22 PM #9
Member
- Join Date
- Jan 2010
- Posts
- 46
- Rep Power
- 0
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.
- 01-21-2010, 10:04 PM #10
Member
- Join Date
- Aug 2009
- Posts
- 76
- Rep Power
- 0
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.
- 01-22-2010, 01:23 AM #11
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
Looks like everything alright but im get error? any ideas?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); } }
-
Last edited by Fubarable; 01-22-2010 at 02:47 AM.
-
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?
- 01-22-2010, 03:04 AM #14
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
yes thats the problem that i want to make GUI, but cant understand how to change it
-
How to change what? Your assignment doesn't sound like it requires or that it should even be a GUI.
- 01-22-2010, 03:20 AM #16
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
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
-
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?
- 01-22-2010, 04:11 AM #18
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
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.
-
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!
-
Last edited by Fubarable; 01-22-2010 at 04:24 AM.
Similar Threads
-
i have a small error but i can't figure out what is wrong [code provided]
By blueduiker in forum New To JavaReplies: 3Last Post: 01-11-2010, 06:48 AM -
small doubt
By ravi kumar in forum Java SoftwareReplies: 1Last Post: 08-02-2009, 03:50 PM -
small problem
By barusk in forum NetworkingReplies: 4Last Post: 03-21-2009, 06:19 AM -
Urgent small code
By karingulanagaraj in forum New To JavaReplies: 7Last Post: 08-11-2008, 04:11 AM -
Small problem
By ayoood in forum New To JavaReplies: 2Last Post: 06-06-2008, 12:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks