Forum Stats

  • 3,826,348 Users
  • 2,260,632 Discussions
  • 7,896,912 Comments

Discussions

Unable to create string array

supertoy
supertoy Member Posts: 1
edited Mar 24, 2018 5:02PM in New To Java

I'm still pretty new to Java so arrays still have me troubled.

What I'm trying to do: Have the user input an unknown number of string entries and then print out the array. This is part of a larger task where I will loop over each entry and perform a task. But for now I'm just trying to get the array working.

What is expected: User enters Apple Banana Lemon and the output shows:

Apple

Banana

Lemon

What is happening is:User enters Apple Banana Lemon and the output shows:

Apple Banana Lemon

When I hard code the array my array prints correctly (What is expected) when I input the values using Scanner I get the other results (What is happening).

This produces the desired output:

import java.lang.String;

public class fruit {

   public static void main(String[] args) {

  String fruits[] = {"apple", "banana", "lemon"};

   for (int i = 0; i < fruits.length; i++)

  {

  System.out.println(fruits[i]);

  }

  }

}

apple

banana

lemon

Process finished with exit code 0

This produces the incorrect output:

import java.util.*;

import java.lang.String;

public class fruits {

   public static void main(String[] args) {

  Scanner sc = new Scanner(System.in);

  System.out.println("Enter some fruit: ");

  String name = sc.nextLine();

  String fruits[] = {name};

   for (int i = 0; i < fruits.length; i++)

  {

  System.out.println(fruits[i]);

  }

  }

}

Enter some fruit:

apple banana lemon

apple banana lemon

Process finished with exit code 0

Built with JetBrains IntelliJ

java version "1.8.0_161"

Java(TM) SE Runtime Environment (build 1.8.0_161-b12)

Tagged:

Answers

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Mar 24, 2018 2:19PM

    Try splitting the input line by some delimiter.

       public static void main(String[] args)

       {

          Scanner sc = new Scanner(System.in);

          System.out.println("Enter some fruits [separator is semicolon]:  ");

          String name = sc.nextLine();

          String[] fruits = name.split(";");

          for (int i = 0; i < fruits.length; i++)

          {

             System.out.println(fruits[i]);

          }

       }

    output:

    Enter some fruits [separator is semicolon]: 

    apple;banana;kiwi

    apple

    banana

    kiwi

  • Unknown
    edited Mar 24, 2018 5:02PM
    I'm still pretty new to Java so arrays still have me troubled.

    Ok - but your problem has NOTHING to do with 'arrays'. But to learn about arrays I suggest you read the trails in The Java Tutorials.

    https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

    Those tutorials cover ALL of the basic functionality and have WORKING code examples.

    1. read a trail

    2. try the examples in the trail

    3. modify the example code in some small way

    4. use a gui debugger (e.g. NetBeans) to step thru the code line by line to see what it does

    The above is how you learn.

    What I'm trying to do: Have the user input an unknown number of string entries

    Except your code is NOT doing that

    What is happening is:User enters Apple Banana Lemon and the output shows:Apple Banana Lemon

    What you just said is the user enters ONE STRING whose contents are 'Apple Banana Lemon'. So of course if you print that one string you will get one string.

    and then print out the array.

    And that is EXACTLY what your code does.

    String name = sc.nextLine();String fruits[] = {name};. . .for (int i = 0; i < fruits.length; i++)

    The first line reads ONE STRING.

    The second line puts that ONE STRING into an array

    The third line loops for the length of the array which is ONE.

    The Scanner.nextLine() method reads ONE LINE as ONE STRING.

    The Java API for the Scanner class presents ALL of the methods and tells you what they do and how to use them.

    A simple web search for 'java scanner' returns a link to the Java API as the VERY FIRST RESULT

    https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

    nextLine

    public String nextLine()

    Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

    See the above showing that 'nextLine' returns ONE string?

    There is a trail in The Java Tutorials about Scanners and how to use them.

    https://docs.oracle.com/javase/tutorial/essential/io/scanning.html

    You won't get very far trying to use Java if you don't read the docs and do some tutorials that teach you how to use the functionality.

    The sooner you start doing that the fewer problems/questions/issues you will have and FASTER you will be able to write good code.

This discussion has been closed.