im working on a question where the input is given in the below format:
5
7
121
123
7
121
###
4
3
3
2
5
Explanation of the input:
The first number is N, here in example N=5 (N>1 and N<100000). the next are N lines with different numbers ranging from 1 to 100. then comes ###. then the next number is K, here in example K=4. the next are K lines with different values ranging from 1 to 100. the '###' is used to separate N and K inputs.
my question here is how do i take the input from the user using the '###'. and later how can i differentiate between N and K. Kindly help.
please find my code below. but im not able to figure the format to write this type of input. kindly help.
int n=0,N=0,k=0,y=0,K=0;
System.out.println("Enter 'N': ");
n=in.nextInt();
if(n>=1 && n<=100000) {
N=n;
}
int[] a1 = new int[N];
int[] x = new int[N];
System.out.println("Enter values of N: ");
for (int i=0;i<a1.length;i++) {
x[i] = in.nextInt();
}
for (int i=0;i<a1.length;i++) {
if(x[i]>=1 && x[i]<=5000) {
a1[i] = x[i];
}
}
System.out.println("Enter 'K': ");
k=in.nextInt();
if(k>=1 && k<=100) {
K=k;
}
int[] a2 = new int[K];
System.out.println("Enter values of K: ");
for (int i=0;i<a2.length;i++) {
a2[i] = in.nextInt();
}
You should be able to simply use in.skip("###") before reading k to skip the line with the separator.
Related
Write a program to produce the following output for any given integer number between
1 and 9 inclusive.
Enter an integer value [1..9]: 6
1
12
123
1234
12345
123456
666666
66666
6666
666
66
6
I have done the top half but I can not figure out the bottom with the repeating user input.
package lab7;
import java.util.Scanner;
public class problem5 {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
System.out.println("Input an integer between 1 and 9");
int input = scan.nextInt();
while (input <= 9) {
for (int i = 1; i <= input; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
break;
}
}
}
Expected result: included at the top; actual result so far (input of 5):
1
12
123
1234
12345
You're pretty close. You have a for loop that covers the first half of the output you want. You can add a second for loop to handle the second half of the output.
This is pretty similar to the first loop, but has a few small differences:
instead of the loop variable starting at 1 and increasing, this one starts at input and decreases each time through (i-- instead of i++)
instead of printing any of the loop variables (i or j), it prints the input value ("6" in your example)
for (int i = input; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(input);
}
System.out.println();
}
If I run that code locally – so your for loop, then this for loop, then the break statement – this is the output:
Input an integer between 1 and 9
6
1
12
123
1234
12345
123456
666666
66666
6666
666
66
6
I would prefer a more efficient algorithm, your current approach is O(n2); consider the digits '1' - '9'; if we store them in a String then we can take a simple substring of that String for each line at the top (for example, "123456789".substring(0, 3) -> "123") that can be used to generate the top through successive calls to substring. We can use a similar approach to build the bottom; use an array of all possible rows and iteratively call substring. Finally, don't forget to validate that input is between one and nine inclusive. Something like,
Scanner scan = new Scanner(System.in);
String digits = "123456789";
String[] btm = { "1", "22", "333", "4444", "55555",
"666666", "7777777", "88888888", "999999999" };
System.out.println("Input an integer between 1 and 9");
int input = scan.nextInt();
if (input < 1 || input > 9) {
System.err.printf("Invalid input: %d%n", input);
System.exit(1);
}
for (int i = 0; i < input; i++) {
System.out.println(digits.substring(0, i + 1));
}
for (int i = input - 1; i >= 0; i--) {
System.out.println(btm[input - 1].substring(0, i + 1));
}
My problem is that only the 5th input gets printed while the rest is not
Scanner ns = new Scanner(System.in);
int n = 0;
int i=1;
while(i<=5)
{
System.out.println("enter a number");
n = ns.nextInt();
i++;
}
System.out.println(+n);
System.out.println(+n);
System.out.println(+n);
System.out.println(+n);
System.out.println(+n);
Let's say I typed 1, 2, 3, 4, 5 respectively,
it should look like this
1
2
3
4
5
But instead I get
5
5
5
5
5
You could store your input parameters into ArrayList
public static void main(String[] args) {
Scanner ns = new Scanner(System.in);
int n = 0;
int i = 1;
List<Integer> params = new ArrayList<>();
while (i <= 5) {
System.out.println("enter a number");
n = ns.nextInt();
params.add(n);
i++;
}
for (Integer param : params) {
System.out.println(param);
}
}
Output:
1
enter a number
2
enter a number
3
enter a number
4
enter a number
5
1
2
3
4
5
All the inputs are being printed, the problem is that the while loop sets n to the value of 5 and then you print 5 five times. The correct code you are looking for is:
int n = 0;
int i=1;
while(i<=5)
{
System.out.println("enter a number");
n = ns.nextInt();
i++;
System.out.println(+n);
}
Please print number in loop. See code below :
Scanner ns = new Scanner(System.in);
int n = 0;
int i=1;
while(i<=5)
{
System.out.println("enter a number");
n = ns.nextInt();
System.out.println(n);
i++;
}
So basically the assignment says i need to get a number 'n' from the user and for all the numbers between 1 to 'n' code the program to print all the numbers divided by 3 without residue && print ONLY the numbers that both (or one) of their digits equal to 5 or less, for example if the user give 22 the programs prints 3,12,21.
thats what ive done by now (the place i putted a question mark is where im having hard time to figure out what to do) so this code in not compiled yet :
public static void main(String[] args) {
Scanner get = new Scanner(System.in);
int num;
System.out.println("Enter A Random Number: ");
num = get.nextInt();
for (int i=1;i>0 && i<=num;i++) {
if (i%3==0 && ?)
System.out.println(i);
I'm a bit confused about the second part of your question but based on the first part and what I got from the second part ( that the sum of the digits of numbers that is equal or less to 5 should only be printed )
Here is the code for your program : (It should work perfectly, update me if you find any problems)
public static void main(String[] args) {
Scanner get = new Scanner(System.in);
int num;
System.out.println("User please enter a number of your choice : ");
num = get.nextInt();
for(int x = 1 ; x < num ; x++){
String number = ""+ x ;
int sum = 0 ;
for(int i = 0 ; i < number.length() ; i ++ ){
sum +=number.charAt(i)-'0' ; }
if(x % 3 == 0 && sum <= 5){
System.out.println(x) ; }
sum = 0 ;
}
}
divided by 3 without residue
If you understand what "without residue" means, then I assume you are familiar with modulo arithmetic. In programming, we have the modulo operator % which returns the remainder from a division. So 25 % 8 evaluates to 1. You can use this to get the digits of a number 21 % 10 evalutes to 1 which is exactly the ones digit. To get the the tens digit, we need to divide by 10 first 21 / 10 % 10 evaluates to 2. This works because integer division throws away the remainder.
This will do. So, you have to go through every digit so, I converted it into a string and then matched the regex for 1 to 5 on that character and then put it back in another string and it solves it.
public static void main(String[] args) {
Scanner get = new Scanner(System.in);
int num;
System.out.println("Enter A Random Number: ");
num =Integer.parseInt(get.nextLine());
for(int i =1;i<=num; i++){
if(i%3==0){
String input = Integer.toString(i);
String toPrint = "";
for(int j =0 ; j<input.length();j++){
if(Character.toString(input.charAt(j)).matches("^[1-5]$")){
toPrint+=Character.toString(input.charAt(j));
}
}
//check the length to avoid cases like 30,60 etc.
if(input.length()==toPrint.length()){
System.out.println(toPrint);
}
}
}
}
Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, the program should: output the individual digits of 3456 as 3 4 5 6 and the sum as 18, output the individual digits of 8030 as 8 0 3 0 and the sum as 11, output the individual digits of 2345526 as 2 3 4 5 5 2 6 and the sum as 27, and output the individual digits of 4000 as 4 0 0 0 and the sum as 4.
Moreover, the computer always adds the digits in the positive direction even if the user enters a negative number. For example, output the individual digits of -2345 as 2 3 4 5 and the sum as 14.
This is the question I'm having minor difficulties with, the only part I can't figure out is how can I print the single integers in the order that he wants, from what I learned so far I can only print them in reverse. Here's my code:
import java.util.*;
public class assignment2Q1ForLoop {
static Scanner console = new Scanner (System.in);
public static void main(String[] args) {
int usernum, remainder;
int counter, sum=0, N;
//Asaking the user to enter a limit so we can use a counter controlled loop
System.out.println("Please enter the number of digits of the integer");
N = console.nextInt();
System.out.println("Please enter your "+N+" digit number");
usernum = console.nextInt();
System.out.println("The individual numbers are:");
for(counter=0; counter < N; counter++) {
if(usernum<0)
usernum=-usernum;
remainder = usernum%10 ;
System.out.print(remainder+" ");
sum = sum+remainder ;
usernum = usernum/10;
}
System.out.println();
System.out.println("the sum of the individual digits is:"+sum);
}
}
You have to storeremainder variables in an array and then print them in the loop from last index to first as shown in this tutorial.
You can either store digits in array and then print them, or you can try something like that:
final Scanner console = new Scanner(System.in);
System.out.println("Please enter your number");
final int un = console.nextInt();
long n = un > 0 ? un : -un;
long d = 1;
while (n > d) d *= 10;
long s = 0;
System.out.println("The individual numbers are:");
while (d > 1) {
d /= 10;
final long t = n / d;
s += t;
System.out.print(t + " ");
n %= d;
}
System.out.println();
System.out.println("the sum of the individual digits is:" + s);
An idea would be : convert int to string and write a method
getChar(int index) : String
which gives you for example 4 from 3456 with
getChar(2);
See Java - Convert integer to string
Here, I wrote a code for your problem with using a stack. If you want a simple code, you can comment my solution and I will wrote another one.
Scanner c1 = new Scanner(System.in);
System.out.print("Enter the number: ");
int numb = c1.nextInt();
numb = Math.abs(numb);
Stack<Integer> digits = new Stack<Integer>();
while(numb>0){
int n = numb%10;
digits.push(n);
numb = numb/10;
}
int sum = 0;
while(!digits.isEmpty()){
int n = digits.pop();
sum+=n;
System.out.print(n+" ");
}
System.out.print(sum);
I'm working on a simple reservation system with 10 elements (seats). I want to check if elements from 1 to 5 has been set. If Yes, then set the elements from 6 to 10 (Vice-Versa).
An element should not be assigned a value more than once. My code so far.
boolean[] seats = new boolean[10];
Scanner input = new Scanner(System.in);
System.out.print("Choose FirstClass(1) / Economy(2): ");
int flightClass = input.nextInt();
for (int j = 0; j < seats.length; j++) {
System.out.println("\nEnter Seat Number: ");
int enterSeat = input.nextInt();
if (flightClass == 1) {
if (enterSeat >= 0 && enterSeat <= 5) {
System.out.println("You're in the First Class.");
seats[enterSeat] = true;
System.out.printf("You're Seat Number is %d\n", enterSeat);
}
} else if (flightClass == 2) {
if (enterSeat >= 6 && enterSeat <= 10) {
System.out.println("You're in the Economy.");
seats[enterSeat] = true;
System.out.printf("You're Seat Number is %d\n", enterSeat);
}
}
My Question: How do I check if elements from 1 to 5 has been set, and if they have, set the elements from 6 to 10, and vice versa?
For example:
Enter seat no. to book:
1
Enter seat no. to book:
2
Enter seat no. to book:
3
Enter seat no. to book:
4
Enter seat no. to book:
5
All the first class seats(1-5) has been set. Now the remaining seats are from 6 - 10.
Enter seat no. to book:
6 so on...
Arrays in Java1 are indexed from zero, not from one. Therefore, your code that checks >=1 and <=10 should be changed to >=0 and <=9, or use2
seats[enterSeat-1]
instead of
seats[enterSeat]
To find the next available element from among the elements of a sub-array, you can use this loop:
int firstFree = -1;
for (int j = 0 ; j != 5 ; i++) {
if (!seat[j]) {
firstFreeSeat = j;
break;
}
}
if (firstFreeSeat == -1) {
System.out.printl("Sorry!");
}
1 As well as C, C++, C#, Objective C, and many other languages
2 This is something you may want to do if you expect the user to enter numbers one through ten rather than zero through nine - a more natural choice for seat numbering.