Jump to content

Need Help with an assignment.

420istoday

I am in a Java class, it's about halfway through, we haven't been over regex, so this should be limited to the basics, most likely loops and strings.

 

So the assignment is:

 

"Write a program (using JOptionPane for both input and output) that reads a text file with a list of website names until the end of the file is reached. Display the file names to the output window with a total count of website names that are commercial (ends with .com) and a count of websites that educational (end with .edu)."

 

 

I can import the file and have it read, but I can't seem to figure out in which way I'm supposed to count each of these website domains. I thought about breaking them up into individual strings, then using .endswith(), but I feel there is a much simpler solution I am not seeing. 

 

I don't have any code to show right now, I left it on my school computer, but it ended right where I had the file imported and printed to console as a test.

 

Help is greatly appreciated.

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

code would be super helpful. Would jog my memory aswell and might be able to at least give some hints

Remember kids, the only difference between screwing around and science is writing it down. - Adam Savage

 

PHOΞNIX Ryzen 5 1600 @ 3.75GHz | Corsair LPX 16Gb DDR4 @ 2933 | MSI B350 Tomahawk | Sapphire RX 480 Nitro+ 8Gb | Intel 535 120Gb | Western Digital WD5000AAKS x2 | Cooler Master HAF XB Evo | Corsair H80 + Corsair SP120 | Cooler Master 120mm AF | Corsair SP120 | Icy Box IB-172SK-B | OCZ CX500W | Acer GF246 24" + AOC <some model> 21.5" | Steelseries Apex 350 | Steelseries Diablo 3 | Steelseries Syberia RAW Prism | Corsair HS-1 | Akai AM-A1

D.VA coming soon™ xoxo

Sapphire Acer Aspire 1410 Celeron 743 | 3Gb DDR2-667 | 120Gb HDD | Windows 10 Home x32

Vault Tec Celeron 420 | 2Gb DDR2-667 | Storage pending | Open Media Vault

gh0st Asus K50IJ T3100 | 2Gb DDR2-667 | 40Gb HDD | Ubuntu 17.04

Diskord Apple MacBook A1181 Mid-2007 Core2Duo T7400 @2.16GHz | 4Gb DDR2-667 | 120Gb HDD | Windows 10 Pro x32

Firebird//Phoeniix FX-4320 | Gigabyte 990X-Gaming SLI | Asus GTS 450 | 16Gb DDR3-1600 | 2x Intel 535 250Gb | 4x 10Tb Western Digital Red | 600W Segotep custom refurb unit | Windows 10 Pro x64 // offisite backup and dad's PC

 

Saint Olms Apple iPhone 6 16Gb Gold

Archon Microsoft Lumia 640 LTE

Gulliver Nokia Lumia 1320

Werkfern Nokia Lumia 520

Hydromancer Acer Liquid Z220

Link to comment
Share on other sites

Link to post
Share on other sites

you could use String.indexOf(substring,start) where 'start' is the location in the string where you want to start the search.

This returns the first location where the substring occurs, you can then start the search again at that location+substring.length().

