Hi!
I hope someone can help me out with this.I have one task left on my homework that i´m struggling with.
The assignment is:
Create a program in which you can put 10 words into separated by enter in the command window after that print the words backwards in the commandwindow. Everything in the commandwindow no pop ups.

I have tried two diffrent ways:

First:import java.io.*;
public class Ord {
  public static void main (String[] arg) throws IOException {
     BufferedReader input = new BufferedReader
                      (new InputStreamReader(System.in));
System.out.print("s1? "); System.out.flush();
String s = input.readLine();
System.out.print("s2? "); System.out.flush();
s = input.readLine();
System.out.print("s3? "); System.out.flush();
s = input.readLine();
System.out.print("s4? "); System.out.flush();
s = input.readLine();
System.out.print("s5? "); System.out.flush();
s = input.readLine();
System.out.print("s6? "); System.out.flush();
s = input.readLine();
System.out.print("s7? "); System.out.flush();
s = input.readLine();
System.out.print("s8? "); System.out.flush();
s = input.readLine();
System.out.print("s9? "); System.out.flush();
s = input.readLine();
System.out.print("s10? "); System.out.flush();
s = input.readLine();
for ("s10\n9\ns8\ns7\ns6\ns5\ns4\ns3\ns2\ns1");
 System.exit(0);
}
}

In the above I can put the words but they will not come up at the end backwards

Now I have started on a second way and it looks like this:

public class Str {
 public static void main(String[] args) throws IOException {
  String[] s;
  s = new String[10];
                *Right here I have tried diffrent and gets message illegal start of expression *
  for(int i=0; i<=9; i++){
   System.out.println("ord"+(i+1)+"?");
   s[i] = *???? don´t know what to put*;   
  }
  for(int i=9; i>=0; i--) {
   System.out.println(s[i]);
  }
 }
}

Thansk for your help in advance!

Edited 3 Years Ago by pyTony: fixed formatting

import java.io.*;
public class Ord
{
   public static void main ( String[] arg ) throws IOException
   {
      BufferedReader input = new BufferedReader
                             ( new InputStreamReader ( System.in ) );
      System.out.print ( "s1? " );
      System.out.flush();
      String s = input.readLine();
      System.out.print ( "s2? " );
      System.out.flush();
      s = input.readLine();
      System.out.print ( "s3? " );
      System.out.flush();
      s = input.readLine();
      System.out.print ( "s4? " );
      System.out.flush();
      s = input.readLine();
      System.out.print ( "s5? " );
      System.out.flush();
      s = input.readLine();
      System.out.print ( "s6? " );
      System.out.flush();
      s = input.readLine();
      System.out.print ( "s7? " );
      System.out.flush();
      s = input.readLine();
      System.out.print ( "s8? " );
      System.out.flush();
      s = input.readLine();
      System.out.print ( "s9? " );
      System.out.flush();
      s = input.readLine();
      System.out.print ( "s10? " );
      System.out.flush();
      s = input.readLine();
      for ( "s10\n9\ns8\ns7\ns6\ns5\ns4\ns3\ns2\ns1" );
      System.exit ( 0 );
   }
}


public class Str
{
   public static void main ( String[] args ) throws IOException
   {
      String[] s;
      s = new String[10];
      //*Right here I have tried diffrent and gets message
      // illegal start of expression *
      for ( int i = 0; i <= 9; i++ )
      {
         System.out.println ( "ord" + ( i + 1 ) + "?" );
         //s[i] = * ? ? ? ? don´t know what to put*;
      }
      for ( int i = 9; i >= 0; i-- )
      {
         System.out.println ( s[i] );
      }
   }
}

Store words in array/arrayList, print out array/arrayList backwards. What could be easier?

Thank you for taking your time to help me out with this.
Legoland

This article has been dead for over six months. Start a new discussion instead.