How to make my code accepts an input - java

I would just like to ask on how can I make my code to just get the input instead of declaring it? Here's my program. I want to input different atomic numbers and not just "37" like what's in my code. Don't mind my comments, it's in my native language. Thanks!
public class ElectConfi {
public static void main(String s[]) {
int atomicNumber = 37;
String electronConfiguration = getElectronConfiguration(atomicNumber);
System.out.println(electronConfiguration);
}
public static String getElectronConfiguration(int atomicNumber) {
int[] config = new int[20]; //dito nag store ng number of elec. in each of the 20
orbitals.
String[] orbitals = {"1s^", "2s^", "2p^", "3s^", "3p^", "4s^", "3d^", "4p^", "5s^",
"4d^", "5p^", "6s^", "4f^", "5d^", "6p^", "7s^", "5f^", "6d^", "7p^", "8s^"};
//Names of the orbitals
String result="";
for(int i=0;i<20;i++) //dito ung i represents the orbital and tapos ung j
represents ng electrons
{
for(int j=0;(getMax(i)>j)&&(atomicNumber>0);j++,atomicNumber--) //if atomic
number > 0 and ung orbital ay kaya pa magsupport ng more electrons, add
electron to orbital ie increment configuration by 1
{
config[i]+=1;
}
if(config[i]!=0) //d2 nagche-check to prevent it printing empty
orbitals
result+=orbitals[i]+config[i]+" "; //orbital name and configuration
correspond to each other
}
return result;
}
public static int getMax(int x) //returns the number of max. supported electrons by each
orbital. for eg. x=0 ie 1s supports 2 electrons
{
if(x==0||x==1||x==3||x==5||x==8||x==11||x==15||x==19)
return 2;
else if(x==2||x==4||x==7||x==10||x==14||x==18)
return 6;
else if(x==6||x==9||x==13||x==17)
return 10;
else
return 14;
}
}

You can use either a Scanner or BufferedReader and get the user input
Using Scanner
Scanner scanner = new Scanner(System.in);
System.out.println("Please input atomic number");
int atomicNumber = scanner.nextInt();
Using BufferedReader
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int atomicNumber = Integer.parseInt(reader.readLine());

public static String getElectronConfiguration(int atomicNumber) {}
This method accepting any int value and will return String result. so you only need to provide different number as input. There is no change required in this method.
How to provide different inputs?
You can use Scanner to do that.
Scanner scanner = new Scanner(System.in);
System.out.println("Please input atomic number");
int atomicNumber = scanner.nextInt();
Now call your method
String electronConfiguration = getElectronConfiguration(atomicNumber);
What are the other ways?
You can define set of values for atomicNumber in your code and you can run those in a loop

You can get input from command line arguments by doing below :
Scanner scanner = new Scanner(System.in);
String inputLine = scanner.nextLine(); //get entire line
//or
int inputInt= scanner.nextInt();//get an integer
Check java.util.Scaner api for more info - http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
Hope this helps!

You can get the user input from a command line argument:
public static void main(String s[]) {
if (s.length == 0) {
// Print usage instructions
} else {
int atomicNumber = Integer.parseInt(s[0]);
// rest of program
}
}

Related

Resource leak: 'in' is not closed at this location

