This program is working but not the way that I'd like. I am able to calculate the max, but I have to first define the number of arguments. I would like to be able to put in a variable number of arguments, then return the max.
How can I do?
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
int max=cin.nextInt();
for(int i=2; i<=n; i++){
int num=cin.nextInt();
if(num>max) max=num;
}
System.out.println(max);
}
I guess you want to get max without giving the number of inputs as the first argument.
read line as a string
tokenize and covert the numbers to integer (convert to float if you allow floating point numbers)
calc max
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
String input=cin.nextLine();
String[] numbers = input.split("\\s");
int max = Integer.parseInt(numbers[0]);
for(int i=1; i<numbers.length; i++){
int num=Integer.parseInt(numbers[i]);
if(num>max) max=num;
}
System.out.println(max);
}
This is my guess, you can tell me if this is what you want to do.
public static void main(String[] args) {
//Your tokenizer
Scanner scanForNumbers = new Scanner(System.in);
//First prompt for number of numbers
System.out.print("Print the Number of Numbers to Enter:");
int numberOfNumbers = scanForNumbers.nextInt();
System.out.println();
int maxOfNumbers = 0;
int tempNum = 0;
//making number of numbers the max iterate till you get there.
for(int index = 0 index < numberOfNumbers; index++){
//Prompt for a number
System.out.print("Enter another Number");
tempNum = scanForNumbers.nextInt();
System.out.println();
//if it is bigger, set a new max.
if(tempNum > maxOfNumbers){
maxOfNumbers = tempNum;
}
}
System.out.println("The Bigger Number is : " + maxOfNumbers);
}
Related
//Modify the program from the previous exercise, so that it displays just the sum of all of the numbers from one to the input number. Be sure to test your program with several inputs.
// Example5.java
import java.util.Scanner;
public class Example5 {
public static void main(String[] args) {
int count = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = in.nextInt();
while (count <= number) {
System.out.println(count);
++count;
}
}
Given that code I have to modify the program that it displays the sum from 1 to the input number.
You asked
How do I accumulate a sum of a series of input values?
In the mentioned practice you need to separate your numbers with whitespace, there are many more advance approach like using IntStream api.
public class Example5 {
public static void main(String[] args) {
int sum = 0;
System.out.println("Add your numbers to sum: ");
Scanner in = new Scanner(new Scanner(System.in).nextLine());
while (in.hasNext()) {
sum += in.nextInt();
}
System.out.println(sum);
}
}
This code gives an output based on the statement, "Given that code I have to modify the program that it displays the sum from 1 to the input number."
import java.util.Scanner;
public class Example5 {
public static void main(String[] args) {
int sum = 0, number;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number: ");
number = in.nextInt();
for (int i = 1; i <= number; i++) sum += i;
System.out.println(sum);
}
}
The user enters 10 numbers. After that, the program asks the user to enter the index number they want to retrieve like the example below.
How do I ask the user to input an index number and print the array in that specific index number?
This is my code so far
public class ArrayElement {
public static void main(String[] args) {
int [] Array = new int[10];
int index;
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 elements:");
for (int i = 0; i<10; i++){
Array[i] = input.nextInt();
}
System.out.print("Enter an index you want to retrieve: ");
index = input.nextInt();
}
}
public static void main(String[] args) {
int [] Array = new int[10];
int index;
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 elements:");
for (int i = 0; i<10; i++){
Array[i] = input.nextInt();
}
System.out.print("Enter an index you want to retrieve: ");
index = input.nextInt();
System.out.print("Element at index "+index+" is "+Array[index]);
}
Output : Element at index 6 is 42
you can get the element of a particular index of an array as follows
int element = Array[index];
How can we take n input in java according to value of T
Like if i take t=2,then we take two n input
If value of t=3,then we take three n input
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner s= new Scanner(System.in);
int t=s.nextInt();
int n=s.nextInt();
int n=s.nextInt();
int evensum=0;
for (int i=1; i<=n; i++) {
if(i%2==0) {
evensum=evensum+i;
}
System.out.print(evensum);
}
}
As in this code i take t=2 and I take two n inputs.
If I take t=3, then what how could I take that third input?
You can't have multiple variable with the same name, for your purpose use a loop :
Scanner s = new Scanner(System.in);
System.out.println("How many values ?");
int nbInput = s.nextInt();
for (int i = 1; i <= nbInput; i++) {
int input = s.nextInt();
System.out.print(input);
}
import java.util.*;
public class Commission {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter sales amount separated by space:");
double[] saleAmounts = Inputs(input);
}
Below is where my function scans numbers, but I need it to scan an x amount of values entered 'at the same time' to set the array length. Also, I need it so the user only enters in values once
(i have the length at 5 for a logic test)
private static double[] Inputs(Scanner sc) {
double[] sales = new double[5];
for (int i = 0; i < sales.length; i++){
sales[i] = sc.nextDouble();
}
return sales;
}
Array is a data structure with set size. If you want to have dynamic sized 'array', use List or ArrayList for example.
From the user, read an entire line (they enter values separated by spaces and hit enter), then pass that to your method and split it on space.
Something like: no exception checking included
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter sales amounts separated by spaces, then hit Enter: ");
double[] saleAmounts= Inputs(input.nextLine());
for(int i=0; i < saleAmounts.length; i++)
{
System.out.println("index " + i + " : " + saleAmounts[i]);
}
}
private static double[] Inputs(String inputLine)
{
String[] values = inputLine.split(" ");
double[] sales = new double[values.length];
for (int i = 0; i < values.length; i++)
{
sales[i] = Double.parseDouble(values[i]);
}
return sales;
}
This code is supposed to capture 5 user integers, print them out, then print them in reverse. It is capturing the first int only, and printing it 3 times, then printing the first integer again 5 more times without reversing. Test ends with "Process finished with exit code 0" which I think is says the program finished without errors -- which of course is not correct. I assume the issue is in how the user input array is stored. I have it assigning as userNum[i] with a limited array of 5, and int i =0 to begin array storage at userNum[0], so I'm not clear on why all the inputs are not captured up to userNum[4].
Thank you for any insight you can provide. I am very new to java and this is prework for my java class.
import java.util.Scanner;
public class ArrayReverse {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in); // scanner for input
final int NUM_VALS = 5; // number on int user able to enter
int[] userNum = new int[NUM_VALS]; // user integers storage
int j = 0;
int i = 0;
System.out.println("Enter integer values: ");
userNum[i] = scnr.nextInt(); // capture user input int
for (j = 0; j < NUM_VALS; j++) {
System.out.print("You entered: ");
System.out.println(userNum[i]);
++j;
}
System.out.print("\nNumbers in reverse: "); // statement to Print reversed array
for (j = NUM_VALS - 1; j >= 0; j--) {
System.out.print(userNum[i] + " ");
}
}
}
You need to work more about on for loops and study how to iterate values in for loop, the problem in your i,j variables.
Here I fix your code.
import java.util.Scanner;
public class ArrayReverse {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in); // scanner for input
final int NUM_VALS = 5; // number on int user able to enter
int[] userNum = new int[NUM_VALS]; // user integers storage
int j = 0;
int i = 0;
//for 5 inputs you need loop
for(;i<NUM_VALS;i++){
System.out.println("Enter integer values: ");
userNum[i] = scnr.nextInt(); // capture user input int
}
for (j = 0; j < NUM_VALS; j++) {
System.out.print("You entered: ");
System.out.println(userNum[j]);
//++j; //no need to increment as you already did in for loop
}
System.out.print("\nNumbers in reverse: "); // statement to Print reversed array
for (j = NUM_VALS - 1; j >= 0; j--) {
System.out.print(userNum[j] + " ");// userNum[0] = your last value which you reverse
}
}
}
Here is a solution using the collections framework:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class ArrayReverse {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in); // scanner for input
final List<Integer> numbers = new ArrayList<>();
System.out.println("Enter any number of integers. (whitespace delimited. enter a non-integer to quit.): ");
while (scnr.hasNextBigInteger()) {
final int n = scnr.nextInt();
System.out.println("Parsed: " + n);
numbers.add(n);
}
System.out.println("Done reading user input.");
System.out.println("Your input: " + numbers);
Collections.reverse(numbers);
System.out.println("Your input reversed: " + numbers);
}
}
I have provided you with a solution. This is a clean way of doing it.
nextInt() reads the next integer that the user inputs. Notice that this will throw a InputMismatchExceptionif the user does not input a integer as value.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> input = new ArrayList<Integer>();
//Simple loop that will read 5 user inputs
//and add them to the input list
for(int i = 0; i < 5; i++){
input.add(scanner.nextInt());
}
//print the 5 values
for(Integer val : input){
System.out.println(val);
}
//reverse the 5 values
Collections.reverse(input);
//print the 5 values again, but they are now reversed
for(Integer val : input){
System.out.println(val);
}
}