4 Replies - 10152 Views - Last Post: 10 July 2009 - 03:52 AM Rate Topic: -----

#1 jaysonlagare  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 13-May 09

Buffered reader program in java

Posted 08 July 2009 - 04:29 AM

import java.io.*;
public class Lagare4{

	public static void main(String []args){
	BufferedReader DataIn= new BufferedReader(new InputStreamReader(System.in));
	
	String choice="N";
	
	do
	{
	
	int str=0;
	int end=0;
	String num1="";
	String num2="";
	int sum=0;
	int n=0;
	
	System.out.print("Please enter the starting number: ");
	try{  
	num1=DataIn.readLine();
	str=Integer.parseInt(num1);
	}catch( IOException e ){
 	System.out.println("Error!");
 	}
	System.out.print("Please enter the last number: ");
	try{  
	num2=DataIn.readLine();
	end=Integer.parseInt(num2);
	}catch( IOException e ){
 	System.out.println("Error!");
 	}
	
	int[]last=new int[end+1];
	if(str>=end)
	{
	System.out.println("Error: Starting number is greater than last number");
	System.out.println();
	}
	else
	for( int i=str; i<last.length; i++ )
	{
		if(i%2==1)
		
		
		System.out.println(last[i]=i);
		sum=last[i]+sum;		
		
			
	}
	System.out.println("The sum of all odd numbers is "+sum);
	System.out.println("Do it again? (Y/N)");
		try{  
	choice=DataIn.readLine();
		}catch( IOException e ){
 	System.out.println("Error!");
 	}
	
	
	
	}while(!choice.equalsIgnoreCase("N"));
	}
}
Attached File  hello.txt (354bytes)
Number of downloads: 220Attached File  hello.txt (354bytes)
Number of downloads: 220

Is This A Good Question/Topic? 0
  • +

Replies To: Buffered reader program in java

#2 kmangold  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 167
  • Joined: 24-June 09

Re: Buffered reader program in java

Posted 08 July 2009 - 04:34 AM

What is the question?
Was This Post Helpful? 0
  • +
  • -

#3 jaysonlagare  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 13-May 09

Re: Buffered reader program in java

Posted 08 July 2009 - 05:30 AM

hello, this is my java program that prints all odd numbers from starting number upto the last number and get the sum of it!! I have done it using a buffered reader to input integer, And I want this program to input and display the output using JOption pane!! But i am having difficulties of it! Is there anyone else who can solve this problem?? tnx a lot!!
import java.io.*;
public class Lagare4{

	public static void main(String []args){
	BufferedReader DataIn= new BufferedReader(new InputStreamReader(System.in));
   
	String choice="N";
   
	do
	{
   
	int str=0;
	int end=0;
	String num1="";
	String num2="";
	int sum=0;
	int n=0;
   
	System.out.print("Please enter the starting number: ");
	try{  
	num1=DataIn.readLine();
	str=Integer.parseInt(num1);
	}catch( IOException e ){
	 System.out.println("Error!");
	 }
	System.out.print("Please enter the last number: ");
	try{  
	num2=DataIn.readLine();
	end=Integer.parseInt(num2);
	}catch( IOException e ){
	 System.out.println("Error!");
	 }
   
	int[]last=new int[end+1];
	if(str>=end)
	{
	System.out.println("Error: Starting number is greater than last number");
	System.out.println();
	}
	else
	for( int i=str; i<last.length; i++ )
	{
		if(i%2==1)
	   
	   
		System.out.println(last[i]=i);
		sum=last[i]+sum;		
	   
		   
	}
	System.out.println("The sum of all odd numbers is "+sum);
	System.out.println("Do it again? (Y/N)");
		try{  
	choice=DataIn.readLine();
		}catch( IOException e ){
	 System.out.println("Error!");
	 }
   
   
   
	}while(!choice.equalsIgnoreCase("N"));
	}
}

Was This Post Helpful? 0
  • +
  • -

#4 ronaldjames  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 42
  • Joined: 29-June 09

Re: Buffered reader program in java

Posted 08 July 2009 - 06:15 AM

View Postjaysonlagare, on 8 Jul, 2009 - 06:30 PM, said:

And I want this program to input and display the output using JOption pane!! But i am having difficulties of it! Is there anyone else who can solve this problem?? tnx a lot!!


So you want that program in JOptionPane

Here is the code
import javax.swing.JOptionPane;
public class Lagare4{
   
	public static void main(String []args){
	
   
	String choice="N";
   
	do
	{
   
	int str=0;
	int end=0;
	String num1="";
	String num2="";
	int sum=0;
   
	//System.out.print("Please enter the starting number: ");
	try{  
	num1=JOptionPane.showInputDialog(null,"Enter the starting number ");
	str=Integer.parseInt(num1);
	}catch( Exception e ){
	 //System.out.println("Error!");
		JOptionPane.showMessageDialog(null,"Error Occured","ERROR",JOptionPane.ERROR_MESSAGE);
	 }
	///System.out.print("Please enter the last number: ");
	try{  
	num2=JOptionPane.showInputDialog(null,"Enter the last number ");
	end=Integer.parseInt(num2);
	}catch( Exception e ){
	 //System.out.println("Error!");
		JOptionPane.showMessageDialog(null,"Error Occured","ERROR",JOptionPane.ERROR_MESSAGE);
	 }
   
	int[]last=new int[end+1];
	if(str>=end)
	{
	//System.out.println("Error: Starting number is greater than last number");
		JOptionPane.showMessageDialog(null,"Error: Starting number is greater than last number","ERROR",JOptionPane.ERROR_MESSAGE);
	//System.out.println();
	}
	else
	for( int i=str; i<last.length; i++ )
	{
		if(i%2==1)
	   
	   
		//System.out.println(last[i]=i);
		last[i]=i;
		if(last[i]!=0)
		{
			JOptionPane.showMessageDialog(null,"\nThe ODD Numbers are : \n"+last[i]+"\n");
		}
			sum=last[i]+sum;		
	   
		   
	}
	//System.out.println("The sum of all odd numbers is "+sum);
	JOptionPane.showMessageDialog(null,"The sum of all odd numbers is "+sum,"OUTPUT",JOptionPane.PLAIN_MESSAGE);
	//System.out.println("Do it again? (Y/N)");
	
		try{ 
			choice=JOptionPane.showInputDialog(null,"Do it again? (Y/N)");
	
		}catch( Exception e ){
	  JOptionPane.showMessageDialog(null,"Error Occured","ERROR",JOptionPane.ERROR_MESSAGE);
	 }
   
   
   
	}while(!choice.equalsIgnoreCase("N"));
	}
}


Check whether this is enough for you..!
Was This Post Helpful? 1
  • +
  • -

#5 jaysonlagare  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 13-May 09

Re: Buffered reader program in java

Posted 10 July 2009 - 03:52 AM

Yeah!!Thanks for the solution! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1