I just want someone to review my code and suggestion to improve this as a well-written java code.
What I am doing is getting a user input using Scanner, until user input a Number between 0-10.
int Ai = getInput("i", "A");
int Aj = getInput("j", "A");
int Bi = getInput("i", "B");
int Bj = getInput("j", "B");
private static int getInput(String axis, String point) {
int coordinate = 0;
boolean valid = false;
while(!valid){
Scanner in = new Scanner(System.in);
System.out.println("Enter "+ axis +" for " + point +" > ");
if (in.hasNextInt()){
coordinate = in.nextInt();
if (coordinate >= 0 && coordinate <10){
valid = true;
return coordinate;
}
}
}
return coordinate;
}
But I am getting a warning "Resource leak: 'in' is not closed at this location" in line 11 (return coordinate;) Please can someone explain me this
You don't have to instantiate Scanner object in every time the loop runs. So place the Scanner in = new Scanner(System.in); above while loop.
You don't need return inside if block. Because valid = true inside if block which evaluates to false inside while condition which breaks out of loop.
Use this code instead:
private static int getInput(String axis, String point) {
int coordinate = 0;
boolean valid = false;
Scanner in = new Scanner(System.in);
while(!valid)
{
if (in.hasNextInt()){
coordinate = in.nextInt();
if (coordinate >= 0 && coordinate <10){
valid = true;
}
}
}
in.close();
return coordinate;
}
Whenever you open external resources(I/O) in your code in any language( most of them), you have to close that external resource.
Always you have to close the instances of classes that deal with I/O after you are finished with them.
Here the external resource is System.in, you have opened it to take the input but after your work you didn't close that resource. That is why it is asking you to close it.
If you close the scanner in.close() you can't use in to get input next time unless you open it again by instantiating it.
Add the in.close() above both the return statements and there should be no error.
Edit:
The problem is we are closing Scanner in the getinput() which is also closing System.in as said by "Henry" in the comments.
So declare it as static and close the Scanner at the end of the main().
Code:
import java.util.Scanner;
public class Main{
static Scanner in = new Scanner(System.in); //Declared as Static
private static int getInput(String axis, String point) {
int coordinate = 0;
boolean valid = false;
while(!valid)
{
System.out.println("Enter "+ axis +" for " + point +" > ");
if (in.hasNextInt()){
coordinate = in.nextInt();
if (coordinate >= 0 && coordinate <10){
valid = true;
}
}
}
return coordinate;
}
public static void main(String[] args) throws Exception
{
int Ai = getInput("i", "A");
int Aj = getInput("j", "A");
int Bi = getInput("i", "B");
int Bj = getInput("j", "B");
in.close(); // close the scanner when your program is about to end
}
}
when dealing with multiple inputs in a single line
you should use the append the following lines to your code
String[] in= lines.trim().split("\\s+");
for (int i = 0; i < 2; i++) {
a[i] = Integer.parseInt(in[i]);
}
you might not know if a user inputs data in this order the program [earlier] won't be able to parse it causing the function to fail to act properly.

Parsing through a file that uses whitespace as a delimiter -java. (I dont understsand it)

Could someone help me understand what I am doing wrong please?
I have to read through a file with 11 integers and doubles on each line, each line needs to become its own object and stored in an arrayList. However, the delimiter is a single space. And I have used this code, but it doesnt seem to work and I am not sure what I am doing wrong.
package p2_0000000;
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
public class P2_000000 {
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Which file year would you like to analyze:\n"
+ "1) 2007\n"
+ "2) 2011\n"
+ "3) 2013\n"
+ "(Enter number for choice)");
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
ArrayList<dwellingClass> alist = new ArrayList<dwellingClass>();
if (choice == 1) {
try {
File file = new File("2007.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] info = line.split(" ");
int age = Integer.parseInt(info[0]);
int region = Integer.parseInt(info[1]);
double lmed = Double.parseDouble(info[2]);
double fmr = Double.parseDouble(info[3]);
double extremelyLowIncome = Double.parseDouble(info[4]);
double veryLowIncome = Double.parseDouble(info[5]);
double lowIncome = Double.parseDouble(info[6]);
int bedrooms = Integer.parseInt(info[7]);
double value = Double.parseDouble(info[8]);
int rooms = Integer.parseInt(info[9]);
double utility = Double.parseDouble(info[10]);
dwellingClass dwelling = new dwellingClass(age, region, lmed, fmr, extremelyLowIncome, veryLowIncome, lowIncome, bedrooms, value, rooms, utility);
alist.add(dwelling);
}
scanner.close();
} catch (Exception a) {
a.printStackTrace();
System.exit(0);
};
for (dwellingClass each : alist) {
System.out.println(each);
}
}
System.out.println(alist.get(0).getAge());
}
}
I get these errors:
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
Thank you everyone for the help!
I also figured out that this could also work for anyone who reads this post later:
public class P2_0000000 {
public static void main(String[] args) {
// TODO code application logic here
ArrayList<dwellingClass> alist = new ArrayList<dwellingClass>();
try {
File file = new File("2007.txt");
Scanner input = new Scanner(file);
while (input.hasNext()) {
int age = input.nextInt();
int region = input.nextInt();
double lmed = input.nextDouble();
double fmr = input.nextDouble();
double extremelyLowIncome = input.nextDouble();
double veryLowIncome = input.nextDouble();
double lowIncome = input.nextDouble();
int bedrooms = input.nextInt();
double value = input.nextDouble();
int rooms = input.nextInt();
double utility = input.nextDouble();
dwellingClass dwelling = new dwellingClass(age, region, lmed, fmr, extremelyLowIncome, veryLowIncome, lowIncome, bedrooms, value, rooms, utility);
alist.add(dwelling);
}
input.close();
} catch (Exception a) {
a.printStackTrace();
System.exit(0);
};
for (dwellingClass each : alist) {
System.out.println(each.getAge() + each.getRegion());
}
System.out.println(alist.get(0).getAge());
}
}
First you can read the file this way:
Scanner in = new Scanner(new FileReader("2007.txt"));
Secondly to parse white spaces you will need to use something like this:
yourString.split("\\s+");
So your this line should become:
String[] info = line.split("\\s+");
Then you can access your String the way you did it.
But make sure that you are passing the right values i.e. the right types to each of the methods otherwise yo will get the error you are getting.
You should do a number validation of the string you read from the file and make sure that it matches the requirements for building your dwellingClass object.
String line = scanner.nextLine();
String[] info = line.split("\\s+");
boolean validInput = true;
//loop through your info array and check each number is valid beforehand
for(int i = 0; i < info.length; i++)
{
if(!info[i].matches("\\d"))
{
validInput = false;
break;
}
}
//now we want to make sure our input was valid or else we don't create the object
if(info.length == 11 && validInput == true)
{
dwellingClass dwelling = new dwellingClass(
Integer.parseInt(info[0]),
Integer.parseInt(info[1]),
Double.parseDouble(info[2]),
Double.parseDouble(info[3]),
Double.parseDouble(info[4]),
Double.parseDouble(info[5]),
Double.parseDouble(info[6]),
Integer.parseInt(info[7]),
Double.parseDouble(info[8]),
Integer.parseInt(info[9]),
Double.parseDouble(info[4]));
alist.add(dwelling);
}
If you put this inside your while loop it will only create objects with lines read from the file that contain only numbers and contains 11 digits, other lines will simply be ignored. This would allow execution of the file even if a line is not formatted correctly.
You are creating a NEW file, not loading your file. To load a file, you need to use a file input stream. your Scanner wont find anything in a new file and thus its all Null
I stand corrected, the syntax is correct and this would load an existing file. As others have mentioned above its a data issue

