Entering String values into a String Array using Scanner Class [duplicate] - java

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
I am learning about Arrays and had gotten this assignment. Whenever I run the code, the value which is supposed to go into String array goes into Int. It skips over the name[i]=sc.nextLine(); completely.
Any Help will be appreciated.
import java.util.*;
public class Marks_Remarks {
public static void main(String []Args){
Scanner sc= new Scanner (System.in);
System.out.println("Enter how many students");
int n= sc.nextInt();
String name[]= new String[n];
int roll[]= new int[n];
int sub1[]= new int[n];
int sub2[]= new int[n];
int sub3[]= new int[n];
for (int i=0;i<n;i++){
System.out.println("Enter Name");
name[i]= sc.nextLine();
System.out.println("Enter Roll No.");
roll[i]= sc.nextInt();
System.out.println("Enter marks in Three Subjects");
sub1[i]= sc.nextInt();
sub2[i]= sc.nextInt();
sub3[i]= sc.nextInt();
}
int avg; String remark;
for (int k=0;k<n;k++){
avg= (sub1[k]+ sub2[k]+ sub3[k])/3;
if (avg>=85)
remark= "Excellent";
else if(avg>=75 && avg<=84)
remark= "Distinction";
else if(avg>=60 && avg<=74)
remark= "First Class";
else if(avg>=40 && avg<=59)
remark="Pass";
else
remark="Poor";
System.out.println("Name: "+ name[k]+ "\tRoll No.: "+ roll[k]+ " \tAverage: "+ avg+ " \tRemark: " + remark);
avg=0;
}
}
}

Use name[i]=sc.next(); instead of : name[i]=sc.nextLine();
It may help you.

Related

Is it possible for this to be put into a for loop while still returning the variables so that the variables could be put into the array in java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
This is what I got so far, I'm trying to loop the user input and have the variables from the user input added to the list below. I tried to put the user input part into a loop but was not able to transfer the variables into the array
// inport
import java.util.*;
// Main program
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //Scans the user inputs and puts it into array
System.out.print("Enter a number- ");
int a = sc.nextInt();
System.out.print("Enter a number- ");
int b = sc.nextInt();
System.out.print("Enter a number- ");
int c = sc.nextInt();
System.out.print("Enter a number- ");
int d = sc.nextInt();
System.out.print("Enter a number- ");
int e = sc.nextInt();
System.out.print("Enter a number- ");
int f = sc.nextInt();
System.out.print("Enter a number- ");
int g = sc.nextInt();
System.out.print("Enter a number- ");
int h = sc.nextInt();
System.out.print("Enter a number- ");
int i = sc.nextInt();
System.out.print("Enter a number- ");
int j = sc.nextInt();
int[] array = { a, b, c, d, e, f, g, h, i, j };
}
}
This is what I tried:
import java.util.*;
class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter in a number- ");
int a = sc.nextInt();
return a;
}
int[] ary = {a, a, a, a, a, a, a, a, a, a};
}
}
The trick is to initialize the array with the number of elements you want to store before collecting the user input.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] array = new int[10];
int i = 0;
while (i < array.length) {
System.out.print("Enter a valid number: ");
try {
array[i] = sc.nextInt();
i++;
} catch (Exception ex) {
System.out.println("Error: please provide a valid number");
sc.nextLine();
}
}
// Print out your elements
for (int element : array) {
System.out.print(element + " ");
}
}
}

readInt and readLine to make basic adding machine in Java [duplicate]

This question already has answers here:
Java User Input and Difference between readInt and nextInt?
(2 answers)
Closed 3 years ago.
I am trying to make a simple adding machine.
public class AddingMachine { // Save as 'AddingMachine.java"
public static void main(String[] args) {
System.out.println();
System.out.println("Welcome to the Adding Machine.");
System.out.println();
String name = readLine("What is your name? ");
int num1 = readInt("What is the first number? ");
int num2 = readInt("What is the second number? ");
System.out.println();
int sum = num1 + num2;
System.out.print(name);
System.out.print(", the sum is: ");
System.out.print(sum);
}
}
The error message reports "cannot find symbol" readInt or readString but I have found the methods on the Oracle website:
https://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html
There may be some other methods that can solve this problem, but I think the read methods make for a very simple input/output example.
This is the solution,
readInt and readLine are the methods from scanner class.
public class AddingMachine { // Save as 'AddingMachine.java"
public static void main(String[] args) {
System.out.println();
System.out.println("Welcome to the Adding Machine.");
System.out.println();
System.out.println("What is the first number?");
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
System.out.println("What is the second number?");
int num2 = sc.nextInt();
System.out.println();
int sum = num1 + num2;
System.out.print(sum);
System.out.print(", the sum is: ");
System.out.print(sum);
}
}

Create a program that will continuously accept number when user does not input same number as the previous inputted number [duplicate]

This question already has answers here:
Need To Take Input To Array Until User Enters 0 JAVA
(3 answers)
Closed 5 years ago.
This is the only program that i can't create. Please help me with this.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i = 0, j=0;
do {
System.out.print("Enter number: ");
i = input.nextInt();
i=j;
}
while (i == j);
System.out.println("You have inputted the same number on the previous.");
}
Cheers m8, good luck at your test :D
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i = 0, j = 999999, input = 0;
do {
i = j;
System.out.print("Enter number: ");
input = input.nextInt();
j=input;
} while (i != j);
System.out.println("You have inputted the same number on the previous.");
}

