Results 1 to 9 of 9
  1. #1
    john_java is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Not sure hot to finish this Java code..

    I've been trying to edit complete this code for like 40 minutes now.
    The assignment that I am trying to do is Exercise number 2 from here: Programming Exercises
    This is what i've got so far:

    Java Code:
    package chapter29;
    import java.util.Scanner;
    public class Ex2 
    {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		Scanner scan = new Scanner(System.in);//makes a scanner so we can get info in from user
    		
    		System.out.print("Enter a string: "); //tells user to enter their name
    		String name = scan.nextLine(); //actually take that entered info and store into a variable called name (variable type needs to match)
    		name = name.trim();
    		
    		System.out.println(name.substring(0, 1)+ "\n" + (name.substring(1, 2 )));
    		
    		for (int i = 0 ....)
    			System.out.println(name.charAt(i));
    	
    	}
    	
    }
    I'm not sure hot to finish the last few lines there.

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

    Default Re: Not sure hot to finish this Java code..

    Quote Originally Posted by john_java View Post
    I've been trying to edit complete this code for like 40 minutes now.
    The assignment that I am trying to do is Exercise number 2 from here: Programming Exercises
    This is what i've got so far:

    Java Code:
    package chapter29;
    import java.util.Scanner;
    public class Ex2 
    {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		Scanner scan = new Scanner(System.in);//makes a scanner so we can get info in from user
    		
    		System.out.print("Enter a string: "); //tells user to enter their name
    		String name = scan.nextLine(); //actually take that entered info and store into a variable called name (variable type needs to match)
    		name = name.trim();
    		
    		System.out.println(name.substring(0, 1)+ "\n" + (name.substring(1, 2 )));
    		
    		for (int i = 0 ....)
    			System.out.println(name.charAt(i));
    	
    	}
    	
    }
    I'm not sure hot to finish the last few lines there.
    Suggestions:
    • Please print the assignment text in the forum, not a link
    • Please ask a specific question. What exactly are you confused about?

  3. #3
    john_java is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: Not sure hot to finish this Java code..

    Quote Originally Posted by Fubarable View Post
    Suggestions:
    • Please print the assignment text in the forum, not a link
    • Please ask a specific question. What exactly are you confused about?
    Sorry, and i'm not sure how to create that loop.

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

    Default Re: Not sure hot to finish this Java code..

    Quote Originally Posted by john_java View Post
    Sorry, and i'm not sure how to create that loop.
    What loop? What are your assignment instructions?

  5. #5
    john_java is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: Not sure hot to finish this Java code..

    the assignment:
    Write a program where the user enters a string, and the program echos it to the monitor with one character per line:

    C:\>java LinePerChar
    Enter a string:
    Octopus

    O
    c
    t
    o
    p
    u
    s
    To do this you will need to use the following method from class String:

    char charAt( int inx )
    This method returns the character that is at index inx of the String. Characters are indexed beginning at index 0.
    My question is: a word can contain 2 letters, or 20 letters. How do i create the loop for that?

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

    Default Re: Not sure hot to finish this Java code..

    Quote Originally Posted by john_java View Post
    the assignment:

    My question is: a word can contain 2 letters, or 20 letters. How do i create the loop for that?
    That's a good question actually, but you'll be able to find out how many letters by using another String method, the length() method. So do your for loop from 0 to < the String's length.

  7. #7
    john_java is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: Not sure hot to finish this Java code..

    Quote Originally Posted by Fubarable View Post
    That's a good question actually, but you'll be able to find out how many letters by using another String method, the length() method. So do your for loop from 0 to < the String's length.
    I don't get it *_*

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

    Default Re: Not sure hot to finish this Java code..

    Quote Originally Posted by john_java View Post
    I don't get it *_*
    OK, let's see what you currently understand...
    Can you show me a for loop that will loop 4 times?

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

    Default Re: Not sure hot to finish this Java code..

    Quote Originally Posted by Fubarable View Post
    OK, let's see what you currently understand...
    Can you show me a for loop that will loop 4 times?
    If you can show me this, I might be able to walk you through this... up to you.

Similar Threads

  1. Replies: 3
    Last Post: 04-24-2011, 11:06 AM
  2. Replies: 8
    Last Post: 06-06-2010, 03:10 PM
  3. how to finish this?
    By xpandaxlover in forum New To Java
    Replies: 2
    Last Post: 04-09-2010, 07:43 PM
  4. how to finish this?
    By xpandaxlover in forum New To Java
    Replies: 1
    Last Post: 04-09-2010, 07:42 PM
  5. Finish Java Application
    By ojsimon in forum Jobs Offered
    Replies: 8
    Last Post: 12-07-2009, 04:25 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
  •