Jump to content

I need a little java help.

420istoday
Go to solution Solved by Philosobyte,

So I'm in a Java class and it's only been a couple weeks, so I'm pretty new to this.

 

The assignment is to create a program that will take user input of a website from a JOptionPane and return whether it's a government, commercial, or educational website.

 

I tried using String methods, but I can't seem to get my code to isolate the last 3 characters of a string of variable length, even tried doing an indexOf and couldn't get it to display the following characters.

 

I just need tips, hints, or advice, I don't want you to do it for me.

 

Thanks guys.

 

Edit- NVMND  I figured it out, I only needed one int in substring which was the '.' from my  indexOf, it then stores the rest of the string following that as a new string, but please, if you know of an alternate way to do this please feel free to share.

If you look at the API documentation for the String class

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html

You will see there is an "endsWith" method, which only works if the three letter identifier is the last thing in the URL. 

So I'm in a Java class and it's only been a couple weeks, so I'm pretty new to this.

 

The assignment is to create a program that will take user input of a website from a JOptionPane and return whether it's a government, commercial, or educational website.

 

I tried using String methods, but I can't seem to get my code to isolate the last 3 characters of a string of variable length, even tried doing an indexOf and couldn't get it to display the following characters.

 

I just need tips, hints, or advice, I don't want you to do it for me.

 

Thanks guys.

 

Edit- NVMND  I figured it out, I only needed one int in substring which was the '.' from my  indexOf, it then stores the rest of the string following that as a new string, but please, if you know of an alternate way to do this please feel free to share.

Case: Thermaltake Versa H35 | CPU: AMD Ryzen 7 1700x (@4.0Ghz) Cooling: Cooler Master MasterLiquid Lite 240 | MOBO: Gigabyte AB350M-DS3H | RAM: Corsair Vengeance Pro RGB 16GB (2x8GB) 3333Mhz | GPU: MSI ARMOR 8GB OC GTX 1070 | Storage: SAMSUNG 970 EVO 250GB, 1TB Seagate 2.5" 5400RPM | PSU: Corsair CX750M

Link to comment
Share on other sites

Link to post
Share on other sites

So I'm in a Java class and it's only been a couple weeks, so I'm pretty new to this.

 

The assignment is to create a program that will take user input of a website from a JOptionPane and return whether it's a government, commercial, or educational website.

 

I tried using String methods, but I can't seem to get my code to isolate the last 3 characters of a string of variable length, even tried doing an indexOf and couldn't get it to display the following characters.

 

I just need tips, hints, or advice, I don't want you to do it for me.

 

Thanks guys.

I'm assuming you want the TLD identified and return a corresponding string?

|CPU: Intel i7-5960X @ 4.4ghz|MoBo: Asus Rampage V|RAM: 64GB Corsair Dominator Platinum|GPU:2-way SLI Gigabyte G1 Gaming GTX 980's|SSD:512GB Samsung 850 pro|HDD: 2TB WD Black|PSU: Corsair AX1200i|COOLING: NZXT Kraken x61|SOUNDCARD: Creative SBX ZxR|  ^_^  Planned Bedroom Build: Red Phantom [quadro is stuck in customs, still trying to find a cheaper way to buy a highend xeon]

Link to comment
Share on other sites

Link to post
Share on other sites

Educational websites typically have .edu in them, government websites have .gov, and commercial would be .com.

 

