Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Beginner Problem

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner Problem

    Hello, I tried some exercises: Entering a first and a lastname, the return should be the last name in uppercase.
    Example:
    input: John Doe
    output: John DOE

    unfortunately i get a "null null", where is my mistake??

    import java.util.Scanner;
     
    public class A3 {
     
    	static String inData;
    	static String firstname;
    	static String lastname;
     
    	public static void main(String[] args){
     
    		Scanner scan = new Scanner(System.in);
    		inData=scan.next();
    		for(int index=0; index<inData.length(); index++){
    			if(inData.charAt(index) == ' ')
    			{
    				firstname=inData.substring(0, index-1);
    				lastname=inData.substring(index+1, inData.length());
    				lastname=lastname.toUpperCase();
    			}
     
    			else {
    			}
     
    			}
    		System.out.println(firstname + " " + lastname);
    		}
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginner Problem

    Add some more printlns to show what the code is doing and what values you are working with.
    For example print out the value of inData immediately after it is read in:
    System.out.println("inData=" + inData + "<");

    How many lines does your program print out? Can you copy and paste here the full contents of the screen from you test.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Problem

    I found the error: I had to use scan.nextLine(); instead of scan.next();
    No it works, thanks for your reply.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginner Problem

    Yes those methods will return different data. Which one to use depends on your application.

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Problem

    when comparing strings use .equals function or other but not ==

    Keep the code coming!
    Eric

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Beginner Problem

    Quote Originally Posted by ericthegreat View Post
    when comparing strings use .equals function or other but not ==
    Huh? where in the code did he do that?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Beginner Problem

    Where in the posted code does the OP use == to compare Strings?
    Improving the world one idiot at a time!

  8. #8
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Beginner Problem

    Woo I was faster!
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. ServletExample code problem - beginner
    By Sawubona in forum Java Servlet
    Replies: 5
    Last Post: July 4th, 2011, 05:57 AM
  2. Beginner method call problem
    By Lasda in forum Java Theory & Questions
    Replies: 2
    Last Post: May 10th, 2011, 03:31 PM
  3. beginner and need simple help. please!
    By alal12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 19th, 2010, 05:38 PM
  4. Beginner Problem
    By nve5009 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 19th, 2010, 11:29 AM
  5. I need a help ! i am beginner
    By yinky in forum Java Theory & Questions
    Replies: 3
    Last Post: September 30th, 2009, 07:22 AM