I think the problem in this is that the variable max is initialised as 0. Afterwards it remains in the while loop only, so the output is always 0. I dont know how to bring the last max value out of loop and print it.
I need to find the largest value in a scanned file.I've gotten the count, sum, average, evens, and odds myself.The code above the while loop is not mine and my professor said I may no edit it or other wise mess with it. I also may not use arrays.Also I've realized that the largest/smallest are recording the value of count. I've tried the following:
Scanner infile = new Scanner ( new FileReader(args[0]) ); int count=0,sum=0, largest=Integer.MIN_VALUE,smallest=Integer.MAX_VALUE, evens=0, odds=0; double average=0.0;
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);
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;
How to create a simple program that can input number from 1 to n. and display the numbers which input by the user and also display the sum and average of it.
Ex. Please input a number: 1 2 3 4 5 6 7 8 9 10
the sum numbers of 12345678910 is 55 the average number of 12345678910 is ______,
I am very new to Java. I have been working for a couple months on a program for school. It has not gone well. I finally was able to scrap together a working program, but i left something out that needs to be. I have to include input validation to check for negative values, prompting users to re-enter values if negative. I have included my current code, the program works perfectly, but what to do about the negative numbers.
Java Code:
package gradplanner; import java.util.Scanner; public class GradPlanner { public static void main(String[] args) { Scanner input = new Scanner(System.in); int numofclasses = 0; int totalCUs = 0;
I am very new to Java. I have been working on a program. It has not gone well. I finally was able to scrap together a working program, but i left something out that needs to be. I have to include input validation to check for negative values, prompting users to re-enter values if negative.I have included my current code, the program works perfectly, but what to do about the negative numbers.
package gradplanner; import java.util.Scanner; public class GradPlanner { public static void main(String[] args) { Scanner input = new Scanner(System.in); int numofclasses = 0;
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;
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
I'm trying to write a program that calculates the factorials of the numbers 1 through 10, based on user input... My problem is that I don't know how to address the possibility of the user entering something other than a number. When I test the following code by entering a letter, I get an Input Mismatch exception. I'd like to be able to inform the user that the entry is invalid, and ask for another response. Here is my program thus far:
Java Code:
import java.util.Scanner; public class Factorial { public static String entryString; public static char entryChar; public static Scanner input = new Scanner(System.in);
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.
import java.util.Scanner; public class main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int number; System.out.println("Input a number");
[code]....
What i want to do is that if the number is greater than 50 to count until 100. else if the number is less than fifty to count for example from 36 to zero.
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;
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 ?
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]) {
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;
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 static void main(String args[]) { Scanner in=new Scanner(System.in); System.out.println("Enter the Total Number of Integers:"); int totalInteger=in.nextByte(); long largest=0; for(int i=1;i<=totalInteger;i++) { System.out.println("Enter Integer-"+i+":");
[code]...
I want to get Index of that particular number in which Value is largest.
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 ;
I want to reverse grades that i have put using JOptionPane from largest to smallest and the corresponding names..
import javax.swing.*; import java.util.Arrays; public class ArrayUtils { public static void main (String[]args){ String length = JOptionPane.showInputDialog("Number of students");
Write a Java method that returns the largest value of an array of doubles passed to the method as an argument.
Back into java wasn't sure how to do it for doubles did one in the main for integers and then added a method changed from int to double and now i'm lost as go why its not working.
package kickstarter9; public class Kickstarter9 { public static void main(String[] args){ double myList; double[] myList = {6.0, 4.1, 2.4, 6.8, 1.9, 9.4, 2.8, 4.6, 9.3}; // find the largest value in the list
I need to create a program that uses ArrayList to store integers the user inputs, then scan the array for the largest number. I would also like the user to be able to exit this loop if the number 0 is entered.
As you can see below, I'm not sure how to correctly exit the do-while loop. I found this on another forum, but it does not work.
Java Code:
import java.util.*; public class array { public static void main(String [] args){ ArrayList<Integer> list = new ArrayList<Integer>();