perhaps provide the code you currently have? (That way I know what exactly you're trying to do :P)

Specs: CPU - Intel i7 8700K @ 5GHz | GPU - Gigabyte GTX 970 G1 Gaming | Motherboard - ASUS Strix Z370-G WIFI AC | RAM - XPG Gammix DDR4-3000MHz 32GB (2x16GB) | Main Drive - Samsung 850 Evo 500GB M.2 | Other Drives - 7TB/3 Drives | CPU Cooler - Corsair H100i Pro | Case - Fractal Design Define C Mini TG | Power Supply - EVGA G3 850W

Link to comment
Share on other sites

Link to post
Share on other sites

So I'm in a Java class and it's only been a couple weeks, so I'm pretty new to this.

 

The assignment is to create a program that will take user input of a website from a JOptionPane and return whether it's a government, commercial, or educational website.

 

I tried using String methods, but I can't seem to get my code to isolate the last 3 characters of a string of variable length, even tried doing an indexOf and couldn't get it to display the following characters.

 

I just need tips, hints, or advice, I don't want you to do it for me.

 

Thanks guys.

 

Edit- NVMND  I figured it out, I only needed one int in substring which was the '.' from my  indexOf, it then stores the rest of the string following that as a new string, but please, if you know of an alternate way to do this please feel free to share.

If you look at the API documentation for the String class

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html

You will see there is an "endsWith" method, which only works if the three letter identifier is the last thing in the URL. 

Link to comment
Share on other sites

Link to post
Share on other sites

If you look at the API documentation for the String class

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html

You will see there is an "endsWith" method, which only works if the three letter identifier is the last thing in the URL. 

Holy balls, thanks, this just completely changed how I was going about this, you're a life saver.

Case: Thermaltake Versa H35 | CPU: AMD Ryzen 7 1700x (@4.0Ghz) Cooling: Cooler Master MasterLiquid Lite 240 | MOBO: Gigabyte AB350M-DS3H | RAM: Corsair Vengeance Pro RGB 16GB (2x8GB) 3333Mhz | GPU: MSI ARMOR 8GB OC GTX 1070 | Storage: SAMSUNG 970 EVO 250GB, 1TB Seagate 2.5" 5400RPM | PSU: Corsair CX750M

Link to comment
Share on other sites

Link to post
Share on other sites

If you look at the API documentation for the String class

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html

You will see there is an "endsWith" method, which only works if the three letter identifier is the last thing in the URL. 

Holy balls, thanks, this just completely changed how I was going about this, you're a life saver.

It's a solution but not the best one IMO

 

IMO using getHost() then splitting it using split("\\.")

https://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html

|CPU: Intel i7-5960X @ 4.4ghz|MoBo: Asus Rampage V|RAM: 64GB Corsair Dominator Platinum|GPU:2-way SLI Gigabyte G1 Gaming GTX 980's|SSD:512GB Samsung 850 pro|HDD: 2TB WD Black|PSU: Corsair AX1200i|COOLING: NZXT Kraken x61|SOUNDCARD: Creative SBX ZxR|  ^_^  Planned Bedroom Build: Red Phantom [quadro is stuck in customs, still trying to find a cheaper way to buy a highend xeon]

Link to comment
Share on other sites

Link to post
Share on other sites

 

It's a solution but not the best one IMO

 

IMO using getHost() then splitting it using split("\\.")

https://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html

 

Keep in mind he's only been learning Java for 

 

a couple weeks

:D

I don't think he needs the absolutely most efficient solution at this point. 

Link to comment
Share on other sites

Link to post
Share on other sites

Hey guys, I finished the code, it works as the instructions specify it should.

 

Here's the code:

 

import javax.swing.JOptionPane;
 
public class Chapter5project {
 
public static void main(String[] args) {
 
String com = "com";
String gov = "gov";
String edu = "edu";
String org = "org";
 
String website = JOptionPane.showInputDialog(null, "Please enter your website.");
boolean com1 = website.endsWith(com);
boolean gov1 = website.endsWith(gov);
boolean edu1 = website.endsWith(edu);
boolean org1 = website.endsWith(org);
 
if (com1 == true)
JOptionPane.showMessageDialog(null, "This is a business website.");
 
else if (gov1 == true)
JOptionPane.showMessageDialog(null, "This is a government website.");
 
else if (edu1 == true)
JOptionPane.showMessageDialog(null, "This is a university website.");
 
else if (org1 == true)
JOptionPane.showMessageDialog(null, "This is an organization's website.");
 
else JOptionPane.showMessageDialog(null, "This is a website for another entity");

Case: Thermaltake Versa H35 | CPU: AMD Ryzen 7 1700x (@4.0Ghz) Cooling: Cooler Master MasterLiquid Lite 240 | MOBO: Gigabyte AB350M-DS3H | RAM: Corsair Vengeance Pro RGB 16GB (2x8GB) 3333Mhz | GPU: MSI ARMOR 8GB OC GTX 1070 | Storage: SAMSUNG 970 EVO 250GB, 1TB Seagate 2.5" 5400RPM | PSU: Corsair CX750M

Link to comment
Share on other sites

Link to post
Share on other sites

 

Hey guys, I finished the code, it works as the instructions specify it should.

 

Here's the code:

 

import javax.swing.JOptionPane;
 
public class Chapter5project {
 
public static void main(String[] args) {
 
String com = "com";
String gov = "gov";
String edu = "edu";
String org = "org";
 
String website = JOptionPane.showInputDialog(null, "Please enter your website.");
boolean com1 = website.endsWith(com);
boolean gov1 = website.endsWith(gov);
boolean edu1 = website.endsWith(edu);
boolean org1 = website.endsWith(org);
 
if (com1 == true)
JOptionPane.showMessageDialog(null, "This is a business website.");
 
else if (gov1 == true)
JOptionPane.showMessageDialog(null, "This is a government website.");
 
else if (edu1 == true)
JOptionPane.showMessageDialog(null, "This is a university website.");
 
else if (org1 == true)
JOptionPane.showMessageDialog(null, "This is an organization's website.");
 
else JOptionPane.showMessageDialog(null, "This is a website for another entity");

 

Nice.

 

Your classname should be Chapter5Project because classnames use upper CamelCase, where each word's first letter is capitalized. This doesn't affect the code's function, of course, but using conventions makes code more readable. 

 

Try to avoid number suffixes with variable names. Instead of using "com" and "com1," you could use "comString" or "comExtension" for the String and "isCom" for the boolean. These aren't the best names, and they're a bit long, but at least they're specific so that the reader might not have to look back at your previous code to see what class the variable is for. 

Edited by Raymondbl
Link to comment
Share on other sites

Link to post
Share on other sites

Nice.

 

Your classname should be Chapter5Project because classnames use upper CamelCase, where each word's first letter is capitalized. This doesn't affect the code's function, of course, but using conventions makes code more readable. 

 

Try to avoid number suffixes with variable names. Instead of using "com" and "com1," you could use "comString" or "comExtension" for the String and "isCom" for the boolean. These aren't the best names, and they're a bit long, but at least they're specific so that the reader might not have to look back at your previous code to see what class the variable is for. 

@420istoday

 

yes, try using better variable names trust me. It will save you a lot of headache later on in your coding life when you have a proper naming scheme suited to your taste.

 

personally it doesn't matter if its not short/shortened it's not like typing that thing down would be really hard. Plus all IDE's do a pretty good job of letting us read the code easier. With the colors and shit

|CPU: Intel i7-5960X @ 4.4ghz|MoBo: Asus Rampage V|RAM: 64GB Corsair Dominator Platinum|GPU:2-way SLI Gigabyte G1 Gaming GTX 980's|SSD:512GB Samsung 850 pro|HDD: 2TB WD Black|PSU: Corsair AX1200i|COOLING: NZXT Kraken x61|SOUNDCARD: Creative SBX ZxR|  ^_^  Planned Bedroom Build: Red Phantom [quadro is stuck in customs, still trying to find a cheaper way to buy a highend xeon]

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×