Results 1 to 3 of 3
Thread: Loop-y Blah
- 09-25-2011, 07:15 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 16
- Rep Power
- 0
Loop-y Blah
Say I need to type in my name - letter by letter, and then concatenate the input to form the whole name. All the while I am checking for numbers and marking for errors.
So for my first name, entered in as a string I have it down pat and it seems to be working fine. but i can't figure out how to have each letter entered in and then join them together
.gif)
Scanner keyboard = new Scanner(System.in);
System.out.println("What is your first name?");
firstName = keyboard.nextLine();
for(i=0; i<firstName.length();i++)
{
ch=firstName.charAt(i);
if (!(ch>=65 && ch <=90))
{
if(!(ch>=97 && ch<=122))
{
flag=1;
System.out.println("You've entered in an invalid name");
break;
}
}
}
System.out.println("Enter in your last name, letter by letter");
- 09-25-2011, 08:07 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
Re: Loop-y Blah
If you have a valid firstName and a valid lastName you can join the two together as follows:
But your question raises another question: what are those numbers 65, 90, 97 and 122 doing in your code?Java Code:String firstName= ...; String lastName= ...; String completeName= firstName+" "+lastName;
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-25-2011, 08:16 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 PM -
[Q] Loop issue (while loop)
By iriscience in forum New To JavaReplies: 9Last Post: 01-31-2011, 04:21 PM -
JAVAC is not recognized blah blah....=(
By Lance87 in forum New To JavaReplies: 33Last Post: 12-20-2010, 08:09 PM -
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04-30-2010, 05:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks