Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Kindly help me  RSS feed

 
mr_doer
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this program I wanna have the numbers array(numbers[]) in the
arraySplit method.How can I access it.This code is giving an error stating "cannot resolve variable numbers[] in the arrayCount method".If I declare numbers[] outside the meth3,it is
giving array out of bound exception during runtime.Kindly help.

import java.io.*;
class one{
public int num;
int var;
int flag;
int cntr;
int meth1(){

try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
var=Integer.parseInt(s);
}
catch(IOException e){}
return var;
}
void meth2(){

try{
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
String s1=br1.readLine();
num=Integer.parseInt(s1);
}
catch(IOException e){}
}

void meth3(){
System.out.println("num="+num);
int cntr;
String s2[]=new String[num];
int numbers[]=new int[num];
try{
BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
for(cntr=0;cntr<num;cntr++)>
{
s2[cntr]=br2.readLine();
numbers[cntr]=Integer.parseInt(s2[cntr]);

}
System.out.println("You entered the following numbers:");
for(int i=0;i<num;i++)>
{
System.out.println(numbers[i]);
}
}
catch(IOException e){}
}
int power(int var){
flag=1;
this.var=var;
for(int i=1;i<=var;i++)
{
flag=2*flag;
}
return flag/2;
}
void arrayCount(){
int i=0;
System.out.println("flag/2="+(flag/2));
for(int j=0;j<(flag/2);j++)
{

if(numbers[j]<(flag/2))
i++;
}
System.out.println("value of i="+i);
}
}
class three{
public static void main(String args[]){
one obj=new one();
System.out.println("Enter the type of variable:");
int var=obj.meth1();
System.out.println("Mention the total Numbers to be entered:");
obj.meth2();
System.out.println("Enter the numbers:");
obj.meth3();
int sub_factor=obj.power(var);
System.out.println("Subtraction factor="+sub_factor);
obj.arrayCount();
}
}

Thanks,
Mr.Doer
 
Eric Spery
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Doer. The Class file you included has a number of other bugs preventing examination of the specific problem you mention. Did you accidentally post an older version of the file? Is there any way you can post a version which will produce the error you're seeing.
e.
 
mr_doer
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is the program, I am battling from two days.The source code is correct but in the decription instead of arrayCount,I mentioned arraySplit.
Thanks,
Sujay
 
Eric Spery
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. I understand the source code you're compiling is working fine with the exception of the error you are discussing; however, the source code you have pasted into your note has other errors as well. I suspect something happened when you cut and paste this code into your mail. Could you repaste the code?
e.
 
mr_doer
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

import java.io.*;
import java.lang.*;
class one{
public int num;
int var;
int flag;
int cntr;
int meth1(){

try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
var=Integer.parseInt(s);
}
catch(IOException e){}
return var;
}
void meth2(){

try{
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
String s1=br1.readLine();
num=Integer.parseInt(s1);
}
catch(IOException e){}
}

void meth3(){
System.out.println("num="+num);
int cntr;
String s2[]=new String[num];
int numbers[]=new int[num];
try{
BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
for(cntr=0;cntr<num;cntr++)>
{
s2[cntr]=br2.readLine();
numbers[cntr]=Integer.parseInt(s2[cntr]);

}
System.out.println("You entered the following numbers:");
for(int i=0;i<num;i++)>
{
System.out.println(numbers[i]);
}
}
catch(IOException e){}

}
void power(){
flag=1;
this.var=var;
for(int i=1;i<=var;i++)
{
flag=2*flag;
}
}
void arrayCount(){
int i=0;
System.out.println("flag/2="+(flag/2));
for(int cntr=0;cntr<(flag/2);cntr++)
{

if(numbers[cntr]<(flag/2))
i++;
}
System.out.println("value of i="+i);
}
}
class three{
public static void main(String args[]){
one obj=new one();
System.out.println("Enter the type of variable:");
int var=obj.meth1();
System.out.println("Mention the total Numbers to be entered:");
obj.meth2();
System.out.println("Enter the numbers:");
obj.meth3();
obj.power();
obj.arrayCount();
}
}

 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mr_doer,
Your contribution to Javaranch is very important to us.
However we do have a naming policy outlined here.
Please register again with a name which complies to this rule.
Otherwise I'm afraid you may come to the Ranch soon and find that your account has been locked.
Thank You.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!