I need to write a loop that takes in two values from the user-given list and then work with those values in the loop. My issue is that I can not seem to get the loop to take in the 2 values from the String.
Here is my code:
import java.util.Scanner;
public class practice2 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("enter numbers seperated by commas");
String numbers = input.nextLine();
Scanner s = new Scanner(numbers);
s.useDelimiter(",");
for(int i =0; i<numbers.length(); i+=2 ) {
int newnum = i/25;
System.out.println(newnum);
}
}
}
Here, to get the values - do as below
public static void main(String[] args) {
System.out.println("enter numbers seperated by commas");
Scanner input = new Scanner (System.in);
String[] numbers = input.nextLine().split(",\\s*");
//to read number
System.out.println(numbers[0] + " - " + numbers[1]);
//to use them as int
int i = Integer.parseInt(numbers[0]);
System.out.println(++i);
}
Related
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int A = sc.nextInt().split(":");
int B = sc.nextInt();
System.out.println(A + B);
}
}
If I'm given an input like 1:2 then the output should be 3. Likewise 54:6 then 60.
But I'm getting an error. What should I do to achieve that output?
You cannot call split on an integer, it is meant for splitting a String. Try this:
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] numbers = sc.next().split(":");
int A = Integer.parseInt(numbers[0]);
int B = Integer.parseInt(numbers[1]);
System.out.println(A + B);
}
}
Of course some validation would be nice (check if the String contains a colon, if the parts are numeric, etc.), but this should point you in the right direction.
At first, read a whole input line into a String variable. Then just split it into two values and cast them to integer.
Code example:
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();
String[] splittedValues = inputString.split(":");
int
A = Integer.parseInt(splittedValues[0]),
B = Integer.parseInt(splittedValues[1]);
System.out.println(A + B);
you will need to take input as string and then split your input by : and convert the string to integer and add them.
see example below
public class Hello {
public static void main(String[] args) {
String input;
Scanner sc = new Scanner(System.in);
input = sc.next();
String[] parts = input.split(":");
if(parts.length > 0) {
int sum = Integer.parseInt(parts[0])+Integer.parseInt(parts[1]);
System.out.println(sum);
} else{
System.out.println("Enter number in format example 12:2");
}
}
}```
You can try this:
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
sc.next().charAt(0);
int B = sc.nextInt();
System.out.println(A + B);
}
}
I think Splitting of two integers is not possible in java(it is possible in python by using the input.split() function) ,for that reason it is better take input as string and split input by using colon(:) operator and convert those input string to an integer and add the both to print the result.
java code:
import java.util.Scanner;
Public class TwoIntegers
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in());
String[] two_numbers = s.next().split(":");
int fir_num = Int.parseInt(two_numbers[0]);
int sec_num = Int.parseInt(two_numbers[1]);
int sum=fir_num+sec_num;
System.out.println("The sum of two numbers is:"+sum);
}
}
I'm not supposed to use loops for this assignment. Just if/else statements.
I'm trying to get the number of inputs(up to a maximum of 8 inputs) from the scanner and adding them to a counter.
For example, if the user enters 1 3 6 4, the counter should equal 4.
import java.util.Scanner;
public class MyPredictionsOfCodeBehaviour {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.println("Enter some numbers between 1 and 8.");
int counter = userInput.nextInt();
System.out.println(counter);
userInput.close();
}
}
Scan the entire input line as a string instead, split by space and read the resulting array's length
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.println("Enter some numbers between 1 and 8.");
String input = userInput.nextLine(); //Read input as string
System.out.println(input.split(" ").length); //Count inputs
userInput.close();
}
You can use any other delimiter in split() if you like.
this is my code:
public static void main(String[] args) {
System.out.println("Please enter the number of values you would like to enter: ");
Scanner scan = new Scanner(System.in);
int intNumberOfNumbers = scan.nextInt();
for (int i = 0; i < intNumberOfNumbers; i++) {
System.out.println("Please enter a value for index " + i + ":");
int intValue = scan.nextInt();
}
}
What I'm trying to do is create a scanner that asks how many values they want to enter and whatever that value is, that's how many times it asks for number input. The problem is after I ask the question how can I add the number to an array list?
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int numOfInput=sc.nextInt();
ArrayList<Integer> array=new ArrayList<Integer>();
while(numOfInput-->0){
array.add(sc.nextInt());
}
}
public static void main(String[] args) {
System.out.println("Please enter the number of values you would like to enter: ");
Scanner scan = new Scanner(System.in);
int intNumberOfNumbers = scan.nextInt();
ArrayList<Integer> myArray= new ArrayList<>();
for (int i = 0; i < intNumberOfNumbers; i++) {
System.out.println("Please enter a value for index " + i + ":");
int intValue = scan.nextInt();
myArray.add(intValue);
}
}
I want to enter a string of numbers, delimited by ",". I don't know how long it will be. The input will be passed to the program and will end with the letter "x".
JAVA!
import java.util.Scanner;
public class fromUserSum {
/// input : 1,2,4x from user
/// output : 7
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num;
int sumTotal=0;
while(scan.nextByte() != 'x') {
num = scan.nextInt();
sumTotal += num;
}
System.out.println(sumTotal);
scan.close();
}
}
PLEASE help! :)
//////////////
public class fromUserSum {
/// input : 1,2,4x from user
/// output : 7
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String userInput;
do {
System.out.println("Please enter a list of numbers in following format: 1,2,3,4x");
userInput = scan.next();
} while (!userInput.matches("(?:\\d+(?:,\\d+)*)x") || !userInput.matches("\\d+( \\d+)*x"));
scan.close();
String[] numberStrings;
if (userInput.contains(",")) {
numberStrings = userInput.replace("x", "").split(","); // 4x is now 4 and split by ','
} else {
numberStrings = userInput.replace("x", "").split(" ");
}
int sum = 0;
for (String i : numberStrings) {
sum += Integer.valueOf(i);
}
System.out.println("The sum of all numbers in the list is: " + sum);
}
}
Try this
public static void main(String[] args) {
int result = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value");
String string = sc.nextLine();
string = string.replace("x", "");
String[] strArray = string.split(",");
for (String str : strArray) {
result += Integer.valueOf(str);
}
System.out.println("Result is " + result);
}
Input is 1,2,3,4x
Output is Result is 10
import java.io.Console;
import java.util.Scanner;
class MainClass{
public static void main(String args[]) {
int sum=0;
Console c=System.console();
Scanner scan = new Scanner(c.readLine());
scan.useDelimiter("[,x]");
while(scan.hasNextInt())
sum+=scan.nextInt();
scan.close();
System.out.print(sum);
}
}
This does the job as well. Additionally it tells the user what input is expected and if the entered list does not match the format.
public static void main(final String[] args) {
final Scanner userInputScanner = new Scanner(System.in);
String userInput;
do {
System.out.println("Please enter a list of numbers in following format: 1,2,3,4x or 1 2 3 4x");
userInput = userInputScanner.nextLine();
} while (!(userInput.matches("\\d+(,\\d+)*x") || userInput.matches("\\d+( \\d+)*x")));
userInputScanner.close();
final String[] numberStrings = userInput.replace("x", "").split("[, ]");
int sum = 0;
for (final String numberString : numberStrings) {
sum += Integer.valueOf(numberString);
}
System.out.println("The sum of all numbers in the list is: " + sum);
}
Hi how in java we can limit numbers of integers a certain user input? Below is my code of accepting user inputs, how can i ensure that only accept 5 integers from users? Thanks
public static void main(String args[])
{
int a;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string");
a = in.nextInt();
System.out.println("You entered integer "+a);
}
public static void main(String args[])
{
int a[] = new int[5];
int i = 0;
Scanner in = new Scanner(System.in);
while(i<5)
{
System.out.println("Enter an integer");
a[i] = in.nextInt();
i++;
System.out.println("You entered integer "+a[i-1]);
}
}
For a[i] to be between 1 and 100
public static void main(String args[])
{
int a[] = new int[5];
int i = 0;
Scanner in = new Scanner(System.in);
while(i<5)
{
System.out.println("Enter an integer between 1 and 100");
a[i] = in.nextInt();
if(a[i]>=1 && a[i]<=100)
{
i++;
System.out.println("You entered integer "+a[i-1]);
}
}
}
Use a for loop that iterates only 5 times and take the input inside that for loop.
Declare a static field as count=0,
Check whether a>0
If it is , increment count by 1
When count is already 5, dont let the user input anything