Java Scanner delimiter and System.in

I have some problem when I ask the user to input some numbers and then I want to process them. Look at the code below please.
To make this program works properly I need to input two commas at the end and then it's ok. If I dont put 2 commas at the and then program doesnt want to finish or I get an error.
Can anyone help me with this? What should I do not to input those commas at the end
package com.kurs;
import java.util.Scanner;
public class NumberFromUser {
public static void main(String[] args) {
String gd = "4,5, 6, 85";
Scanner s = new Scanner(System.in).useDelimiter(", *");
System.out.println("Input some numbers");
System.out.println("delimiter to; " + s.delimiter());
int sum = 0;
while (s.hasNextInt()) {
int d = s.nextInt();
sum = sum + d;
}
System.out.println(sum);
s.close();
System.exit(0);
}
}
Your program hangs in s.hasNextInt().
From the documentation of Scanner class:
The next() and hasNext() methods and their primitive-type companion
methods (such as nextInt() and hasNextInt()) first skip any input that
matches the delimiter pattern, and then attempt to return the next
token. Both hasNext and next methods may block waiting for further
input.
In a few words, scanner is simply waiting for more input after the last integer, cause it needs to find your delimiter in the form of the regular expression ", *" to decide that the last integer is fully typed.
You can read more about your problem in this discussion:
Link to the discussion on stackoverflow
To solve such problem, you may change your program to read the whole input string and then split it with String.split() method. Try to use something like this:
import java.util.Scanner;
public class NumberFromUser {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] tokens = sc.nextLine().split(", *");
int sum = 0;
for (String token : tokens) {
sum += Integer.valueOf(token);
}
System.out.println(sum);
}
}
Try allowing end of line to be a delimiter too:
Scanner s = new Scanner(System.in).useDelimiter(", *|[\r\n]+");
I changed your solution a bit and probably mine isn't the best one, but it seems to work:
Scanner s = new Scanner(System.in);
System.out.println("Input some numbers");
int sum = 0;
if (s.hasNextLine()) {
// Remove all blank spaces
final String line = s.nextLine().replaceAll("\\s","");
// split into a list
final List<String> listNumbers = Arrays.asList(line.split(","));
for (String str : listNumbers) {
if (str != null && !str.equals("")) {
final Integer number = Integer.parseInt(str);
sum = sum + number;
}
}
}
System.out.println(sum);
look you can do some thing like this mmm.
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Input some numbers");
System.out.println("When did you to finish and get the total sum enter ,, and go");
boolean flag = true;
int sum = 0;
while (s.hasNextInt() && flag) {
int d = s.nextInt();
sum = sum + d;
}
System.out.println(sum);
}

String input in java