Java array and nextInt(); do not work properly

Hello guys I am having a problem with an array and a .nextInt(); this is causing my output line at the 3rd prompt to shift up instead of under, and seriously cannot figure out what's wrong.
I have tried .hasNextInt(); but nothing, it actually gives me an error, so here is the code:
import java.util.Random;
import java.util.Scanner;
public class birthday {
public static void main(String[] args) {
System.out.println("Welcome to the birthday problem Simulator\n");
String userAnswer="";
Scanner stdIn = new Scanner(System.in);
do {
int [] userInput = promptAndRead(stdIn); //my problem
double probability = compute(userInput[0], userInput[1]);
// Print results
System.out.println("For a group of " + userInput[1] + " people, the probability");
System.out.print("that two people have the same birthday is\n");
System.out.println(probability);
System.out.print("\nDo you want to run another set of simulations(y/n)? :");
//eat or skip empty line
stdIn.nextLine();
userAnswer = stdIn.nextLine();
} while (userAnswer.equals("y"));
System.out.println("Goodbye!");
stdIn.close();
}
// User input prompt where you make the simulation. For people and return them as an array
public static int[] promptAndRead(Scanner stdIn)
{
System.out.println("Please enter the number of simulations you want to do: ");
int[] userInput =new int [2]; //my problem
userInput[0]= stdIn.nextInt(); //my problem
System.out.println("Please enter the size of the group you want : ");
int[] userInput1 = new int [2];
userInput[1] = stdIn.nextInt();
int a = userInput[1];
while (a<2 || a>365)
{
System.out.println("please type the number that is between 2~365");
}
System.out.println();
return promptAndRead(stdIn);
}
// Method for calculations
public static double compute(int numOfSimulation, int numOfPeople)
{
for (int i =0; i < numOfPeople; i++)
{
Random rnd = new Random(1);
//Generates a random number between 0 and 364 exclusive
int num = rnd.nextInt(364);
System.out.println(num);
System.out.println(num / 365 * numOfPeople * numOfSimulation);
}
return numOfPeople;
}
}
Found it!!!!!!!!!!!
do this:
// User input prompt where you make the simulation. For people and return them as an array
public static int[] promptAndRead(Scanner stdIn)
{
System.out.println("Please enter the number of simulations you want to do: ");
int[] userInput =new int [2]; //CHANGE THIS TO 1?
userInput[0]= stdIn.nextInt(); //my problem
System.out.println("Please enter the size of the group you want : ");
int[] userInput1 = new int [2]; //CHANGE THIS TO 1?
userInput[1] = stdIn.nextInt();
int a = userInput[1];
while (a<2 || a>365)
{
System.out.println("please type the number that is between 2~365");
}
System.out.println();
return(userInput);
}
To return the array
Let me know!
I actually don't think you can do it there with that userInput, I am saying this because the methodology of doing this program is quite arcane.
You are then calling 2 arrays at prompting, I wonder if you might change that to one what will change such as:
// User input prompt where you make the simulation. For people and return them as an array
public static int[] promptAndRead(Scanner stdIn)
{
System.out.println("Please enter the number of simulations you want to do: ");
int[] userInput =new int [2]; //CHANGE THIS TO 1?
userInput[0]= stdIn.nextInt(); //my problem
System.out.println("Please enter the size of the group you want : ");
int[] userInput1 = new int [2]; //CHANGE THIS TO 1?
userInput[1] = stdIn.nextInt();
int a = userInput[1];
while (a<2 || a>365)
{
System.out.println("please type the number that is between 2~365");
}
System.out.println();
return promptAndRead(stdIn);
}
As also the return promptAndRead(stdIn); might be part of the problem
Don't know though just trowing suggestions at Markov ;)

Finding Min/Max numbers in a java array [duplicate]

This question already has answers here:
Java Minimum and Maximum values in Array
(13 answers)
Closed 6 years ago.
I am making a program that finds the min/max numbers in a java array. I am currently stuck at the last part which is finding the min/max. I have currently setup all other parts of the program. This is my code.
import java.util.Scanner;
public class X {
public static void main (String[] args) {
Scanner input= new Scanner(System.in);
System.out.println("Enter size of array");
int n= input.nextInt();
int[] x= new int[n];
System.out.println("Enter Array nums");
for(int i=0;i<n;i++){
x[i]= input.nextInt();
}}}
Arrays.sort(x);
sorts the array, so after doing that all you need to do is look in the first and last element to find the min and max.
You can use the following methods to directly find the max and min.
List list = Arrays.asList(x);
System.out.println(Collections.min(list));
System.out.println(Collections.max(list));
simple.
So Simple actually
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.println("Enter size of array");
int n= input.nextInt();
int[] x= new int[n];
System.out.println("Enter Array nums");
for(int i=0;i<n;i++){
x[i]= input.nextInt();
}}}
Arrays.sort(x);
System.out.println(String.format("Min= %d Max= %d",x[0],x[x.length -1]));
}
import java.util.Scanner;
public class X {
public static void main (String[] args) {
Scanner input= new Scanner(System.in);
System.out.println("Enter size of array");
int n= input.nextInt();
int[] x= new int[n];
int min_num,max_num;
System.out.println("Enter Array nums");
for(int i=0;i<n;i++){
x[i]= input.nextInt();
if(i==0){
min_num=max_num=x[i];
}else{
if(x[i]>max_num)
max_num=x[i];
if(x[i]<min_num)
min_num=x[i];
}}}

Categories