import java.io.*;
class Ex33
{
	public static void main(String[] args)
	{
		Console console=System.console();
                System.out.println("Please enter the next number");  
                String input;
                input=console.readLine(); 
		int n1;
                n1=Integer.parseInt(input);
                int sum=0;
                int sum2=0;
                int input2=0;
                input2=n1;
                int count=1;
                char dot=',';
                String Number="";
                int RealNumber2=0;
                String All="";
                String RealNumber="";
                
                
                while(count<5)
                           

                {    
                     System.out.println("Please enter the next number");
                     input=console.readLine();
                     n1=Integer.parseInt(input);
                     sum=(n1+sum);
                     All+=n1+",";   // here i should get a string of all numbers that were fed into the application
                     count++; 
                }
                   
                 sum2=sum/4; // the sum of all the numbers is divided by amount of numbers put in, giving the average
                 System.out.println("Your average is"+" "+sum2);
                 System.out.println(All);
               
               
                
                 for(int count2=0;count2<All.length();count2++)
                 {
               
                      char c=All.charAt(count2);
                      
                       if(c!=dot)
                        {
                        Number+=c; // this should add each number till it reaches dot
                        }     
                       else
                         {
                         RealNumber2=Integer.parseInt(Number);//here i should get a string of numbers and it should be converted to a number
                          }   

                          if(RealNumber2<sum2)
                          System.out.println(RealNumber2); // a number should be printed out , that is smaller than the sum
               
                 }             
 
        }

}

i am stuck on the last part, . my task is to print all the numbers put into the applications which are smaller than the sum.

the last part is problematic..what the last part should do is count through all the characters in teh string "All", and save the numbers before dot. then convert those numbers and compare them to the average..

but what i get at the end...is few irrelevant numbers such as 0 0 12 ..for example

I do not understand what you are trying to do. please first tell me exactly what the program (not some subsection of it) must do.

I have read through your code and although i dont know what you are trying to do i can already tell that you are doing some things in a questionable manner. but i'll only know when i know what the program as a whole is supposed to accomplish.

to begin with: write the prgram as a serious of functions. The in main use one repetition structure to call all the funtions which is not the function main. let these functions do all the work.

Main should have one loop which receive input from the keyboard the number of times that you want to enter a number and then with this number each repetition aught to call a function with this number as argument to do its work. the function then returns to main the result of that work.

that way your program has some structure and you can localise problems to one section of it.

for example. in this program of yours it is clear that you first accept numbers. they over write all the old numbers. only then do you start to process the numbers which is already overwritten.

here how output should be:


"Please enter the next number
10
"Please enter the next number
10
"Please enter the next number
10
"Please enter the next number
10
"Please enter the next number
9
your average is 10

10,10,10,9

9 (the only number that is smaller than average)


as i explained in the code..

for(int count2=0;count2<All.length();count2++)
                 {
               
                      char c=All.charAt(count2);
                      
                       if(c!=dot)
                        {
                        Number+=c; 
                        }     
                        else
                         {
                        RealNumber2=Integer.parseInt(Number);                           
                         
                         }
                            if(RealNumber2<sum2)
                        System.out.println(""+RealNumber2+","); 
                          
                 }             
                          
        }

}

this last part is problematic, cause it doesnt add number after the dot, it adds them all up..

like this

1
10
1010
101010
10101010


something like that. i dont know how to tell the program to add numbers seperately after the dot

is that the assignment as it has been given to you?

i can only guess what the assignment should be if you are given output but not requirements. i would guess to get to that output you have to first receive all of the numbers, then get the average of those numbers, then print all the numbers smaller than average. please correct me if you feel that i am wrong in my above assesment.

if the above assesment on my part is correct then here is how to continue. to begin with i would suggest that you add the input to an integer array. use a for loop. in this for loop receive each number from the keyboard and add it to the array.

now that you have your input create two functions apart from main. one is compute average and the other is determine smaller. compute average should receive an integer array as input and return a double. determine smaller should also receive an integer array but it should also receive a double which holds smaller.

