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 6 of 6

Thread: JAVA - Get array of String from input

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default JAVA - Get array of String from input

    Hello,

    First element (Zero element) in myString array always is null. Why?
    Thanks

    import java.util.*;
     
     
    class MainClass { 
     
     
    	public static void main(String[] args) 
    	{ 
    		Scanner S = new Scanner (System.in);
    		int num = S.nextInt();
     
    		String[] myString = new String[num];
     
    		for	(int i = 0 ; i < num ; i++)
    		{
    			myString[i] = S.nextLine();
    		}
    		String[] MY = null;
    		for (int i = 0 ; i < num ; i++)
    		{
    				MY = myString[i].split(" ");
    			for(int j = 0 ; j < MY.length; j++) {
    			char[] myChar = MY[j].toCharArray();
    			if (myChar[0] >= 97 && myChar[0] <= 122)
    			{
    				myChar[0]-=32;
    			}
    			MY[j] = new String(myChar);
    			}
    			myString[i] = String.join(" ", MY);
    		}
    		for	(int i = 0 ; i < num ; i++)
    		{
    			System.out.println(myString[i]);
    		}
    	}
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JAVA - Get array of String from input

    Can you post some output that shows the value of null in the array?

    The problem is with the Scanner class's way on handling end-line characters.
    Read this for more details: https://www.geeksforgeeks.org/why-is...ext-functions/
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    shervan_360 (June 19th, 2020)

  4. #3
    Junior Member
    Join Date
    Jun 2020
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: JAVA - Get array of String from input


  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JAVA - Get array of String from input

    Please copy the output and paste it here as text, not as a image. Text can not be copied from an image for including in a response.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    shervan_360 (June 19th, 2020)

  7. #5
    Junior Member
    Join Date
    Jun 2020
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: JAVA - Get array of String from input

    I reffered to the introduced link
    The problem is solved with your guidance.

    Thanks

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JAVA - Get array of String from input

    Ok,I'm glad you have solved the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    shervan_360 (June 19th, 2020)

Similar Threads

  1. Java help with String Input and Output and casting
    By jadenrocks12 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 2nd, 2014, 05:21 PM
  2. [SOLVED] How To Compare String input to char Array....
    By Praetorian in forum Java Theory & Questions
    Replies: 13
    Last Post: February 12th, 2014, 03:34 AM
  3. [SOLVED] Sorting an object array using string variables from a string array
    By Shadud in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 05:50 PM
  4. Placing user input to a array and to then display it as a string
    By LaliB in forum Collections and Generics
    Replies: 5
    Last Post: January 12th, 2012, 11:41 AM
  5. Taking Input in String Array
    By syehjafa in forum Collections and Generics
    Replies: 8
    Last Post: July 25th, 2011, 07:18 AM