Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 544 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.5K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 439 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Unable to create string array

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)
Answers
-
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
-
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.