Repeat untill indexOf returns -1 (doesn't find anything).

 

So something like this:

while(start!= -1){    start= input.indexOf(substring,start);    if(start!= -1){        count ++;        start+= substring.length();    }}
Link to comment
Share on other sites

Link to post
Share on other sites

 

you could use String.indexOf(substring,start) where 'start' is the location in the string where you want to start the search.

This returns the first location where the substring occurs, you can then start the search again at that location+substring.length().

Repeat untill indexOf returns -1 (doesn't find anything).

 

So something like this:

while(start!= -1){    start= input.indexOf(substring,start);    if(start!= -1){        count ++;        start+= substring.length();    }}

I need it to do a text file with about 30 lines of varying length though, that and I'd have to convert each one into a string. That's why I haven't done it this way.

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

Not sure if I understand, but I think this is what you want:

String outputString = "";Scanner s = new Scanner(your file here);int dotComCount = 0;while(s.hasNextLine()) {    currentLine = s.readLine;    outputString += currentLine;    switch(currentString.substring(currentString.lastIndexOf("."), currentString.length)) {    case ".com":        dotComCount++;    //Do this for .edu, .org, and anything else    }}//Print out result (outputString and int values) here

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
Share on other sites

Link to post
Share on other sites

 

Not sure if I understand, but I think this is what you want:

String outputString = "";Scanner s = new Scanner(your file here);int dotComCount = 0;while(s.hasNextLine()) {    currentLine = s.readLine;    outputString += currentLine;    switch(currentString.substring(currentString.lastIndexOf("."), currentString.length)) {    case ".com":        dotComCount++;    //Do this for .edu, .org, and anything else    }}//Print out result (outputString and int values) here

More like this....

I just need it to count the ".com",".edu", etc.

import java.util.Scanner;import java.io.*;import javax.swing.JOptionPane;public class Chapter6Project60 {	public static void main(String[] args) throws IOException {		int comCount = 0, eduCount = 0, govCount = 0, orgCount = 0;				File inputFile = new File ("websiteList.txt");		Scanner scan = new Scanner(inputFile);				FileReader fileReader = new FileReader(inputFile);		BufferedReader bufferedReader = new BufferedReader(fileReader);		StringBuffer stringBuffer = new StringBuffer();		String line;		while ((line = bufferedReader.readLine()) != null) {			stringBuffer.append(line);			stringBuffer.append("\n");		}		fileReader.close();				JOptionPane.showMessageDialog(null,"Contents of file:\n"+ stringBuffer.toString());

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

 

More like this....

I just need it to count the ".com",".edu", etc.

-snip-

I got it, thanks for the input guys!

import java.util.Scanner;import java.io.*;import javax.swing.JOptionPane;public class Chapter6Project60 {	public static void main(String[] args) throws IOException {		int comCount = 0, eduCount = 0, govCount = 0, orgCount = 0;				File inputFile = new File ("websiteList.txt");//		Scanner scan = new Scanner(inputFile);				FileReader fileReader = new FileReader(inputFile);		BufferedReader bufferedReader = new BufferedReader(fileReader);		StringBuffer stringBuffer = new StringBuffer();		String line;		while ((line = bufferedReader.readLine()) != null) {			if (line.endsWith("com"))			{ 				comCount++;			}			else if(line.endsWith(".edu"))			{				eduCount++;			}			else if(line.endsWith("gov"))			{				govCount++;			}			else if(line.endsWith("org"))			{				orgCount++;			}			stringBuffer.append(line);			stringBuffer.append("\n");		}		fileReader.close();				JOptionPane.showMessageDialog(null,"Contents of file:\n"+ stringBuffer.toString());				JOptionPane.showMessageDialog(null, ".com: " + comCount + "\n.edu: " + eduCount						+ "\n.gov: " + govCount + "\n.org: " + orgCount);

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

import java.util.Scanner;import java.io.*;import javax.swing.JOptionPane;public class Chapter6Project60 {	public static void main(String[] args) throws IOException {		int comCount = 0, eduCount = 0, govCount = 0, orgCount = 0;				File inputFile = new File ("websiteList.txt");//		Scanner scan = new Scanner(inputFile);				FileReader fileReader = new FileReader(inputFile);		BufferedReader bufferedReader = new BufferedReader(fileReader);		StringBuffer stringBuffer = new StringBuffer();		String line;		while ((line = bufferedReader.readLine()) != null) {			if (line.endsWith("com"))			{ 				comCount++;			}			else if(line.endsWith(".edu"))			{				eduCount++;			}			else if(line.endsWith("gov"))			{				govCount++;			}			else if(line.endsWith("org"))			{				orgCount++;			}			stringBuffer.append(line);			stringBuffer.append("\n");		}		fileReader.close();				JOptionPane.showMessageDialog(null,"Contents of file:\n"+ stringBuffer.toString());				JOptionPane.showMessageDialog(null, ".com: " + comCount + "\n.edu: " + eduCount						+ "\n.gov: " + govCount + "\n.org: " + orgCount);

swap out that nested if then else with a switch.

Link to comment
Share on other sites

Link to post
Share on other sites

[browser glitched double post]

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Your solution is flawed. Consider: http://somesite.com/somepage

Basically just need another function to search for the FQDN. he'd need to use regex, if he can figure this out, i'd give him an A straight away, even if the rest of his code was borked.

Then just pass each line to that function and have it return the top level domain. you will need to also check for country codes being the top level domain as you may want get a <someuniversity>.edu.<contrycode> which wouldn't increase the count, but you may very well want it to.

Link to comment
Share on other sites

Link to post
Share on other sites

...you will need to also check for country codes being the top level domain as you may want get a <someuniversity>.edu.<contrycode> which wouldn't increase the count, but you may very well want it to.

It would probably be good to surface that as a configuration option.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Your solution is flawed. Consider: http://somesite.com/somepage

My teacher wouldn't throw something at us we couldn't do with what we've learned.

This solution is similar to the ones my classmates got.

For the specific purpose this is correct.

 

I could have used a switch statement, but I really prefer nested if/else for this, I'm sure none of my classmates code is identical to mine though.

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

My teacher wouldn't throw something at us we couldn't do with what we've learned.

 

No? Well I'd hazard to bet that:
  1. It would be worth a bonus
  2. It will come up as a question later

In education (especially higher) you are expected to excel, to demonstrate that you can be autonomous with researching and implementing above and beyond...

 

I could have used a switch statement, but I really prefer nested if/else for this...

 

You could have and you should have; as it is right now it is in fact what is called a 'code smell' i.e. it's crap.

 

Out of all of those statements though I think these are what piss me off the most:

 

This solution is similar to the ones my classmates got.

For the specific purpose this is correct.

 

... I'm sure none of my classmates code is identical to mine though.

 

Are you a drone? Are you content not giving a shit and doing 'only just good enough' quality work? Where is your enthusiasm and drive to do 'more'? I'm sure, as you said, none of your classmates code will be the same - some of them probably gave a shit and put some effort in.

 

You could do so much with this assignment and really have something that sets you apart if you would just put your mind to the task.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

 

No? Well I'd hazard to bet that:
  1. It would be worth a bonus
  2. It will come up as a question later

In education (especially higher) you are expected to excel, to demonstrate that you can be autonomous with researching and implementing above and beyond...

 

 

 

You could have and you should have; as it is right now it is in fact what is called a 'code smell' i.e. it's crap.

 

Out of all of those statements though I think these are what piss me off the most:

 
 

 

Are you a drone? Are you content not giving a shit and doing 'only just good enough' quality work? Where is your enthusiasm and drive to do 'more'? I'm sure, as you said, none of your classmates code will be the same - some of them probably gave a shit and put some effort in.

 

You could do so much with this assignment and really have something that sets you apart if you would just put your mind to the task.

 

Okay, I understand, no I'm not doing "good enough quality work" I'm doing exactly as I'm asked with the information that we have gone over thus far.

Could I have done better, absolutely, but I was pressed for time, this was due that day, and we talked about it in class, another person done it the way I did it, albeit the with the switch statement instead, and another did it a different way with a similar nested if/else. I'm not the only one doing it this way. It's simple enough and gets the job done.

 

Remember the Kiss method. The more complicated you make things, the harder they are to fix.

 

She doesn't give bonus points for creativity or anything like that either, I'm pretty sure she just runs the code to see if it works as intended and nothing else.

 

How is it "crap" if it does what it's intended to do? Sure there may be better ways to do it, but when it comes down to it, you click "run" and it displays the same dialog boxes.

 

This is the ninth week of a beginner class, not a senior level class I need to ace to get a doctorate.

 

I rewrote that section with a switch just to humor you though.

import java.util.Scanner;import java.io.*;import javax.swing.JOptionPane;public class Chapter6Project60 {	public static void main(String[] args) throws IOException {		int comCount = 0, eduCount = 0, govCount = 0, orgCount = 0;				File inputFile = new File ("websiteList.txt");		FileReader fileReader = new FileReader(inputFile);		BufferedReader bufferedReader = new BufferedReader(fileReader);		StringBuffer stringBuffer = new StringBuffer();		String line;		while ((line = bufferedReader.readLine()) != null) {			int length = line.length();			String suffix = line.substring(length - 3);			switch (suffix)			{			case "com" : comCount++	;			break;			case "edu" : eduCount++ ;			break;			case "org" : orgCount++ ;			break;			case "gov" : govCount++ ;			break;			}			stringBuffer.append(line);			stringBuffer.append("\n");		}		fileReader.close();				JOptionPane.showMessageDialog(null,"Contents of file:\n"+ stringBuffer.toString());				JOptionPane.showMessageDialog(null, ".com: " + comCount + "\n.edu: " + eduCount						+ "\n.gov: " + govCount + "\n.org: " + orgCount);}}

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

snip

 

snip

I usually agree with the point brought up by Nuluvius, but in this case I agree with the OP in that when you're in school and have so much stuff to do that sometimes you have to go over 24 hours without sleep (29 hours in my case yesterday), there's no room for excellence unless you want to kill yourself with a lack of sleep.

 

You have to make do with less-than-excellent work when the excellence of the work actually isn't important; in this case, a homework assignment in a beginner Java class is not an area where excellency will earn you any brownie points or critical experience. Especially when you know how to do it better and just can't spare the time. 

Link to comment
Share on other sites

Link to post
Share on other sites

I usually agree with the point brought up by Nuluvius, but in this case I agree with the OP in that when you're in school and have so much stuff to do that sometimes you have to go over 24 hours without sleep (29 hours in my case yesterday), there's no room for excellence unless you want to kill yourself with a lack of sleep.

 

You have to make do with less-than-excellent work when the excellence of the work actually isn't important; in this case, a homework assignment in a beginner Java class is not an area where excellency will earn you any brownie points or critical experience. Especially when you know how to do it better and just can't spare the time. 

Gonna be about 32 hours myself right now. I'm using this time to clean my kitchen, it's gotten rather nasty.

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

I usually agree with the point brought up by Nuluvius, but in this case I agree with the OP in that when you're in school and have so much stuff to do that sometimes you have to go over 24 hours without sleep (29 hours in my case yesterday), there's no room for excellence unless you want to kill yourself with a lack of sleep.

 

Okay, I understand, no I'm not doing "good enough quality work" I'm doing exactly as I'm asked with the information that we have gone over thus far.

Could I have done better, absolutely, but I was pressed for time, this was due that day, and we talked about it in class, another person done it the way I did it, albeit the with the switch statement instead, and another did it a different way with a similar nested if/else. I'm not the only one doing it this way. It's simple enough and gets the job done.

 

Gonna be about 32 hours myself right now. I'm using this time to clean my kitchen, it's gotten rather nasty.

 

It wasn't initially made clear that it was a compromise due to time. It came across, to me at least, as a lack of effort. However I do believe that whenever one is not faced with a compromise then putting in the extra is always worth it.

 

You have to make do with less-than-excellent work when the excellence of the work actually isn't important; in this case, a homework assignment in a beginner Java class is not an area where excellency will earn you any brownie points or critical experience. Especially when you know how to do it better and just can't spare the time. 

 

She doesn't give bonus points for creativity or anything like that either, I'm pretty sure she just runs the code to see if it works as intended and nothing else.

 

This is the ninth week of a beginner class, not a senior level class I need to ace to get a doctorate.

 

One should always start as they mean to go on and in doing so it will only ever become ingrained to then later manifest as a natural disposition towards attention to and pursuit of detail. You shouldn't ever allow anyone to demotivate you from putting all of yourself into a thing or allow yourself to ascribe to the mentality that it doesn't matter...

 

How is it "crap" if it does what it's intended to do? Sure there may be better ways to do it, but when it comes down to it, you click "run" and it displays the same dialog boxes.

 

It's crap based on the design, in other words that amount of if else is not considered good architecture.
 

Remember the Kiss method. The more complicated you make things, the harder they are to fix.

 

You don't have a handle on that concept yet (and that's perfectly fine at your level), if you did then above point would not have been raised because the design would have allowed for a reduction in the branching logic while at the same time making things more extensible. As for adding complexity, it wouldn't have done so at all, it would have actually removed it.

 

I rewrote that section with a switch just to humor you though.

 

You don't need to be concerned with humouring me, you should instead seek to humour that voice in the back of your skull that keeps saying: "That's not good enough, I can do better, I want more..." Or maybe there's only silence, I sometimes have a hard time telling with this current generation ya know  :lol:

The single biggest problem in communication is the illusion that it has taken place.

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

×