Program should get a number from user then - java

I read an int numer from the user.
I should calculate all numbers which have mod1 until numer is reached.
The program should write them one by one.
public class meraba {
public static void main(String[] args) {
int number;
#SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("Sayı girin");
number = input.nextInt();
for (int i = 0; i < 0; i++) {
if (number == i) {
for(int d = 0;d<number;d++){
}
break;
}
}
}
}
I couldn't get it to do that.
I'm looking forward to a correct solution
in java.

public class meraba {
public static void main(String[] args) {
int number;
#SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("Sayı girin");
number = input.nextInt();
for (int i = 2; i < number; i++) {
if (number % i == 1) {
System.out.println("Mod 1 condition satisfies: " + i);
}
}
}
}
I hope, I understood your question very well ;)

Related

How to modify program to run until a certain key is entered

What is the best way to terminate this by entering a specific keyword or letter? Ideally want the program to terminate by entering a key followed by enter
class NestedLoopTable {
private int start;
private int end;
public NestedLoopTable() {
}
public NestedLoopTable(int aStart, int aEnd) {
super();
start = aStart;
end = aEnd;
}
public void printTable() {
// looping through the number of rows to print the table
for (int i = start; i <= end - start + 1; i++) {
for (int j = start; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}
}
}
import java.util.Scanner;
public class NestedLoopTableApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter start and end");
int start = sc.nextInt();
int end = sc.nextInt();
NestedLoopTable np = new NestedLoopTable(start, end);
np.printTable();
sc.close();
}
}
The best way to terminate the program when a specific key was entered would be to check for the key entered, and if it matches the key that will be entered when the program terminates, then run System.exit(0)
You need a simple loop in for your program:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int start = 0;
while (true) {
System.out.printf("Enter start or %s:\n", EXIT_KEYWORD);
if(scanner.hasNextInt())
start = scanner.nextInt();
else if(scanner.next().equalsIgnoreCase(EXIT_KEYWORD))
return;
}
int end = 0;
while (true) {
System.out.printf("Enter end or %s:\n", EXIT_KEYWORD);
if(scanner.hasNextInt())
end = scanner.nextInt();
else if(scanner.next().equalsIgnoreCase(EXIT_KEYWORD))
return;
}
NestedLoopTable np = new NestedLoopTable(start, end);
np.printTable();
scanner.close();
}
}
(Actually this is just a sample code and you can employ other approaches with much cleaner interface and code!)

Java - Display 1and2 3and4... loop with input

I'm new in learning java and programming. Help me to solve this
input : 5
output : 1and2 3and4 5and6 7and8 9and10
Q1 : is it using even/odd correct?
Q2 : is there any other way?
Please help me solve this
I'm thinking using even and odd here. so this what i think so far
import java.util.Scanner;
class example3{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("input");
int n = scan.nextInt();
int odd = 0;
int even = 0;
for(int i = 0 ; i <= n; i++) {
if(i%2==0) {
even++;
} else {
odd++;
}
n--;
}
System.out.println(odd+ "and" + even);}
}
Well based on the input and output examples you gave in your question, your problem can be rephrased to this:
For an input n, print the first n pairs of odd and even numbers
and some code that does this is the following:
public static void main(String[] args)throws Exception{
Scanner scan = new Scanner(System.in);
System.out.println("input");
int n = scan.nextInt();
int odd = -1;
int even = 0;
for(int i = 1 ; i <= n; i++) {
odd = odd + 2;
even = even + 2;
System.out.println(odd+ "and" + even);}
}
}
and here is the console upon running it:
input
5
1and2
3and4
5and6
7and8
9and10

program ends before reaching while loop

hi guys I'm trying to make a lottery program And I'm trying to get the users input to after asking if they want to retry playing. But my program ends before reaching the while loop.
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String yn = "";
Lottery.getTicket();
Lottery.generateWinningNumbers();
System.out.print("\nWould you like to try again? ");
while(input.hasNextLine())
{
yn = input.nextLine();
if(yn.equalsIgnoreCase("y"))
{
Lottery.getTicket();
Lottery.generateWinningNumbers();
}
else
{
System.out.println("Done");
}
}
input.close();
}
}
In my Lottery class:
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import java.util.Scanner;
public class Lottery {
public static Set<Integer> generateWinningNumbers()
{
Random rndNumbers = new Random();
TreeSet<Integer> winningNumbers = new TreeSet<Integer>();
int max = 40;
int min = 1;
int range;
int sixNum;
for(int i = 0; i < 6; i++)
{
range = max - min + 1;
sixNum = rndNumbers.nextInt(range) + min;
while(winningNumbers.contains(sixNum))
{
sixNum = rndNumbers.nextInt(range) + min;
}
winningNumbers.add(sixNum);
}
System.out.print("Winning Numbers: " + winningNumbers);
return winningNumbers;
}
public static Set<Integer> getTicket()
{
int userInput;
TreeSet<Integer> getNumbers = new TreeSet<Integer>();
Scanner input = new Scanner(System.in);
System.out.println("Enter your 6 numbers between 1-40: ");
for (int i = 0; i<6 ; i++)
{
System.out.print(i+1 + ": ");
userInput = input.nextInt();
while( userInput <1 || userInput > 40 || getNumbers.contains(userInput))
{
if (getNumbers.contains(userInput))
{
System.out.println("Number already picked");
userInput = input.nextInt();
}
if(userInput < 1 || userInput > 40)
{
System.out.println("Invalid. Pick a number between 1-40");
userInput = input.nextInt();
}
}
getNumbers.add(userInput);
}
input.close();
System.out.println("Your ticket was: " + getNumbers);
return getNumbers;
}
}//end of Lottery class
You are using the Scanner Object
Scanner input = new Scanner(System.in);
in Lottery.getTicket and you do
input.close();
This means that System.in will be closed for the rest of the program
Try passing the Scanner object from main to other classes and method that need it.

read multiple inputs from same line in java

Here is my code snippet
public void m1(int a) // a value passed from main
{
for(int i=0;i<a;i++)
{
// Read "a" inputs from the user one by one
}
}
public static void main(String[] args)
{
int a;
// read value of a from user
m1(a)
}
Can U please tell me how to give this input in one line.
Like in the same line we need to provide the value of a and also should take a values from user.
eg:enter code here
a=6. 6 values from user
6 22 33 44 55 66
6 and the 6 inputs from the user should be in the same line (given by the user at the same time).
You could go this way:
public class Number {
static Scanner sc = new Scanner(System.in);
static int arr[];
public static void read(int a)
{
arr = new int[a];
for (int i = 0; i < a; i++) {
arr[i] = sc.nextInt();
}
}
public static void main(String args[]) {
System.out.print("Enter numbers here : ");
int a = sc.nextInt();
read(a);
// printing the array
for (int i = 0; i < a; i++) {
System.out.print(arr[i]+" ");
}
System.out.println("");
}
}
But better and cleaner way will be returning the array from read method:
public class Number {
static Scanner sc = new Scanner(System.in);
public static int[] read(int a)
{
int arr[] = new int[a];
for (int i = 0; i < a; i++) {
arr[i] = sc.nextInt();
}
return arr;
}
public static void main(String args[]) {
System.out.print("Enter numbers here : ");
int a = sc.nextInt();
int numbers[] = read(a);
// printing the numbers array
for (int i = 0; i < a; i++) {
System.out.print(numbers[i]+" ");
}
System.out.println("");
}
}
Input:
Enter numbers here : 4 1 2 3 4
Output:
1 2 3 4
Solve that your problem? I haven't understand exactly what you want, but with this you can do anything, or not?
public static void m1(String[] a) // a value passed from main
{
for (int i = 1; i < a.length; i++) {
// Read "a" inputs from the user one by one
System.out.println(a[i]);
}
}
public static void main(String[] args) {
m1(args);
}
public static void m1(int a) // a value passed from main
{
Scanner scan = new Scanner(System.in);
int arr[] = new int[a];
for(int i=0;i<a;i++)
{
arr[i]=scan.nextInt();
}
}
public static void main(String[] args)
{
int a=(int) 6.6;
// read value of a from user
m1(a);
}
Input:
1 -2 "nonumber" 34
Output:
1
-2
34
Code:
String line = scanner.nextLine();
Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher(line);
while (m.find()) {
System.out.println(m.group());
}
First, a couple of quick pointers:
- Name your methods something more practical than m1()
- Make sure you end your statements with a semi-colon ( e.g m1() )
- You need to define m1() as static, or otherwise instantiate the class which contains m1()
- Learn about Scanners and Arrays; you must import a library to use a Scanner object. ( import java.util.Scanner; )
public static void storeIntegers(int a){
//This is how you declare an array.
int[] someIntArray = new int[a];
//You must create a Scanner object to take in user input.
Scanner userInput = new Scanner(System.in);
for(int i = 0; i < a; i++){
someIntArray[i] = userInput.nextInt();
}
// Just to make sure it worked.
for(int e = 0; e < someIntArray.length; e++){
System.out.println(someIntArray[e]);
}
}// End storeIntegers()
public static void main(String[] args){
Scanner userInput = new Scanner(System.in);
System.out.println("How many numbers?");
int a = userInput.nextInt();
storeIntegers(a);
}// End main()

Small addition to current code

import java.util.Scanner;
public class linecounter {
public static void main(String[] args) {
System.out.print("Enter a line of integers ");
Scanner chopper = new Scanner(System.in);
int x = chopper.nextInt();
while (chopper.hasNextInt()) {
System.out.println(chopper.nextInt());
}
}
}
I am in a CS1 class learning the basics of Java and have a quick question, on this code could anyone tell me how i could get it to keep count of how many integers were typed in?
Thank you
above your while loop, declare:
int count = 0;
then in your while loop use
count++;
This will start you at 0 and every time it increments the count
You could add a counter to the while loop.
int counter = 0;
while (chopper.hasNextInt()) {
counter++;
System.out.println(chopper.nextInt());
}
System.out.println(counter);
In the cases that you have integer numbers, double numbers and you only need count the integer numbers, you can use:
public class linecounter {
public static void main(String[] args) {
System.out.print("Enter a line of integers ");
Scanner chopper = new Scanner(System.in);
int x = chopper.nextInt();
int counter = 0;
while (chopper.hasNextInt()) {
System.out.println(chopper.nextInt());
String myCurrentArg = chopper.nextInt();
if(isInteger(myCurrentArg) ){
counter++;
}
}
System.out.println("The number of integer arguments are: " + counter);
}
public static boolean isInteger(String s) {
return isInteger(s,10);
}
}

Categories

Resources