I have to call the method that takes an array list of parameter and moves the minimum value in front of the list. I keep getting an error int this line:
System.out.printf("Display numbers\n", myList.minInteger);
Here's the code:
import java.util.*;
public class MinToFront{
public static void main (String [] args){
MinToFront myList = new MinToFront();
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(3);
list.add(8);
list.add(92);
list.add(4);
list.add(2);
list.add(17);
list.add(9);
myList.minToFront(list);
System.out.printf("Display numbers\n", myList.minInteger);
}// end of main
public static void minToFront (ArrayList<Integer> minInteger){
int result = 0;
int min = minInteger.get(0);
for (int i = 0; i < minInteger.size(); i++){
if (minInteger.get(i)< min) {
min = minInteger.get(i);
result = min;
}
}
minInteger.add(0, minInteger.remove(result));
}// end of method
}//end of class
First, writing "Display numbers" does not display numbers. It displays the sentence you just wrote. Second, do not create an instance if the class includes main().
Lastly, you should put brackets at the end of a method while calling it.
Here is your code, corrected:
public static void minToFront (ArrayList<Integer> minInteger)
{
int result = 0;
int min = minInteger.get(0);
for (int i = 0; i < minInteger.size(); i++)
{
if (minInteger.get(i)< min)
{
min = minInteger.get(i);
result = min;
}
}
minInteger.add(0, result);
minInteger.remove(result);
}// end of method
}//end of class
Related
here's the code for MyArrayList
public void addLast(int number){
if(!isFull())
doubleTheArray();
element[count++] = number;
}
and here's the code for MyOrderedList
public void add(int item){
for(int i = 0; i < count; i++){
for(int j = i+1; j < count; j++){
if(element[i] > element[j]){
item = element[i];
element[i] = element[j];
element[j] = item;
}
}
}
addLast(item);
}
This is the main method
public static void main(String [] args){
MyOrderedList oList = new MyOrderedList();
oList.add(3);
oList.add(5);
oList.add(2);
oList.add(4);
oList.add(7);
oList.add(8);
oList.add(10);
System.out.println("Ordered list of items: "+ oList);
}
Can anyone help me find out what's wrong? We're not allowed to use sort so I made a solution but 2 numbers are outputted twice in the main method for MyOrderedList. We have to add the elements at the right location. Or in ascending order for add(item). isFull() is boolean method that doubles (doubleTheArray()) the array size if it is true.
I am creating a very simple method that returns the maximum value in an array on Eclipse, and I get this following error: Cannot invoke findMax() on the array type double[]
Here is the code:
class themax {
private double findMax(double[] array) {
double max = array[0];
for (int index =1; index < array.length;index++) {
if (array[index]> max)
{
max = array[index];
}
}
return max;
}
}
public class maxandmin2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
double[] array;
array = new double[5];
for(int i=0;i<array.length;i++) {
array[i] = Math.random()*10;
}
array.findMax();
}
}
thanks a lot!
The method findMax is in the themax class and requires that the array is passed
You can call this method by instantiating a new themax and then calling it.
Change your main to
public static void main(String[] args) {
double[] array;
array = new double[5];
for(int i=0;i<array.length;i++) {
array[i] = Math.random()*10;
}
System.out.println ("The max is " + new themax().findMax(array));
}
note The findMax method should be public
i have this problem with my array list and i cant seem to solve it. i made sure that it directly stores into the array list but it still displays zeros instead of what i input. I've asked my friends about it and they don't know either. it keeps displaying zeros instead.
i showed this to my friends and they say nothing is wrong with it but i think there is something wrong with it though.
import java.util.*;
public class ArrayListAgain{
public static void main(){
ArrayList<Integer> integers = new ArrayList<Integer>();
ArrayList<Integer> divby2 = new ArrayList<Integer>();
ArrayList<Integer> divby3 = new ArrayList<Integer>();
ArrayList<Integer> divby5 = new ArrayList<Integer>();
int s;
Scanner scan = new Scanner(System.in );
System.out.println("How many integers do you want to enter?" );
s=scan.nextInt();
Integer ints;
for (int i = 0; i < s; i++){
ints = new Integer(scan.nextInt());
integers.add(ints);
if(ints.getInteger()%2 == 0){
divby2.add(ints);
}
if(ints.getInteger()%3 == 0){
divby3.add(ints);
}
if(ints.getInteger()%5 == 0){
divby5.add(ints);
}
}
System.out.println("Integers Entered: " );
for(int a=0; a<integers.size(); a++)
{
System.out.print(integers.get(a).getInteger()+ ",");
}
System.out.println("Divisible by 2:");
for(int a =0; a<divby2.size(); a++){
System.out.print(divby2.get(a).getInteger()+ ",");
}
System.out.println("Divisible by 3:");
for(int a =0; a<divby3.size(); a++){
System.out.print(divby3.get(a).getInteger()+",");
}
System.out.println("Divisible by 5:");
for(int a =0; a<divby5.size(); a++){
System.out.print(divby5.get(a).getInteger()+",");
}
}
}}
//2nd class//
public class Integer(){
private int Number;
public Integer(int x){
this.Number=Number;
}
public void setInteger(){
this.Number=Number;
}
public int getInteger(){
return Number;
}}
This is because the constructor of your self-defined Integer class is wrong.
It should be
private int number;
public Integer(int x){
this.number = x;
}
Also, you don't need to define setter if you already have a constructor.
Also, I would advise you to use the java.lang.Integer class instead of defining your own.
I'm creating a program that will generate 100 random numbers between 1 and 1000, add them to a list, and then sum up those numbers. Here's my code:
public class Iteration {
public static void main (String [] args){
private int RandomDataAnalyzer(int Rando) {
Random rand = new Random();
List<Integer> NumList = new ArrayList<Integer>();
for (int i=0;i<=100;i++){
Rando = rand.nextInt(1001);
NumList.add(Rando);
}
int sum = 0;
for (int i=0; i<100; i++)
{
Rando = rand.nextInt(100);
sum = sum + Rando;
}
return sum;
}
}
}
And here's my errors:
H:\Java\Iteration.java:12: error: illegal start of expression
private int RandomDataAnalyzer(int Rando) {
^
H:\Java\Iteration.java:12: error: ';' expected
private int RandomDataAnalyzer(int Rando) {
^
H:\Java\Iteration.java:12: error: ';' expected
private int RandomDataAnalyzer(int Rando) {
Any help, please?
You can't define a method inside another method. Close your main method first, then start RandomDataAnalyzer:
public static void main(String[] args) {
// Contents of main.
}
public int RandomDataAnalyzer(int Rando) {
// Contents of RandomDataAnalyzer.
}
I'm going to assume that this isn't a homework problem and that you're learning Java on your own. If I'm wrong, shame on me for giving a working version. But my impression is that you'll learn from this:
public class gpaCalc
{
static Random rand;
static int rando;
public static void main(String[] args)
{
ArrayList<Integer> NumList = new ArrayList<>(); // use ArrayList
for (int i=0;i<=100;i++)
{
rand = new Random();
rando = rand.nextInt(1001);
NumList.add(rando);
}
int sum = 0;
for (int i=0; i<100; i++)
{
rando = NumList.get(i); // get is "opposite" of put--get the value put into the list earlier
sum = sum + rando;
}
System.out.println(sum);
}
}
There doesn't seem to be a need for a separate method called randomDataAnalyzer.
Welcome to StackOverflow.
Let's begin with the first issue: randomDataAnalyzer is being defined inside main method. Which is wrong, you should be actually defining it at the same level.
Taken into consideration, you should also add the static word before this function because this method is part of the class, and not of the elements. It's not necessary to create a new element of the class 'Iteration' for using a simple method.
Last, but not least, you are looping through the arraylist incorrectly. You are not even calling it. As you will see now:
import java.util.*;
public class Iteration
{
public static void main(String[] args)
{
ArrayList<int> numberList = new ArrayList<int>(); // we define the arraylist
for (int i = 0; i < 100; i++)
{
numberList.add(new Random().nextInt(1001)); // we add a random number to the list
}
// finally, after the 100 values were added..
System.out.println(randomDataAnalyzer(numberList)); // we show the output
}
public static int randomDataAnalyzer(ArrayList<int> list) // we will send this function an arraylist which will get the Σ.
{
int sum = 0;
for (int value : list) // this is how we loop through foreach value in the list
{
sum += value; // which means sum = sum + value.
}
return sum; // after looping and summing up, here'll be the result
}
}
I hope this was what you were looking for.
Here's a working version. Note the changes to your original:
Don't define methods inside methods: it is illegal syntax.
Rando, NumList: the naming conventions are to start classes, interfaces, enums (i.e. types) with a capital letter, and methods, fields, and variables with a lowercase case letter;
Removed the int Rando parameter alltogether: it's value was never used (it was only assigned to)
Use the values from numList rather than generating new numbers.
Added a method illustrating that the use of such a list is not needed in this case; the 'list' of 1000 numbers is still present, but only conceptually.
import java.util.*;
public class Iteration {
public static void main (String [] args) {
int sum = new Iteration().randomDataAnalyzer();
System.out.println(sum);
}
private int randomDataAnalyzer() {
Random rand = new Random();
List<Integer> numList = new ArrayList<Integer>();
for ( int i=0; i<100; i++ )
{
numList.add( 1 + rand.nextInt(1000) );
}
int sum = 0;
for ( int i=0; i<numList.size(); i++ )
{
sum = sum + numList.get(i);
}
return sum;
}
// Note that the above method has the same effect as this one:
private int moreEfficient() {
Random rand = new Random();
int sum = 0;
for ( int i=0; i < 100; i++)
sum += 1 + rand.nextInt(1000);
return sum;
}
}
I'm new here..
I want to make a code to remember the last 10 numbers and to be not same.
private static ArrayList<Integer> nums = new ArrayList<Integer>();
public static void main(String[] args)
{
System.out.println(getRandomNumber());
}
public static int getRandomNumber()
{
int randomN = 0, rand = Rnd.nextInt(20);
while (nums.size() == 10)
{
nums.remove(nums.get(0));
continue;
}
if (!nums.contains(rand))
{
nums.add(rand);
randomN = rand;
}
else getRandomNumber();
return randomN;
}
when the array reach 10 values the first one will be deleted .. I hope you understand what I want :) Thanks
Try using an ArrayDequeue and when the length grows to more than 10, you simple remove the items from the tail.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
class main{
public ArrayList<Integer> nums;
public Random generator;
public static void main(String[] args){
// Calling Start
(new main()).start();
}
public void start(){
nums = nums = new ArrayList<Integer>();
generator = new Random();
for(int i=0;i<15;i++){
add(generator.nextInt(20));
print();
}
}
public void add(int newNumber){
// Check by iterating if i is already in nums
Iterator it = nums.iterator();
while(it.hasNext()){
if(newNumber == (Integer) it.next())
return; // i is already in our list
// so get out add()
}
if(nums.size() == 10){
int forward = nums.get(0);
for(int i = 1; i < 10; i++){
// Move numbers back 1 position
nums.set(i-1,forward);
// Save next number in forward
forward = nums.get(i);
}
}
nums.add(newNumber);
}
public void print(){
String str = "";
Iterator it = nums.iterator();
if(it.hasNext()){
str += "num: [ " + (Integer) it.next();
}
while(it.hasNext()){
str += " , " + (Integer) it.next();
}
str += " ]";
System.out.println(str);
}
}
I would either use a circular array or a linked list for this. Depending on what you plan to use the list of numbers for.