import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter A stock symbol (example, BMY, TSLA): ");
String stockSymbol = scanner.nextLine().toUpperCase();
scanner.close();
try {
String apiUrl = "https://query1.finance.yahoo.com/v8/finance/chart/" + stockSymbol + "?interval=1d";
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
double stockPrice = parseStockPriceFromResponse(response.toString());
if (stockPrice != -1.0) {
System.out.println("Current " + stockSymbol + " stock price: $" + stockPrice);
} else {
System.err.println("Error fetching stock price. Please check the stock symbol.");
}
} catch (Exception e) {
System.err.println("Error fetching stock price: " + e.getMessage());
}
}
private static double parseStockPriceFromResponse(String jsonResponse) {
try {
int startIndex = jsonResponse.indexOf("\"regularMarketPrice\":") + 21;
int endIndex = jsonResponse.indexOf(",", startIndex);
String priceString = jsonResponse.substring(startIndex, endIndex);
double stockPrice = Double.parseDouble(priceString);
return stockPrice;
} catch (Exception e) {
System.err.println("Error parsing JSON: " + e.getMessage());
return -1.0;
}
}
}
#Turn this code from a .java to .jar
1 messages · Page 1 of 1 (latest)
Detected code, here are some useful tools:
Formatted code
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter A stock symbol (example, BMY, TSLA): ");
String stockSymbol = scanner.nextLine().toUpperCase();
scanner.close();
try {
String apiUrl = "https://query1.finance.yahoo.com/v8/finance/chart/" + stockSymbol + "?interval=1d";
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null ) {
response.append(line);
}
reader.close();
double stockPrice = parseStockPriceFromResponse(response.toString());
if (stockPrice != - 1.0) {
System.out.println("Current " + stockSymbol + " stock price: $" + stockPrice);
}
else {
System.err.println("Error fetching stock price. Please check the stock symbol.");
}
} catch (Exception e) {
System.err.println("Error fetching stock price: " + e.getMessage());
}
}
private static double parseStockPriceFromResponse(String jsonResponse) {
try {
int startIndex = jsonResponse.indexOf("\"regularMarketPrice\":") + 21;
int endIndex = jsonResponse.indexOf(",", startIndex);
String priceString = jsonResponse.substring(startIndex, endIndex);
double stockPrice = Double.parseDouble(priceString);
return stockPrice;
} catch (Exception e) {
System.err.println("Error parsing JSON: " + e.getMessage());
return - 1.0;
}
}
}
<@&987246399047479336> please have a look, thanks.
You can use </chatgpt:1108714622413963314> to ask ChatGPT about your question while you wait for a human to respond.
u want a runnable jar?
why can't you do it yourself ?
we can show u how to do it urself, perhaps that's better on the long run?
i tried 😭
yeah
please do
do u have an IDE like intellij?
okay, vsc is generally rather bad for java. but afaik it can create runnable jars as well
you could do it from command lines
show them please
one sec
and show the commands you used
so ?
where i got that
open cmd and run javac App.java
(the vsc ui can do it with a few clicks without commands)
really?
yeah as shown in the article i just linked
but ofc it ultimately only uses commands under the hood. commands that u could also use manually
IntelliJ CE is also free =>
Your main class is called App
so does it work ?
one more thing
i tried turning the .jar to a .exe
using Launch4J
it dosent work
like the exe got made
but when i launch it
nun happen
its like a console only application right?
I think you might need to do some setup in launch4j for that
but not sure
yea
do yk how i would do that
it might be a issue with the .jar as i wasnt able to run that either
nope
good
yeah
do i type that into my console?
it auto created one
I think this is not helpful actually
when i used launch4j
I missunderstood the docs I think
o lol
you mean like a xml file tho right
im gonna try geniuinecoder.com
?
alr
kk
is this what could be causing it
wait
i jus realizied
that sais JRE
it depends
not JDK
thats fine
does it matter>
only JRE is needed to run a jar
i coded it in JDK 21
JDK is needed for developer
because?
idk
the guy from youtube did
so version 21 should run fine
do this at least
nice
oop
it crashes
cuz the code is sopose to be a current stock price finder
but once i put in the stock symbol
it crashes
I doubt that, I guess it just finished running and thus closed the program
uh I think you could technically put a scanner.nextLine() a the end to bypass that
and after user presses enter its going to close
not sure if there is a different way (official way)
also I would prefer using the official tool jpackage instead of launch4j
but both works here
if u code it in , u can just convert it again 😉