the above two functions you should be able to code. if you cant ask for help and i will direct you.

question though. you are refering to a some. why are you refering to any sums? the only sum is in the compute average. you should learn that each function does one job and it does that one job well. so let compute average alone deal with a sum as part of its job in computing the average. the rest of the code should have absolutely no clue at all of sums or whatever. this is called encapsulation and it is a very important factor in writing good code that actually works.

the other this is what do you mean with add the number after the dot? the only adding will be done in computeaverage. no other part of your code should carry this burden.

you can prob print the values directly from main. the smaller than average values will be printed with a for loop cycling through the array that comes back from determine smaller. the list of numbers can be printed by cycling through the input values array.

this too you should be able to do. if you need help just post and i shall direct you.

okay, first, i cant use arrays. only loops (for, while, switch)

second, my sum2 (average) is fine, i need to sperate the numbers out of the string, and i cant , thats the problem.. if i succeed seperating them, i am a king

cause then i will be able to compare them against sum2 (average)

ok i see. this is of course far more difficult but i'll try.

like so:

<code>
string sNumber = "";

for ( int x = 0; x < All.length; x++ ) //you have count2 instead of x but whatever...
{
if ( All[x] == ", " ) {
//you have hit the end of a number. do your thing
int iNumber = integer.parseInt(sNumber)
some compare code
print the thingie
remember to clear the string. sNumber = "";
}
else
sNumber = sNumber + All[x];
}
</code>

hope this helps. try it quick. i have to leave soon but if you are quick i might help if it does not work.

question. i see that you say All.length. Does this actually works? The compiler actually uses this to compete the length of the string?

out of curiousity. why the insistence upon a string and not a int array?

i have forgotten how the code tags work here. you will just have to read the above carefully to see what is indentation and where brackets must be.

as i said before i dont use arrays. so no array use

Ghost can you be a bit more clear in your code

what do i need to put under if and else to make it work

import java.io.*;
class Ex33
{
	public static void main(String[] args)
	{
		Console console=System.console();
                System.out.println("Please enter the next number");  
                String input;
                input=console.readLine(); 
		int n1;
                n1=Integer.parseInt(input);
                int sum=0;
                int sum2=0;
                int count=1;
                char dot=',';
                String Number;
                int RealNumber2=0;
                String All="";
                String RealNumber="";
                Number="";
                
                while(count<5)
                           

                {    
                     System.out.println("Please enter the next number");
                     input=console.readLine();
                     n1=Integer.parseInt(input);
                     sum=(n1+sum);
                     All+=","+n1;   
                     count++; 
                }
                   
                 sum2=sum/4;
                 System.out.println("Your average is"+" "+sum2);
                 System.out.println(All);
               
               
                
                 for(int count2=0;count2<All.length();count2++)
                 { String sNumber="";
               
                      char c=All.charAt(count2);
                                  
                      if(c!=dot)
                       { 
                        sNumber+=c;
                        } 
                        else
                         {
 
                        RealNumber2=Integer.parseInt(sNumber); // the problem is here. sNumber doesnt refer to sNumber+=c, but it referes to ""...that is the problem,, how can i make it refer to sNumber                          
                         
                      
                            if(RealNumber2<sum2)
                        System.out.println(RealNumber2+","); 
                        }  
                 }         
                       
        }

}
import java.io.*;
class Ex33
{
	public static void main(String[] args)
	{
		Console console=System.console();
                System.out.println("Please enter the next number");  
                String input;
                input=console.readLine(); 
		int n1;
                n1=Integer.parseInt(input);
                int sum=0;
                int sum2=0;
                int count=1;
                char dot=',';
                String Number;
                int RealNumber2=0;
                String All="";
                String RealNumber="";
                
                
                while(count<5)
                           

                {    
                     System.out.println("Please enter the next number");
                     input=console.readLine();
                     n1=Integer.parseInt(input);
                     sum=(n1+sum);
                     All+=","+n1;   
                     count++; 
                }
                   
                 sum2=sum/4;
                 System.out.println("Your average is"+" "+sum2);
                 System.out.println(All);
               
               
                
                 for(int count2=0;count2<All.length();count2++)
                 { 
                       
                 
           
                      if(All.charAt(count2)!=dot)
                       { 
                        sNumber = sNumber + All.charAt(count2);/ it wont let me initialize this command, i dont know how to initialize it, so that it would work
                       
                        } 
                        else
                         {
 
                          RealNumber2=Integer.parseInt(sNumber);                          
                         
                      
                            if(RealNumber2<sum2)
                        System.out.println(RealNumber2+","); 
                        }  
                 }         
                       
        }

}

this is another version

okay, first, i cant use arrays. only loops (for, while, switch)

second, my sum2 (average) is fine, i need to sperate the numbers out of the string, and i cant , thats the problem.. if i succeed seperating them, i am a king

cause then i will be able to compare them against sum2 (average)

But you are then in a world of hurt, because:

If you cant use arrays then you cant use strings, as strings are implemented using arrays.

This is odd, very, very odd. I could see an instructor asking to implement this with at most three individual items (ints), but once you hit 4 there is no more learning, just wrote copying and pasting of code. And 5 is worst.

If you are in a module that is learning about loops, then i am pretty sure that your instructor really wants you to be using int arrays, not character arrays (Strings).

no , you cant use arrays..

i think the way it should be is:

the formula is running like this.. All.charAt(count2)

then when it reaches a comma, (dot) , it goes to else in the if method and performs a sum of the string of numbers (that is later converted to numbers) with teh sum..

but it doesnt work.. i know there should be some modification for it to work, but i dont understand it..

for( )
method should make the numbers (count2) run through all All string characters,


if() method
should string the characters to numbers

else
(when reaching dot) should do the calculation, and then it should continue calculating

no , you cant use arrays..

i think the way it should be is:

the formula is running like this.. All.charAt(count2)

then when it reaches a comma, (dot) , it goes to else in the if method and performs a sum of the string of numbers (that is later converted to numbers) with teh sum..

but it doesnt work.. i know there should be some modification for it to work, but i dont understand it..

for( )
method should make the numbers (count2) run through all All string characters,


if() method
should string the characters to numbers

else
(when reaching dot) should do the calculation, and then it should continue calculating

And like i said - if you can not use arrays, then you can not use Strings - simple as that.

Or said another way - when you use Strings you are using arrays. And the entire program becomes trivial using int arrays instead of char arrays.

And like i said - I know of no rational instructor that would ever assign something like this.

Just look at the horrible example this code is performing the append after append after append.

I recommend that you specifically ask if you are allowed to use arrays (either int or some floating point) otherwise this is a mostly useless exercise - teaching poor programming practice is not usually the norm.

well, i use characters to merge into a string and then convert into a number..

why is that a problem?


my instructor isnt rational, he gives hard homework, and does minimal explanation..


i dont know what to do, ! i am new to java and i feel weak , it is either my IQ is low or the exercise is hard..

but i do know that the solution to the problem uses loops and ifs conditions only

well, i use characters to merge into a string and then convert into a number..

why is that a problem?


my instructor isnt rational, he gives hard homework, and does minimal explanation..


i dont know what to do, ! i am new to java and i feel weak , it is either my IQ is low or the exercise is hard..

but i do know that the solution to the problem uses loops and ifs conditions only

According to you, you can not use arrays. Strings are arrays. Therefore according you, you can not use Strings - QED - simple at that.

And as to your instructor, send me his email and i will ask professor to instructor (or professor or whatever he is).

About this problem:

if(All.charAt(count2)!=dot)
                       { 
                        sNumber = sNumber + All.charAt(count2);/ it wont let me initialize this command, i dont know how to initialize it, so that it would work
                        }

I cannot find where you have initialized the sNumber. Maybe you should change at the beginning of your code this:
String Number; to this: String sNumber = "";

Have you consider using StringTokenizer class. Technically is not using arrays. Check the API. It will make separating the numbers much easier:

StringTokenizer strTok = new StringTokenizer(All, ",");

while (strTok.hasMoreTokens()) {
   System.out.println(strTok.nextToken());
}

Edit: Although I don't think that he will approve such solution even if it is within the requirements. He might say he didn't want you to use some ready class and your approach might be better

This exercise is very good for teaching to students the use of arrays. I find it strange that you are not allowed to use them

the code i gave to you above uses no arrays. it uses a string. that is why i asked you if All.length actually worked.

now back to the first code. not the "another version" code.

The reason why you get the error you are commenting in that code is because the sNumber variable is declared and initialized inside the loop. So each time the loop comes round it sets it to "". Put it before the for loop. As I have done in my code. I apologise for the unclear code. I forgot how to use the code tags.

second. Each time that the loop encounters a comma then sNumber has a valid number. Bow do the comparison and if it is right then print it. like so:

if ( All[x] == dot ) //where dot is of course ", "
{
    int iNumber = integer.parseInteger(sNumber)
    
    if ( iNumber < average )
        System.out.println(iNumber);

    //and here is your other mistake.  you must now set sNumber to ""
    sNumber = "";
}
else   //as long as you have not yet struck the comma
{
   sNumber = sNumber + All[x];
}

as you can see the second issue is that you have to empty the string when you want to deal with a new number. And again. This code contains no arrays. It is your own code just adapted a bit.

I also dont know if integer.parseint will have a problem with the space before the number in sNumber. In that case I am sure you can use some function to strip of the white space.

I am just curious. Why no arrays? The code i am giving you has no arrays.

Also another thing that I need to mention after reading Unbidden Ghost post.

When you create the All variable you begin with the ',' char. Meaning that the first character would be the dot. So you will go in the loop and try to parse an emty string.

Try to check if the 'sNumber' is empty before parsing it.


Also I believe the problem is quite the opposite. I didn't see anywhere in the code the 'sNumber' declared anywhere. It also need to be declared and initialized outside the loop which is not

if i initialize sNumber as a string,,,
when i come to this line, it would refer to the declared sNumber outside the loop.


int iNumber = integer.parseInteger(sNumber)


so lets say i do this String sNumber=""; (now it is initialized)
when it comes to sNumber , it will refer to the String and not to the new variable i created in the loop #
sNumber = sNumber + All[x]; (which is actually the string that i want to convert to a whole number)

if i initialize sNumber as a string,,,
when i come to this line, it would refer to the declared sNumber outside the loop.


int iNumber = integer.parseInteger(sNumber)


so lets say i do this String sNumber=""; (now it is initialized)
when it comes to sNumber , it will refer to the String and not to the new variable i created in the loop #
sNumber = sNumber + All[x]; (which is actually the string that i want to convert to a whole number)

You need to declare and initialize the sNumber outside the loop. Otherwise you will get errors. Inside the loop you change its value by doing this: sNumber = sNumber + All[x]; The sNumber is 'one'. If you declare it in the loop with each loop you will have a new value which will erase the previous parts of the number.

Unbidden Ghost code will work as long as the sNumber is created outside the loop

>iq low
i dont know. if you are new to java and you have coded so far then you prob just need practise. you should not put yourself down. tell yourself a negative thing often enough and you'll actually start to believe it. and once you believe it it becomes true by virtue of you not doing anything that counters that negative believe.

now back to the code. i dont understand what your prob with my code is. take the if statement which i have written and paste it in the for loop. put the creation and initialization of sNumber outside the loop.

like so:

string sNumber = "";

//only now do you begin the for loop.
for ( int count2 = 0; count2 < All.length; count2++ )
{
    //in here you execute the if statement.  but since you insist 
    //on count two dont forget to replace the x I specify with count2.
}

the code uses no array except for the string which you yourself has used. if your All string has the correct value then my code should work. just take the if statement and paste it inside the for loop. you might have to do minimal debugging for some syntax but the logic is sound.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.