How to input in array using java [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hello i have been trying to make a program THAT ASK
1)How many students are there??
2)Asks a name and Test marks
can somebody help me with this . Im new to programming here is my code
Scanner input = new Scanner(System.in);
System.out.println("How many Students in class?");
int n = input.nextInt();
System.out.println("Enter the " + n + " names: ");
String [] names = new String[n];
for (int i = 0; i < names.length; i++)
{
names[i] = input.nextLine();
}

It is a "problem" with nextInt, the method only read the number and leave the <enter> so the next time you call nextLine, the <enter> being present, it will read this, so the value is empty. You need to clear the input first, simply by reading the line.
System.out.println("How many Students in class?");
int n = input.nextInt();
input.nextLine(); //will consume the \n

Related

Is there a specific way to make this code better for a beginner in java? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
int k=0;
System.out.println("Enter the number of times for a sentence to loop.\n");
Scanner i = new Scanner( System.in);
int j = i.nextInt();
System.out.println("Enter the sentence to be looped. \n");
Scanner sentence = new Scanner(System.in);
String sent = sentence.nextLine();
while (k < j ) {
System.out.println(sent);
}
}
}
This is just a program to take a sentence from a user and then print it the number of times the user requires it.
Just started learning it. I have a bit of experience in C , but this seems a bit too tedious could i use any other options ?
There's a couple of things that could be improved. Firstly, the code doesn't even work properly since k is never incremented (causes an infinite loop).
while (k < j ) {
System.out.println(sent);
k++;
}
Also, you only need one Scanner object to read user input. Creating a second one is just waste of memory and makes the code more confusing to read.
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int y = scanner.nextInt(); //we can use the same object for a different variable.
In Java, class names should always start with an uppercase letter. This is to differentiate objects/classes from methods (which should start with a lowercase letter). This makes the code easier to read.
public class Hello {
Another thing, when you know exactly how many times you want to loop, it's better to use a for loop.
for(int k=0; k<j; k++) {
System.out.println(sent);
}

Using the Scanner function to get user input [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
so I am trying to use the scanner class to receive a few different inputs from a user. They can enter in a movie title, year, director, duration, actors, and genre.
However, for one of the user inputs, it is printing the director and duration together.
Here is a screenshot for more reference. Why is is asking for user input for director and duration at the same time but expecting a user input for duration?
Code and Output
System.out.println("Enter the title: ");
String title = myObj.nextLine();
System.out.println("Enter the year: ");
int year = myObj.nextInt();
System.out.println("Enter the duration: ");
int duration = myObj.nextInt();
System.out.println("Enter the director: ");
String director = myObj.nextLine();
System.out.println("Enter the actors: ");
String actors = myObj.nextLine();
System.out.println("Enter the genre: ");
String genre = myObj.nextLine();
int rating = ran.nextInt(10) + 1;
Calling nextInt() will not consume the newline. It will instead be consumed by the subsequent call to nextLine() when getting the director. To solve this you can instead call nextLine() and then Integer.parseInt() to convert the string to an integer.
.nextInt() will only read the integer provided in the input. Now the scanner cursor is at "/n"(newline). To avoid this you can either use
int duration = Integer.parseInt(myObj.nextLine());
or
int duration = myObj.nextInt();
myObj.nextLine();

How to save stdin data into class/object [JAVA] [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to use a Class to hold user inputs. For example "example_name";"example_value";"example_city" is the form of the input and I want to make objects from an input stream of theese three inputs line by line from a file. So whenever I enter Adam;27; London it should be saved into an Object(Adam,27,London) and ask for the next input.
Thank you for your help, I know this might be a stupid question but I'm new to OO programming and I have a C background and don't want to use two dimensional arrays.
Let's break this problem into 3 section. First, we will have to design the class. Second, we will take the user input and create an object of that class until the user press n(no). Third, we will display the user information. Here I have used a LinkedList data structure for storing the inputs.
Section 1:
class example{
String example_name;
int example_value;
String example_city;
example(String name,int value,String city)
{
this.example_name = name;
this.example_value = value;
this.example_city = city;
}
}
section 2:
LinkedList<example> ob = new LinkedList<>();
Scanner input = new Scanner(System.in);
System.out.println("Press y to Continue n to Exit");
char i = input.next().charAt(0);
while(i!='n')
{
input.nextLine();
System.out.println("Enter name");
String name = input.nextLine();
System.out.println("Enter value");
int value = input.nextInt();
input.nextLine();
System.out.println("Enter city");
String city = input.nextLine();
ob.addLast(new example(name,value,city));
System.out.println("Press y to Continue n to Exit");
i = input.next().charAt(0);
}
Section 3:
for(example x:ob)
System.out.println(x.example_name + " " + x.example_value + " "+x.example_city);

Unable to print all the characters of a string by charAt() in Java? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a question. I am a beginner in programming and I have been stuck here for hours now. I want to know how you can read all the letters in a certain user input.
String naem = inPut.nextLine(); //where inPut is a "Scanner inPut = new Scanner (system.in)
nameInput = naem.charAt(0); //where nameInput is a char
System.out.println("Name: "+nameInput);
I am making a program where have to take input a name and all the characters of that name will be printed through System.out.println("Name: " +nameInput);
I'd really appreciate it if you could help out.
You want to print the naem which you are receiving by the input and not a single character which is what you'd get if you used .charAt().
nameInput = naem.charAt(0); //1
System.out.println("Name: "+nameInput);//2
What actually these two lines are doing is that :
Line 1 is taking the first character of the string and storing it into nameInput and then line 2 is printing it.
Now if you want to print all the character in this way you have to go through the entire string. You can do this by using loop.
Here is an Example :
String naem = inPut.nextLine(); //where inPut is a "Scanner inPut = new Scanner (system.in)
System.out.println("Name: ");
for (int i = 0; i < naem.length(); i++)
{
char nameInput = naem.charAt(i);
System.out.print(nameInput);
}
By naem.length(), you will get the length of the string.

How to handle wrong user input [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My program requires user to enter a 4-digit number(int type). Currently, when the user enters any character other than numeric, the program crashes. My hope is that when the user enters a non-numeric character, the program can tell the user "Wrong input" then let the user re-enter.
Does exception handling work for this? Or is there any other good way to solve this?
If you are using scanner, you can use the nextInt() method with the condition hasNextInt() which would only read integer inputs. Have a look at this post for example:
Taken from the above mentioned post.
Scanner sc = new Scanner(System.in);
System.out.print("Enter number 1: ");
while (!sc.hasNextInt()) sc.next();
int num1 = sc.nextInt();
int num2;
System.out.print("Enter number 2: ");
do {
while (!sc.hasNextInt()) sc.next();
num2 = sc.nextInt();
} while (num2 < num1);
System.out.println(num1 + " " + num2);
Also you can look at the documentation for scanner's nextInt() here

Categories

Resources