I am trying to take string input in java using Scanner, but before that I am taking an integer input. Here is my code.
import java.util.*;
class prc
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int n=input.nextInt();
for(int i=1;i<=n;i++)
{
String str=input.nextLine();
System.out.println(str);
}
}
}
The problem is that if I give a number n first, then the number of string it is taking as inputs is n-1.
e.g if the number 1 is entered first, then it is taking no string inputs and nothing is printed.
Why is this happening ?
Thanks in Advance!
nextLine() reads everything up to and including the next newline character.
However, nextInt() only reads the characters that make up the integer, and if the integer is the last (or only) text in the line, you'll be left with only the newline character.
Therefore, you'll get a blank line in the subsequent nextLine(). The solution is to call nextLine() once before the loop (and discard its result).
Information regarding the code is mentioned in the comments written next to each line.
public static void main(String[] args) {
int num1 = sc.nextInt(); //take int input
double num2 = sc.nextDouble(); //take double input
long num3 = sc.nextLong(); //take long input
float num4 = sc.nextFloat(); //take float input
sc.nextLine(); //next line will throw error if you don't use this line of code
String str = sc.nextLine(); //take String input
}
import java.util.*;
class prc
{
public static void main(String[] args)
{
String strs[];
Scanner input = new Scanner(System.in);
int n = input.nextInt();
input.nextLine();
strs = new String[n];
for(int i = 1; i < n; i++)
{
strs[i] = input.nextLine();
System.out.println(strs[i]);
}
}
}

ChatBot return bug

I'm working on a Chat Bot project, and I'm almost done, other than the fact that whenever I enter an input, it returns multiple outputs depending on the length of the input X.
Here is the source code:
import java.util.*;
public class ChatBot
{
public static String getResponse(String value)
{
Scanner input = new Scanner (System.in);
String X = longestWord(value);
if (value.contains("you"))
{
return "I'm not important. Let's talk about you instead.";
}
else if (X.length() <= 3)
{
return "Maybe we should move on. Is there anything else you would like to talk about?";
}
else if (X.length() == 4)
{
return "Tell me more about " + X;
}
else if (X.length() == 5)
{
return "Why do you think " + X + " is important?";
}
return "Now we are getting somewhere. How does " + X + " affect you the most?";
}
private static String longestWord(String value){
Scanner input = new Scanner (value);
String longest = new String();
"".equals(longest);
while (input.hasNext())
{
String temp = input.next();
if(temp.length() > longest.length())
{
longest = temp;
}
}
return longest;
}
}
This is for testing the Chat Bot:
import java.util.Scanner;
public class Test {
public static void main (String [ ] args)
{
Scanner input = new Scanner (System.in);
ChatBot e = new ChatBot();
String prompt = "What would you like to talk about?";
System.out.println(prompt);
String userInput;
userInput = input.next();
while (!userInput.equals("Goodbye"))
{
System.out.println(e.getResponse(userInput));
userInput = input.next();
}
}
}
I am also trying to modify the Bot so it counts the number of times it has responded; and also modify it so it randomly returns a random response depending on the length of the input. Any help will be much appreciated. Thank You!
You are using the Scanner.next method which only returns the next word in the string. So if you input a string with multiple words, your bot will respond to each of them.
You can use Scanner.nextLine() to get the entire input string, instead of only 1 word.
To count the number of times your bot has responded, you can create a field in the bot class:
private int responseCount = 0;
Then if you change yout getResponse method from a static method to an instance method, you can update this value from this method:
public String getResponse(String value)
{
String X = longestWord(value); //Your longestWord should also not be static.
this.responseCount++;
if (value.contains("you"))
{
...
Regarding counting the responses, just modify your main method:
import java.util.Scanner;
public class Test {
public static void main (String [ ] args)
{
int numberOfResponses = 1;
Scanner input = new Scanner (System.in);
ChatBot e = new ChatBot();
String prompt = "What would you like to talk about?";
System.out.println(prompt);
String userInput;
userInput = input.next();
while (!userInput.equals("Goodbye"))
{
System.out.println(e.getResponse(userInput));
userInput = input.nextLine();
numberOfResponses++;
}
input.close();
System.out.println(numberOfResponses);
}
}
If I have the time I will edit my post in a few minutes to check your problem regarding the double appearences of a response. You also forgot to close the Scanner.
EDIT: It actually happens because scanner has as a default the delimiter set to be on whitespace. so if you input a text with a whitespace, the while loop runs twice for one user input. Just use the nextLine() command.
Why is this code:
Scanner input = new Scanner (System.in);
In your getResponse method? Its not used at all. Take a closer look at your methods as they are holding some strange code.

Categories