Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 19th, 2007, 09:08 PM
Authorized User
 
Join Date: Aug 2005
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Default I/O streams: keyboard input

hello everybody!
i've a small problem here. in this program, a series of string inputs has to be taken from user through keyboard, the no of inputs is based on test cases, i.e., if user enters 3 test cases then he has to enter 3 string inputs, the string inputs are 2 be stored in an array, i dont know what iz the problem with this program, iz there n.e 1 who can point out and correct my mistake(s)

import java.io.*;
class InputTest
{
  public static void main(String args[]) throws IOException
  {
    int testCases;
    String inputs[];
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    testCases=Integer.parseInt(br.readLine());
    System.out.println("test cases="+testCases+"\n");
    for(int i=0;i<testCases;i++)
    {
      inputs[i]=br.readLine();
      System.out.println("input="+inputs[i+1]+"\n");
    }
  }
}

plz reply as soon as possible bcoz this is my class assignment :-(
i'll w8 4 a good reply

 
Old March 20th, 2007, 11:14 PM
Authorized User
 
Join Date: Mar 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

u can try this frist read the string with for loop and then display with another for loop
if it is not possible if write a function that function take a parameter of String type in class
that will call in main() fun

for(int i=0;i<testCases;i++) {
        inputs[i]=br.readLine();
}
for(int i=;i<inputs.length;i++) {
          System.out.println(""+inputs[i]);
}


System.out.println(


K.Mallikarjunarao
 
Old April 20th, 2007, 01:58 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi,
One thing to keep in mind while declaring variables in a member function is to initialize them. If you dont initialize them the its a compiler error.

Change the String inputs[]; to String inputs[] = null;

and your program has few more errors. Try the modified code

import java.io.*;
class InputTest
{
  public static void main(String args[]) throws IOException
  {
    int testCases;
    String inputs[] = null;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    testCases=Integer.parseInt(br.readLine());
    System.out.println("test cases="+testCases+"\n");
    inputs = new String[testCases];
    for(int i=0;i<testCases;i++)
    {
      inputs[i]=br.readLine();
      System.out.println("input="+inputs[i]+"\n");
    }
  }
}

your program will work

Regards,
Rakesh





Similar Threads
Thread Thread Starter Forum Replies Last Post
Streams in C# pady123 C# 2 September 11th, 2007 10:17 AM
How do you get keyboard input? Polymer JSP Basics 1 October 28th, 2005 02:25 PM
Play audio streams directly from a winsock port AshBex Pro VB.NET 2002/2003 2 April 8th, 2005 12:22 AM
XML, Sockets, Streams and a headache owain General .NET 4 September 8th, 2004 04:53 AM
recording live streams using java udayagiri suresh babu Java GUI 2 August 8th, 2004 06:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.