0

Here is my code:

Scanner s = new Scanner(System.in);
int n = s.nextInt();
s.next();
String[] words = new String[n];

for ( int a = 0; a < n; a++ )
{
    words[a] = s.nextLine();
}  
for ( int a = 0; a < n; a++ )
{
    System.out.println(words[a]);
}

The program is about inputting a number of how many lines of string you will input.

Now when I input 2, the size of the words array will become 2, on the first for loop it will ask the user to enter a string N times. after that, the second for loop will print all the inputted words from the array, but it only outputs empty strings.

Example of unexpected output:

2
hello
world

Process completed

Expected output:

2
hello
world
hello
world

Process completed

i dont know why, but when i put some System.out.print() or System.out.println() in my program it works perfectly

Lorence Hernandez
  • 1,096
  • 12
  • 23
  • 1
    What is `s`? You have several different methods on it, but no idea what they do. – krillgar Jan 20 '16 at 12:32
  • @krillgar I would assume a `new Scanner(System.in)`. – assylias Jan 20 '16 at 12:32
  • What's the type of `s`? – Andrea Bergia Jan 20 '16 at 12:32
  • What exactly is `s` in your code? Can you post link to javadoc? – toKrause Jan 20 '16 at 12:36
  • @toKrause my bad, but the thread has been edited – Lorence Hernandez Jan 20 '16 at 12:38
  • @assylias hello , about the duplicated topic, I have read it but i dont think we have a similar problem – Lorence Hernandez Jan 20 '16 at 12:39
  • 1
    @LorenceHernandez replace `s.next();` with `s.nextLine();` - I then get your expected output for the given input. – Andy Turner Jan 20 '16 at 12:41
  • @LorenceHernandez You need to call `s.nextLine()` instead of `s.next()` after `s.nextInt()`. – assylias Jan 20 '16 at 12:42
  • When i do that and input 2 in nextInt(); the first forloop only enable me to input one string. its like after inputting the first string the loop skips the next words[a] = s.nextLine(); – Lorence Hernandez Jan 20 '16 at 12:48
  • This is not a dublicate , really I cant understand those people , here is your code , u just mess up 1st line with the 2nd.My solution is another scanner instance.Power users have to understand that conversatios for the easiest thing and maybe dublicate some times help us all... :( – Tsakiroglou Fotis Jan 20 '16 at 12:58
  • package StackOverFlow; import java.util.Scanner; /** * * @author fotis */ public class arraay { public static void main(String[] args){ Scanner s = new Scanner(System.in); int n = s.nextInt(); String[] words = new String[n]; System.out.println("Please enter " + n + " words."); for ( int a = 0; a < n; a++ ) { Scanner word=new Scanner(System.in); words[a] = word.nextLine(); } System.out.println("your input is :"); for ( int i = 0; i< n; i++ ) { System.out.println(words[i]); } } } – Tsakiroglou Fotis Jan 20 '16 at 12:58
  • @TsakiroglouFotis i tried your code but when i input 2 and then input two words, hello world, the first output is hello and the 2nd output is empty string. and when i input 3 it only enables me to input 2 words. i dont know if my jcreator has a bug or my jdk – Lorence Hernandez Jan 20 '16 at 13:02
  • Works fine here.I paste again : package StackOverFlow; import java.util.Scanner; /** * * @author fotis */ public class arraay { public static void main(String[] args){ Scanner s = new Scanner(System.in); int n = s.nextInt(); String[] words = new String[n]; System.out.println("Please enter " + n + " words."); for ( int a = 0; a < n; a++ ) { Scanner word=new Scanner(System.in); words[a] = word.nextLine(); } System.out.println("your input is :"); for ( int i = 0; i< n; i++ ) { System.out.println(words[i]); } } } – Tsakiroglou Fotis Jan 20 '16 at 13:13
  • thanks but its still the same. – Lorence Hernandez Jan 20 '16 at 13:16
  • It's astonish , they hold or put dublicate everywhere.Thats maybe because of comment text.Can you write an email for some seconds so i can send you the code? – Tsakiroglou Fotis Jan 20 '16 at 13:16
  • i dont know why, but when i put some System.out.print() or System.out.println() in my program it works perfectly – Lorence Hernandez Jan 20 '16 at 13:20
  • I got it delete your mail – Tsakiroglou Fotis Jan 20 '16 at 13:24
  • http://pastebin.com/LKBp66hq can you take a look at this? the snippets works differently when i put or remove line 29. i cant think of anything, is it really a bug? – Lorence Hernandez Jan 20 '16 at 14:01

0 Answers0