Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 19th, 2007, 09:08 PM
Authorized User
 
Join Date: Aug 2005
Location: , , .
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

Reply With Quote
  #2 (permalink)  
Old March 20th, 2007, 11:14 PM
Authorized User
 
Join Date: Mar 2007
Location: Hyderabad, A.P, India.
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
Reply With Quote
  #3 (permalink)  
Old April 20th, 2007, 01:58 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hyderabad, A.P., India.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Streams in C# pady123 C# 2 September 11th, 2007 10:17 AM
Play audio streams directly from a winsock port AshBex Pro VB.NET 2002/2003 3 July 20th, 2006 08:11 PM
How do you get keyboard input? Polymer JSP Basics 1 October 28th, 2005 02:25 PM
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



All times are GMT -4. The time now is 04:19 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.