• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

array (new to programming)

 
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what am i doing wrong in sum array class



import java.util.*;
public class getInfo
{
int size;

int arraySize()
{

System.out.println("Please enter the Size of an array");
Scanner array = new Scanner(System.in);
size= array.nextInt();

return size;

}
}

import java.util.*;

public class calcArray extends getInfo
{

public int [] calcOnArray()
{
int[] arr= new int[size];
System.out.println("Please enter the elements of the array");

for (int i =0; i<arr.length;i++)

{
Scanner s= new Scanner(System.in);
arr[i]=s.nextInt();
}
for(int j=0; j<arr.length;j++) {
System.out.println("the elements of the array are " +arr[j]);

}

}
public class sumArray extends calcArray {

int sum = 0;
public void sumArr() {
int[] s = new int[arr[i]];
sum = sum + s;
System.out.println("the sum of array is " +sum);
}
}

}
Public class Array {

public static void main(String[] args) {

calcArray s =new calcArray();
s.arraySize();
s.calcOnArray();

}

}
 
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please re-post your code, the code tags didn't come out right. You need to end up with a code tag at the start of the code and a /code tag at the end of the code.

Highlight the entire block of code, then click the "Code" button above the edit window.
 
Carey Brown
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please give more detail about your problem. What output were you expecting, and what output did you get? Did you get any error messages? If so, post the complete error message.
 
Shaki Java
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops
I'm just playing and exploring inheritance the object is to get the user....input some date and then i define bunch of classes   to random execution from addition to sorting.... In my calcArray class my original plan was to prompt for input for the array and planned to use the display method in a different class but was not successful as i'm unable to return. My plan is to use the either the input number by user or the result of these number  for any thing may that be a string for concatenation like i said it dont matter just practicing   at the beginning level



 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something happened to the code's indentations.  All the statements start in the first column instead of being indented to show nesting in the logic.
Poorly indented code is hard to read and understand.
 
Shaki Java
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Help me write a class add the user entered arrays elements in public class calcArray extends getInfo in a separate classes it should be a two line code i guess . i just dont know how
to the numbers out from public class calcArray extends getInfo to be used in other classes.  The way im thinking is

public class sumArray extends calcArray {

int sum = 0;
public void sumArr() {
int[] s = new int[arr[i]]; ...............>this has to be wrong does not make sense to me. So my question is what statement should i use to get the elements of the arrays entered by user in my previous class
sum = sum + s;
System.out.println("the sum of array is " +sum);
}



 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please copy the full text of the compiler error messages and paste it here.


That statement says to create an array named s using the content of arr[i] for its size.
What do you intend that statement to do?

You need to be more careful when wrapping the code in code tags.  The last post did not wrap all of the code.
 
Shaki Java
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was thinking this would have the elements of the array entered by user....i know its wrong just dont know what should i write...there are no errors yet the previous two claases run fine...int the third class i'm stuck dont know how to move forward... when the user enter the elements after i prompt them "Please enter the elements of the array" i want to add that input of array in the third class...how do i do that?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

.int the third class i'm stuck dont know how to move forward.


Can you use the name of the class so we know which one you are asking about?
Why are you writing so many classes with a single method.  There is no need to do that.  All the methods can go in one class.

 i want to add that input of array


Are you asking :
how to put the user's input into an array?
Or how add the contents of an existing array?
 
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I was you I would use an arrayList, not an array, if you use an arraylist you don't have to ask the user how mutch values the user wants to put in.
Anyhow, you could ask the user to seperate the values by an space. Take it all in as an string (String s = array.nextLine();), then split the string (String[] splited = s.split(" ");).
Loop through it (for(int i = 0, i < size, i++)) and asign in it (arr[i] = Integer.parseInt(splited[i]);)
 
Shaki Java
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The third class is

public class sumArray extends calcArray

I'm just practicing....this is my second array program..and first inheritance program ....The reason for so many classes is to understand inheritance....plus to learn hot to pass data from parent to child... my next class which will extend sumArray after once i do the addition on the numbers user will enter will be to multiplay the sum off all the numbers with any one of the element entered by user in calacArray. Then resulting number from that will be divided by a prime number i gues..Like i said i'm just playing. My problem ho do i point to those single data element. e.g

class userInfo

Enter number

Class displayInfo extend userinfo

The number you enter are xxx

Class addData extend displayInfo

The result of the adding the number you entered is xxx

Class multiplyData extend addData

The result of multiplaying the Second number you entered with the sum of all numbers is XXX
.
.
.
so on and so forth


now my problem is  that the number entered by user in the below

"Class displayInfo extend userinfo

The number you enter are xxx"


HOW DO I Adress those number in

Class addData extend displayInfo

And once addressed in Class addData extend displayInfo  then how do i adress them in Class multiplyData extend addData will any thing change (I know it wont just still would like to know)


AND I THANKYOU so much all of you...... for being patient to answer my illLogical question
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please remember to wrap all posted code in code tags.

You are using too many classes in the project.   The code you write should go in methods.  All the methods can go in one class.
This project is not a good place to use inheritance.
When you discuss problems it should be about what a particular method does or does not do.  

Note: Java coding conventions say class names should start with an uppercase letter.  Method and variable names with a lowercase letter.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic