User To Input 10 Numbers - Find Smallest Element In Array

Dec 2, 2014

I am trying to write a code that asks the user to input ten numbers and then finds and displays the smallest number out of the ones given. I am supposed to implement arrays into the program to do this. But the problem I have run into is that when I compile the code in jgrasp, I am given several error messages and I am not quite sure what I have done wrong. I'm assuming it is either a syntax or a logical error on my part but reading over the code I do not understand what is causing these errors.

This is the most current draft of my code:

import java.util.Scanner;
 public class Exercise7_9 {
public static void main(String[] args) {
double[] numbers = new double[10];
//Enter ten double numbers: Scanner(System.in)
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Please enter ten numbers: ");

[Code] .... 
 
/* Sample Run:
Enter ten numbers: 1.9, 2.5, 3.7, 2, 1.5, 6, 3, 4, 5, 2
*/

And these are the exact error messages:

----jGRASP exec: javac -g Exercise7_9.java
Exercise7_9.java:35: error: '.class' expected
if (double m > list[i]) {
^
Exercise7_9.java:35: error: illegal start of expression
if (double m > list[i]) {

[Code] .....

View Replies


ADVERTISEMENT

Smallest Element In Array

Nov 7, 2014

I have been working on this assignment for a bit now. I seem to have most of the logic of it down, as far as I can tell, but I seem to have fallen into a bit of a brain lapse when it comes to invoking methods I've made in my main method. Here's the code:

package Module5;
import java.util.Scanner;
public class Exercise6Nine {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 numbers.");
double[] numbers = new double[10];

[Code] .....

My question is, in my main method, I have code in there to attempt to call my method "min" from below, and to use the result of the min's calculations and print them via my main. Only, I can't seem to properly invoke the method from my main method!

Also, if you could check over my min method and see if anything blatantly wrong is there and let me know, that would be great.

Looking over my question, and it seems there is no EDIT button, but I may as well put the assignment parameters here, since I asked for double checking on my method. Write a method that finds the smallest element in an array of double values using the following header:

public static double min( double[ ] array )

Write a test program that prompts the user to enter 10 numbers, invokes this method to return the minimum value, and displays the minimum value.

View Replies View Related

How To Find Smallest Number In A Group Of 3 Numbers Using While Loop

Mar 23, 2015

import java.util.Scanner;
public class AvgLrgSml{
public static void main(String[]args){
System.out.print("Hello there. Please enter any three numbers.");
Scanner keyboard = new Scanner(System.in);
double num1 = keyboard.nextDouble();
double num2 = keyboard.nextDouble();

[Code]...

View Replies View Related

Return Smallest Of 10 Doubles (User Input)

Oct 1, 2014

This code will let a user type 10 double's, and return the smallest of those 10 double's. But I don't really understand what happens in de last for-loop.

package hsleiden.webcat.exercise06_09;
import java.util.Scanner;
public class Exercise06_09
{
public static void main (String[] args){
double[] allNumbers = new double [10];
 
[Code] ....

View Replies View Related

Find Largest And Smallest Value From Array

Nov 15, 2014

I am trying to display Largest value and Smallest value in an array but when I execute I get Largest value as 0.

Following is the code:

package simpleArray;
import javax.swing.JOptionPane;
class ArrayInteractive {
static String ch = "Q";
static boolean flag = true;
static String a[];

[code]....

View Replies View Related

How To Find Maximum Element Of Each Column In 2D Array

Oct 26, 2014

import java.io.IOException;
public class Largestcolumn
{
public static void main ( String[] args ) throws IOException
{
int largest = 0;
int newnumber = 0;
int[][] data = { {3, 2, 5},

[Code] ....

When I run this code, I get this following output: The largest element in column 0 is: 9.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at largestcolumn.Largestcolumn.main(Largestcolumn.java:27)
Java Result: 1

It outputs the first column's maximum element but then throws an out of bounds error. I'm new to Java and I can't figure out how to fix my code so that it will work for this multidimensional array and output the maximum elements in all of the columns.

View Replies View Related

Program That Takes User Odd Number And Print Prime Numbers Lower Than User Input

Nov 4, 2014

I have been struggling with this program for weeks. This program is supposed to take a user's inputted odd number and then print out all prime numbers lower than that number.

public class PrimeNumber
{
Scanner scan = new Scanner(System.in);
int userNum;
String neg;

public void getUserNum()

[code]...

View Replies View Related

Reversing Numbers (User Input)

Dec 11, 2014

I am writing a program to reverse a user inputted number (Example - 54321 is 12345) The code works great minus the fact that if I input a number that begins with zero, the output will drop the zero. Would it make more sense to just set this up using strings?

import java.util.*;
public class Week7_Programming_Problem
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int inputNum, outputNum;

[Code] ....

View Replies View Related

1D Arrays List Of Numbers From User Input?

Dec 4, 2014

Write a program to maintain a list of the high scores obtained in a game. The program should first ask the user how many scores they want to maintain and then repeatedly accept new scores from the user and should add the score to the list of high scores (in the appropriate position) if it is higher than any of the existing high scores. You must include the following functions:

-initialiseHighScores () which sets all high scores to zero.

-printHighScores() which prints the high scores in the format: “The high scores are 345, 300, 234”, for all exisiting high scores in the list (remember that sometimes it won’t be full).

-higherThan() which takes the high scores and a new score and returns whether the passed score is higher than any of those in the high score list.

-insertScore() which takes the current high score list and a new score and updates it by inserting the new score at the appropriate position in the list

View Replies View Related

Add All Even Numbers Between 2 And User Input Number Which Is Included In Addition?

Apr 17, 2014

import java.util.*;
 public class SumOfAllEvens { 
public static void main (String[] args) { 
Scanner s = new Scanner (System.in);
 //for (int i=1; i<4; i++){
int usernumber;

[code]....

I'm supposed to use a for loop that runs until it reaches the number input by the user, but I'm not sure how to tell the program to add the user's number along with all of the even numbers in between the user input and 2.

View Replies View Related

Find Both Largest And Smallest Of Several Integers

Apr 9, 2015

Write a program that finds both the largest and smallest of several integers. T. For example, a program that determines the winner and loser of a sales contest would input the number of units sold by each salesperson. The salesperson who sells the most units wins the contest; the salesperson who sells the least units loses the contest. Write a pseudocode program, then a Java program that inputs a series of 10 integers and determines and prints the largest integer and the smallest integer. Your program should use at least the following four variables: counter, number , largest and smallest. After i run the code i just get the largest input without the smallest .

This is my code :

import java.util.Scanner; // program uses scanner
// main Class begins
public class Largest12
{
public static void main(String[] args) //main method begins
{
//Initial variables
int counter = 0; // initiate counter
int number=0 ;

[Code] ....

View Replies View Related

Find Arithmetic Mean Of The Numbers Stored In Array

Feb 23, 2014

The program below is intended to find the arithmetic mean of the numbers stored in the array q in two ways: once by storing the numbers in an ArrayList d, where you allow all the necessary conversions to be performed automatically; and once by storing them in an ArrayList e, where you perform all the conversions by hand. Complete the program.

Here is what I have so far:

double[] q = { 0.5, 2.4, 7.4, 2.8, -6.2 };
ArrayList<Double> d = new ArrayList<Double>();
ArrayList<Double> e = new ArrayList<Double>();
for ( double x : q ) {
d.add( x );
e.add ( new Double ( x ) );

[Code] .....

Why does it still show "a / d.size?" I thought I fixed that. Whatever, it's supposed to be "dTotal / d.size()", etc.

View Replies View Related

Logic To Find Adjacency Of Numbers 0 And 1 In Array

Oct 3, 2013

I should mention here that number of elements can be from 1....10'000. And it has to be done as O(N)

I attempted at the problem like so :

public static int adjacencies(int[] A) {
int count = 0;
boolean found = false;
for(int i = 0; i < A.length-1; i++) {
if (A[i] == A[i+1]) {

[Code] .....

To me it looks right and it seems to work.

View Replies View Related

Request A User To Input Two Numbers For Num1 And Num2 And Do Arithmetic?

Feb 6, 2014

New to programming, and my teacher wants us to declare two variables : num1 and num2, and have the user input values, then run it through 2*num1 - num2.

Here is what I have so far, and I am lost and frustrated:

package hellowworld;
import java.util.Scanner;
public class definevariables {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
int num1;
int num2;

[Code] ....

View Replies View Related

Trying To Find 3 Largest Numbers In Array Using Single For Loop

Feb 5, 2014

I am trying to find 3 largest numbers in an array using a single For loop but my following code is still showing randomly sorted numbers.

public class largest3 {
static int m, n, o;
static int array[] = new int[100];
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
array[i] = (int) (Math.random() * 100);

[Code] ....

View Replies View Related

Array Is A Set Of Numbers In Triangle - Find Maximum Path

Aug 12, 2014

If you aren't familiar with the euler problem 67, you are given an array that is a set of numbers in a triangle. Like this

3
7 4
2 4 6
8 5 9 3

and you have to find the maximum path, which for this one is

(3)
(7) 4
2 (4) 6
8 5 (9) 3

I have solved this problem iteratively with the code below

depth = depth-2;
while (depth >=0) {
for (int j = 0; j <= depth; j++) {
values[depth][j] += Math.max(values[depth+1][j], values[depth+1][j+1]);
}
depth -= 1;
}

depth is a variable for the row in the triangle. My problem is that i need the solution to be recursive and i am having trouble doing this. So far i have

public static int findMax(int[][] array,int depth) {
if (depth==0)
return array[0][0];
else if
}

View Replies View Related

Check For Upper Case Letters From User Input - Cannot Find Symbol Compile Error

Apr 15, 2015

I decided to code this quiz I took in class about asking the user to input a string and the code is suppose to check for upper case letters. If a upper case letter is found, it should increase a count by one. Once the check is done, it should display the number of uppercase letters. For some reason I am getting this weird compile error stating that symbols can't be found...

Java Code:

import java.util.*;
import java.lang.*;
public class StringCheck{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("please enter a string: " );
String s = input.nextLine();

[Code] ......

View Replies View Related

Make Program That Declare Largest And Smallest Out Of Three Numbers?

Jan 13, 2015

how can i make a program that declare the largest and smallest out of three numbers??

/*
* To change this license header, choose License Headers in Project Properties.
int a;
int b;
int c;

[Code]....

View Replies View Related

Display Largest And Smallest Numbers In A Series Of 10 Inputs

Apr 15, 2014

I have to get the user to enter in 10 numbers and with those 10 numbers I have to find the total, average, smallest, largest numbers in the set the user inputs. I have the total and average already figured out but how would you go about trying to find the largest and smallest numbers within this set of code.

import java.util.*;
public class testhw7
{
public static void main (String [] args) {
Scannernumberin = new Scanner (System.in);
doublevalue[];
doubletotal;

[Code]...

View Replies View Related

Lottery - Sorting Numbers From Smallest To Biggest Without Repetition

Oct 29, 2014

I have problem with sorting my numbers from smallest to biggest and i need to not have repeating numbers, I am stuck. I tried using Arrays.sorts(lottery) but didn't work. and i don't know to but make the numbers not repeat in the same line.

package lottonumbers;

public class LottoNumbers {
public static void main(String[] args) {
int[] lottery = new int[6];
for (int numoftimes=0;numoftimes<5;numoftimes++){

[Code] ....

View Replies View Related

User Input Random Numbers Then Program Sort Them In Different Ways - Missing Return Statement

Apr 7, 2014

I have been assigned to write a program that has a user input random numbers then the program is to sort them in different ways. I have the coding for the different sorts but, I have an error saying that I am missing a return statement in the "Bubble" method. I am trying to return "arr[i]" in the "for loop" which gives me this error, and when I try to take the "return arr[i]" outside of the "for()" loop the error reads the it cannot locate variable "i".

import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
public class SwingSorts extends JFrame implements ActionListener
{
JRadioButton bubble;
JRadioButton selection;

[Code] .....

View Replies View Related

How To Get Sum Of All Numbers In A Row Of 2D Array With Row Being Selected By User

Mar 9, 2015

I just learned about 2D arrays and am still trying to get a grasp on the concepts. I'm a little confused by how you return all the values in a row to add up and display the sum if the row is entered by the user.

import java.util.*;
public class RowSum {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int userpick = 0;
int sum = 0;

[Code] .....

View Replies View Related

2D Array Input User For GUI

Mar 25, 2014

Java Code:

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class SortTable {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {

[Code]...

View Replies View Related

Creates Array Of 3 Consecutive Ints (0 - 7) User Has To Guess What Those Numbers Are

Aug 16, 2014

I have the following program. In a nutshell, I creates an array of 3 consecutive ints and the user has to guess what those numbers are, knowing that they are between 0 and 7, this is from Head First Java. In the book, the code has a bug that is made on purpose and they challenge you to fix it. As you can see bellow, every time a user's guess matches a int in the array, the NumOfHits is increased by one. When the NumOfHits is 3 (the length of the array) the game finishes.

The bug is that if you guess 1 and 1 is in the array, if you type in 1, 3 times, I will increase the NumOfHits 3 times and end the game as if you had won. To solve the bug, you need to find a way to tell the program that if the user already guessed that number, it should no longer be taken into account and we shouldn't increase the NumOfHits if the same number is provided.

I "fixed" this by searching for the index of the number that matches an int of the array, and changing that index's value to 100, since the user knows that the numbers are between 0 and 7, they will not submit 100.

The result? If the user submits 2 and 2 is in the array, the value of its indexed is replaced by 100, so that if the user submits 2 again it doesn't count as a match.

Please see the comments in the code

import java.io.*;
import java.util.Arrays;

class DotCom{
int NumOfHits = 0;
int[] LocationCells;
public void setLocationCells(int[] locs){
LocationCells = locs;

[Code] .....

The code works, but for some reason, some times I get a ArrayIndexOutOfBoundsException run time error and it appears to be somehow random. This is what I saw on the command line earlier

C:Userspablo.alvarez>java launchDotCom

Enter a number: 3
missed
Enter a number: 4
hit
Enter a number: 5
hit
Enter a number: 7
missed
Enter a number: 5
missed
Enter a number: 4
missed
Enter a number: 3
missed
Enter a number: 4
missed
Enter a number: 5
missed
Enter a number: 6

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at DotCom.checkYourSelf(launchDotCom.java:16)
at DotComGame.startGame(launchDotCom.java:59)
at launchDotCom.main(launchDotCom.java:72)

As you can see here, 3 returned 'missed' but 4 and 5 returned hit. Since the numbers are in sequence and are only 3, it makes sense that the last number is 6 (4,5,6). You will then notice that when I submitted 3 and 4 again it returned 'missed' as expected, because the numbers were already matched earlier, but then when I submitted 6, it returned the error seen above.

Sometimes this doesn't happen at all, sometimes it happens at the 2nd guess or third, it appears to be somehow random. I'm familiar with the error message, but I don't see where I'm using an index that doesn't exist in the array. The only time I'm referencing an array index is on

LocationCells[index] = 100;

but the index variable will only hold a value if the int submitted by the user matches on of the values in one of the indexes, so how is it that I'm going over the number of available indexes?

View Replies View Related

Input 10 Integer Numbers Into Array And Determine Maximum Value Entered

Apr 22, 2015

a. Write a Java program to input 10 integer numbers into an array named fmax and determine the maximum value entered. Your program should contain only one loop, and the maximum should be determined as array element values are being input. (Hint: Set the maximum equal to the first array element, which should be input before the loop used to input the remaining array values.)

b. Repeat 1a, keeping track of both the maximum element in the array and the index number for the maximum. After displaying the numbers, display these two messages:

The maximum value is: _________
This is element number __________ in the list of numbers

Have your program display the correct values in place of the underlines in the messages.

c. Repeat 1b, but have your program locate the minimum value of the data entered.

I did parts a and b but for part see i just want to know if i did it correctly or not

import java.util.Scanner;
public class MinimumValueArray {
public static void main(String[] args) {
//Variable Declaration
Scanner keyboard = new Scanner(System.in);
int size = 10;

[Code] ,.....

When I run it i get this The minimum value is 0.0

The element that holds the value is 0 right away. is this right for the minimum or am i supposed to enter values and it will display the minimum value like in parts a and b wit the maximum? will the minimum just always be 0 or ?

View Replies View Related

User Input For Array Size

Oct 28, 2014

I am having trouble with an assignment. I need the user to input the size of the array and print when asked. In my program, it prints 100 numbers instead of the user input number, such as 15.

import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;
public class Lab9 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int [] values = new int [100];

[Code] .....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved