Results 1 to 20 of 26
- 05-28-2008, 01:56 PM #1
Easy program...need help (urgent)
My program is supposed to ask a user for their first and last name. And, then return results.
One result is the first and last initials are the same, then it would return the user's name in uppercase.
And, if their initials are different then it would return the user's name in lowercase.
I have to use these methods:
Java Code:toUpperCase()
Java Code:toLowerCase()
I am a Java n00b.
- 05-28-2008, 02:17 PM #2
Please can somebody help me. I am lost.
I am a Java n00b.
- 05-28-2008, 02:21 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 05-28-2008, 02:24 PM #4
I keep getting this errorJava Code:import java.util.Scanner; import acm.graphics.*; import acm.program.GraphicsProgram; import java.awt.Color; import java.awt.event.*; import MyStaticMethods.Prog1; import java.util.*; import java.text.DecimalFormat; public class MrInitials { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); { String result; System.out.print(" enter your first initial "); int f = scanner.nextInt(); System.out.print(" enter last initial "); int l = scanner.nextInt(); System.out.print(" enter your whole first name "); String first = scanner.nextLine(); System.out.print(" enter your whole last name "); String last = scanner.nextLine(); System.out.print(" Are you first and last initials the same? "); String answer = scanner.nextInt(); if(answer = "yes") { first.toUpperCase(); last.toUpperCase(); } else { first.toLowerCase(); last.toLowerCase(); } result = "Your name is " + first + last; System.out.println(result); } } }
Java Code:H:\Java Programs\Final Review\MrInitials.java:29: incompatible types found : java.lang.String required: boolean if(answer = "yes") ^ 1 errorI am a Java n00b.
- 05-28-2008, 02:26 PM #5
I am not even doing it right. I am taking the easy way. If you would can you modify my code for it to find the initials instead of me asking if they are the same?
I am a Java n00b.
- 05-28-2008, 02:43 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Change it like this.
Java Code:if(answer.equals("Yes"))
- 05-28-2008, 04:02 PM #7
Why not instead of having the user input their initials just take the first character of each string and compare them with the .equals. Remember the first character is in the 0 position.
My IP address is 127.0.0.1
- 05-28-2008, 08:05 PM #8
- 05-28-2008, 08:21 PM #9
Member
- Join Date
- Apr 2007
- Location
- USA
- Posts
- 50
- Rep Power
- 0
To get the first character of the string use the substring() method of the String class. Check the API:
String (Java 2 Platform SE 5.0)
- 05-28-2008, 08:28 PM #10
Sorry, I still don't understand where to put it. Sorry, I have only been programming a couple months.
I am a Java n00b.
- 05-28-2008, 08:35 PM #11
do if(firstname.substring(0).equals(lastname.substrin g(0)).
My IP address is 127.0.0.1
- 05-28-2008, 09:39 PM #12
Member
- Join Date
- May 2008
- Posts
- 21
- Rep Power
- 0
u can use first.equalsIgnoreCase(last)
and then convert first an last accordinglyget new coding problems at CODERWORDS
- 05-28-2008, 09:41 PM #13
Member
- Join Date
- May 2008
- Posts
- 21
- Rep Power
- 0
- 05-29-2008, 03:24 AM #14
Thanks guys. I will try this and post results. I appreciate the help.
I am a Java n00b.
- 05-29-2008, 02:02 PM #15
I can't get it to work all the way, I ran out of time anyway for this project. Thanks for the help guys. I will still get a good grade for making this far. Thanks.
And yes, my teacher lets us use anything on the internet (including forums, so this isn't cheating, lol).
Here is as far as I got:
Java Code:import java.util.Scanner; import acm.graphics.*; import acm.program.GraphicsProgram; import java.awt.Color; import java.awt.event.*; import MyStaticMethods.Prog1; import java.util.*; import java.text.DecimalFormat; public class MrInitials { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String result; System.out.print(" enter your first name "); String first = scanner.nextLine(); System.out.print(" enter your last name "); String last = scanner.nextLine(); if(first.substring(0).equals(last.substring(0))) { first.toUpperCase(); last.toUpperCase(); result = "Your name is " + first + last; } else { first.toLowerCase(); last.toLowerCase(); result = "Your name is " + first + last; } System.out.println(result); } }I am a Java n00b.
- 05-29-2008, 02:08 PM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you briefly tell me what you really wants to do?
- 05-29-2008, 02:09 PM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 05-29-2008, 02:39 PM #18
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
@Zebra
Here is the error you have made.
Passing argument in wrong way. If you give one argument at the substring() it is the beginning index. So sub string derived is up to the end of the string.Java Code:if(first.substring(0).equals(last.substring(0)))
Java Code:import java.util.Scanner; /** * * @author Eranga Tennakoon */ public class StringTest { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner ss = new Scanner(System.in); System.out.println("String one: "); String str1 = ss.next(); System.out.println("String two"); String str2 = ss.next(); if(str1.substring(0, 0).equals(str2.substring(0, 0))) { System.out.println("Your name is : " + str1.toUpperCase() + " " + str2.toUpperCase()); } else { System.out.println("Your name is : " + str1.toLowerCase() + " " + str2.toLowerCase()); } } }
- 05-30-2008, 04:20 AM #19
just wondering why did you import all those classes?
You just got T4NK3D!
Adonrcicg to rcesraeh the haumn mnid is clbapae of rdanieg tihs. -Jvaa Sun Moricyssemts-
- 05-30-2008, 04:24 AM #20
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
May be he has more things done on his code. Because some packages are not in Java libraries too. :)
Similar Threads
-
urgent:calling c program from java
By rach in forum New To JavaReplies: 6Last Post: 01-19-2010, 11:37 AM -
Noob question- easy
By mattonitto in forum New To JavaReplies: 7Last Post: 06-13-2008, 12:26 AM -
What does this mean (Very Easy)
By Zebra in forum New To JavaReplies: 6Last Post: 05-01-2008, 01:46 PM -
help me with a realy easy program (substring)
By michcio in forum New To JavaReplies: 7Last Post: 01-27-2008, 12:41 AM -
Easy question
By JavaNoob in forum New To JavaReplies: 10Last Post: 08-03-2007, 10:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks