how to scan many time by using for function? - java

import java.util.Scanner;
public class array2 {
public static void main(String[]sef)
{
int a=0;
int number[]=new int[20];
Scanner scan=new Scanner(System.in);
for(a=0;a<number.length;a++); //**i want scan data many time**
{
System.out.println(a+"번째 학생의 숫자를 입력하십시오");
number[a]=scan.nextInt();
}
scan.close();
for(a=0;a<10;a++)
{
System.out.println(number[a]);
}
}
}
i want to input data in arr many time. but i don't know how to do and i don't find answers in my language

I found some bugs in your code and I have added a few things to it. You can have a look at the snippet below.
import java.util.Arrays;
import java.util.Scanner;
public class array2 {
public static void main(String[] args) {
int number[] = new int[5];
Scanner scan = new Scanner(System.in);
for (int a = 0; a < number.length; a++) {
System.out.println(a + "번째 학생의 숫자를 입력하십시오");
System.out.println("Please Enter a number");
number[a] = scan.nextInt();
}
System.out.println(Arrays.toString(number));
scan.close();
}
}

Related

Create a simple java random number pick with in a certain range

I tried searching on here for something similar, but failed to find it, maybe using wrong keywords let me know, but here is the deal.
I am fairly new with java and wanted to make something useful myself.
My idea was to create a random number picker within a range with.
So let's say range is from 1-50, and I want 5 random number in this range, and they have to be all different. I have worked with Random before, but not sure what I am doing wrong, here is the code I have so far, please push me in the right direction if possible.
I want to create an array or list with the number, or is there a better way to do this?
import java.util.Scanner;
import java.util.Random;
public class Randomizer {
static Random rnd = new Random();
static int rnd(int a, int b){
return a+rnd.nextInt(b-a+1);
}
public static void nPicker(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter start of range: ");
int start = sc.nextInt();
System.out.println("Enter end of range: ");
int end = sc.nextInt();
System.out.println("Enter amount of numbers to pick: ");
int c = sc.nextInt();
sc.close();
rnd(start,end);
int[] randomArrays = new int[c];
for(int i = 0; i>randomArrays.length; i++){
randomArrays.add();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
nPicker();
}
}
sorry if my code is messy.
At the moment I can't even get the random number to be added into the array
Try this, I did explain to you what is wrong with comments at the code, read them.
import java.util.Scanner;
import java.util.Random;
public class Randomizer {
static int[] randomArrays; // You need to declare the vector as an instance variable, otherwise it will disappear when the method it's done.
static Random rnd = new Random();
static int rnd(int a, int b){
return a+rnd.nextInt(b-a+1);
}
public static void nPicker(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter start of range: ");
int start = sc.nextInt();
System.out.println("Enter end of range: ");
int end = sc.nextInt();
System.out.println("Enter amount of numbers to pick: ");
int c = sc.nextInt();
sc.close();
randomArrays = new int[c];
for(int i = 0; i<randomArrays.length; i++){ // The condition was wrong
int numberToAdd = rnd(start,end);
randomArrays[i] = numberToAdd; //You are not using an ArrayList, vector has not the method add(), you have to add new element to the vector throughout its index
}
}
public static void main(String[] args) {
Randomizer rdm = new Randomizer();
rdm.nPicker();
for(int number:rdm.randomArrays) {
System.out.println(number);
}
}
}
Thank you everyone for your advices and hints. In the end I think I managed to get the code how I wanted with the code below.
Tell me if you see an error in this method, or if it has a way that is not allowed in java.
import java.util.Scanner;
import java.util.Random;
import java.util.ArrayList;
import java.util.List;
public class Randomizer {
static Random rnd = new Random();
static int rnd(int a, int b){
return a+rnd.nextInt(b-a+1);
}
public static void nPicker(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter start of range: ");
int start = sc.nextInt();
System.out.println("Enter end of range: ");
int end = sc.nextInt();
System.out.println("Enter amount of numbers to pick: ");
int c = sc.nextInt();
sc.close();
List randomnumbers = new ArrayList();
for(int i = 0; i<c; i++){
int numb = rnd(start,end);
if(randomnumbers.contains(numb)){
break;
}else{
randomnumbers.add(numb);
}
}
randomnumbers.forEach(System.out::println);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
nPicker();
}
}
ok in the end i changed the loops as following:
for(int i = 0; i<c;){
int numb = rnd(start,end);
while(!(randomnumbers.contains(numb))){
randomnumbers.add(numb);
i++;
}
}
randomnumbers.forEach(System.out::println);
}
seems to work fine this way

Selection sort with arrays from keyboard in java

Good Morning,
I'm trying to write a code where the user writes some numbers in the keyboard (casually), and the program should write them in order, from the lower to greater.
I don't remember in particular how to insert the number from the keyboard to the array or to the arraylist class if i don't want any limit.
This is my code. i know it is not correct.
import java.util.*;
class Ordine {
public static void main (String [] args) {
Scanner s = new Scanner (System.in)
System.out.println; ("insert the number which you need to order");
int n = s.nextInt();
int [] x = new int [200];
for (int i=0; i<x.length; i++) {
x [i] = s.nextInt();
for (int j=i+1; j<x.length; j++) {
if (x[i] > x[j]) {
//cambia ELEMENTI
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
}
}
Thank you!
Since array have fixed size you will have to use arraylist. Below is the code
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class test{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
ArrayList<Integer> a = new ArrayList<Integer>(Arrays.asList(1,2,3,4,8,9,10,13));
try{
while (true)
{
int l=0;int r=a.size()-1,m=0;
int p=in.nextInt();
while(l<=r){
m=(l+r)/2;
if(a.get(m)>p){
l=l;r=m-1;
}
if(a.get(m)<p){
l=m+1;r=r;
}
if(a.get(m)==p){
l=m;
break;
}
}
a.add(l,p);
System.out.println("Entered number is: "+p);
System.out.print("Sorted arraylist is: ");
for(int j=0;j<a.size();j++){
System.out.print(a.get(j)+" ");
}
System.out.println();
}
}
catch(Exception e){
}
}
}
I have intitialized the arraylist. And it will print the ordered arraylist everytime you enter a new number.
Below is a sample output

How to compare user input in array?

Hi guys sorry I'm a newbie to Java, this is one of the exercise in my class.
I supposed to ask user input 5 numbers, then compare them if they are the same number that entered before.
These are my code so far, but I can't get it work.
Thanks.
import java.util.Scanner;
public class Source {
private static int num = 0;
private static int[] enterednum = new int[5];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
for(int count = 0; count < enterednum.length; count++) {
System.out.println("Enter a number.");
num = input.nextInt();
compare(enterednum);
}
System.out.println("These are the number you have entered: ");
System.out.println(enterednum);
}
public static void compare(int[] enterednum) {
for(int count = 0; count < 6; count++)
if(num == enterednum[count])
System.out.println("The number has been entered before.");
}
}
You may want something like this:
import java.util.Scanner;
public class Source
{
private static int enterednum[]=new int[5];
public static void main(String args[])
{
int num=0; // make this local variable since this need not be class property
Scanner input = new Scanner(System.in);
for(int count=0;count<enterednum.length;count++)
{
System.out.println("Enter a number.");
num = input.nextInt();
compare(num, count);
enterednum[count] = num; // store the input
}
System.out.println("These are the number you have entered: ");
// print numbers in array instead the array
for(int count=0;count<enterednum.length;count++)
{
System.out.println(enterednum[count]);
}
}
// change the method signature to let it get the number of input
public static void compare(int num, int inputcount)
{
for(int count=0;count<inputcount;count++)
{
if(num==enterednum[count])
System.out.println("The number has been entered before.");
}
}
}
You can do this way if you need.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Source {
public static void main(String[] args) throws IOException {
// I used buffered reader because I am familiar with it :)
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// Create a Set to store numbers
Set<Integer> numbers = new HashSet<>();
for (int i = 0; i < 5; i++) {
System.out.print("Enter a number: ");
String line = in.readLine();
int intValue = Integer.parseInt(line);
// You can check you number is in the set or not
if (numbers.contains(intValue)) {
System.out.println("You have entered " + intValue + " before");
} else {
numbers.add(intValue);
}
}
}
}

error: non-static variable scan cannot be referenced from a static context in Java

import java.util.Scanner;
public class CHP4Ex
{
Scanner scan = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("enter a n: ");
int n = scan.nextInt();
int i=10;
while (i<n)
{
System.out.println(i);
i = i + 10;
}
}
}
Why am I getting this error? I'm basically writing a while loop that prints all positive numbers that are divisible by 10 and less than n. For example, if n is 100, enter 10 ... 90.
Put the Scanner class object inside the main function. Basically the problem is that your code violates the static feature. You cannot use non-static members inside a static function, main being static in your case. So it should be :
import java.util.Scanner;
public class CHP4Ex
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("enter a n: ");
int n = scan.nextInt();
int i=10;
while (i<n)
{
System.out.println(i);
i = i + 10;
}
}
}
You can't refer to non static variable in static context, so change
Scanner scan = new Scanner(System.in);
to
private static Scanner scan = new Scanner(System.in); It should work

How do I Store 6 integers from Scanner Console into a Set

I know this is simple. How would I take input from my console and store the input into a Set that can later be used to be returned on a Method. This is what I have so far.
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class InputConsole {
public static void main(String[] args) {
Set<Integer> s = new HashSet<Integer>(6);
int[] numbers = new int[6];
Scanner input = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
System.out.print("Please enter number ");
numbers[i] = input.nextInt();
{
}
}
}
}
I am using and Array just to test with. The Array is set to 6 so if I type 6 numbers in the console it will stop. I have instantiated the HashSet but I don't know how to go about storing the numbers from the console into it.
Use method Set::add()
for (int i = 0; i < numbers.length; i++)
{
System.out.print("Please enter number ");
s.add(input.nextInt());
}
You don't need int[] array
EDIT:
Whole main()
public static void main(final String ... args)
{
final int inputs = 6;
final Set<Integer> s = new HashSet<Integer>(6);
final Scanner input = new Scanner(System.in);
for (int i = 0; i < inputs; i++)
{
System.out.print("Please enter number #" + (i + 1) + ":");
s.add(input.nextInt());
}
System.out.println("Well done!");
System.out.println(s);
}
import java.util.*;
class Hashsetdemo
{
public static void main(String args[])
{
HashSet h=new HashSet(6);
int [] no = new int[6];
Scanner s=new Scanner(System.in);
for (int i=0;i<no.length;i++)
{
System.out.println("please enter number");
h.add(s.nextInt());
}
System.out.println(h);
}
}

Categories