21 Replies - 8756 Views - Last Post: 31 March 2006 - 10:07 PM
#1
Java assignment help?
Posted 27 March 2006 - 01:46 PM
Here is the assignment
Assignment Requrirements:
Create a program that takes a name and a number as command line String arguments. Have a method in the class that returns whether that number is equal to the length of the name. The program should:
1. Capitalize the first letter of the name.(pg272)
2. Output the capitalized name to the screen
3. Output whether the number was equal to the length of the name.
HINT: The number must be converted to int (pg 270-271)
Example: >java ChNineAsgn danica 7
The name capitalized is: Danica
The number is not equal to the length of the name.
any help will be well recieved thanks.
Replies To: Java assignment help?
#2
Re: Java assignment help?
Posted 27 March 2006 - 05:24 PM
Having said that, here is some base code to get you started:
public class cmdLineArgs{
public static void main (String[] args) {
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}
As you can see, that code will print out any command line arguments that have been passed to the program. Now you need to
- Access each command line argument supplied
- Manipulate where necessary
#3
Re: Java assignment help?
Posted 27 March 2006 - 06:45 PM
/**
*
*
*/
public class cmdLineArgs
{
public static void main (String[] args)
{
String firstName = args[0];
System.out.println("Enter a name and a number");
System.out.println(" + firstname + ");
char c = Character.toUpperCase('a');
char c1 = firstName.charAt(0);
firstName = Character.toUpperCase© + firstName;
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}
I'm getting a run time error it says
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at cmdLineArgs.main(cmdLineArgs.java:10)
Press any key to continue...
I'm still working on it I'm new and having some trouble getting the syntax and layout of writing my own programs thanks for the help.
#4
Re: Java assignment help?
Posted 27 March 2006 - 08:07 PM
import java.io.*;
public class cmdlin {
public static void main (String[] args) {
String fname = args[0];
int num = Integer.parseInt(args[1]);
System.out.println(fname);
System.out.println(num);
}
}
Here is a further hint...for capitalization, check out the substring method of the string class. For the length, compare the length of the string object to the number entered by the user.
#5
Re: Java assignment help?
Posted 28 March 2006 - 02:40 AM
Character.toUpperCase + firstName
that is adding a string to your method
Here is what you need to do:
1. Capitalize the first letter of the name.(pg272)
- Substring the first character out to its own string
- Make it uppercase
2. Output the capitalized name to the screen
- Substring everything but the first name out into its own string
- Concatenate the first character(now in uppercase) with the string that is everything but the first char.
3. Output whether the number was equal to the length of the name.
- just use basic logic to check if the number is greater than the length
This is a decent site for learning about substring :]
http://mindprod.com/.../substring.html
#6
Re: Java assignment help?
Posted 28 March 2006 - 01:12 PM
[CODE]
/**
*
*
*/
import java.io.*;
public class cmdLineArgs
{
public static void main (String[] args)
{
EasyReader console = new EasyReader();
System.out.print("Enter a name:");
String firstName = console.readLine();
System.out.println("" + firstName +"" );
char c1 = Character.toUpperCase('a');
String firstname = console.readLine();
char c = firstName.charAt(0);
firstName = Character.toUpperCase© + firstname.substring(1);
}
}
Here is the error
Enter a name:hello
hello
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: -1
at java.lang.String.substring(String.java:1768)
at java.lang.String.substring(String.java:1735)
at cmdLineArgs.main(cmdLineArgs.java:20)
Press any key to continue...
I think I'm having trouble telling it how to Capitilize the first letter (char c1 = firstName is a problem not sure how it should be?
This post has been edited by chet123: 28 March 2006 - 01:13 PM
#7
Re: Java assignment help?
Posted 28 March 2006 - 01:21 PM
This post has been edited by Amadeus: 28 March 2006 - 01:32 PM
#8
Re: Java assignment help?
Posted 28 March 2006 - 01:30 PM
you don;t really need any variables for this assignment, but we'll use them to make things clear.
You should have a string that holds the name, which is supplied as the first argument...the one in array index 0, correct?
String fname = args[0];
You also need an integer that is the number supplied as the second argument...keep in mind that a command line argument is text, so you will have to convert it to a number:
int num = Integer.parseInt(args[1]);
Compare the number (num) to the length of the name (fname)...strings have a property called length, which you can access for the comparison...the length of the name is fname.length().
Based on the result of the comparison, you can output the appropriate message.
The String Class has a method called substring()...this can be used to get portions of the string. you want to capitalize the first letter
fname.substring(0,1).toUpperCase
This will return the capitalized first letter (please see link for how the substring method works)
You then want to display the capital letter, then the rest of the word without the capital letter.
fname.substring(0,1).toUpperCase() + fname.substring(1)
the second part will return the string, minus the first letter.
Then, it's just a matter of putting it together.
#9
Re: Java assignment help?
Posted 28 March 2006 - 02:20 PM
This post has been edited by chet123: 28 March 2006 - 02:21 PM
#10
Re: Java assignment help?
Posted 28 March 2006 - 03:10 PM
#11
Re: Java assignment help?
Posted 28 March 2006 - 03:56 PM
this is how you ask for it at
System.out.print("enter a name and a number")
should this be first in the program?
and do i need
EasyReader console = new EasyReader();in order to get the program to take input?
System.out.println(" + firstname + ");
can I add toUpperCase in this line
[CODE](" + firstName.toUpperCase.substring(1);
#12
Re: Java assignment help?
Posted 28 March 2006 - 09:50 PM
public class Input
{
public static void main(String[] args)
{
EasyReader console = new EasyReader();
System.out.print("Enter a Name");
String firstName = console.readLine();
char c = firstName.charAt(0);
firstName = Character.toUpperCase(c) + firstName.substring(1);
System.out.println("" + firstName + "");
}
}
This post has been edited by chet123: 28 March 2006 - 09:52 PM
#13
Re: Java assignment help?
Posted 28 March 2006 - 10:23 PM
#14
Re: Java assignment help?
Posted 28 March 2006 - 10:30 PM
If so than I'm making it harder than it has to be.
So I could use any name and number in the program because I thought that it had to have a user's input after running?
This post has been edited by chet123: 28 March 2006 - 10:52 PM
#15
Re: Java assignment help?
Posted 29 March 2006 - 04:44 AM
Quote
Create a program that takes a name and a number as command line String arguments.
The description above means that you must supply the name and length at runtime...like so:
java myprogram charles 7
Unless there is more to the assignment, you do not need to prompt the user for the information.
|
|

New Topic/Question
Reply



MultiQuote




|