Jump to content

Mehoy Minoy

Member
  • Posts

    10
  • Joined

  • Last visited

Awards

This user doesn't have any awards

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

Mehoy Minoy's Achievements

  1. So im 100% sure that my problem is due to the way its written because im new to programming. So im trying to get my program to read from a .txt file and count how many punctuation marks and symbols and whatnot are in the .txt file and then use if statements to add to counters, which eventually all print at the bottom of the code. Yet since im still a novice I'm not sure why I'm not able to get it to add to the counters or read from the .txt file. Any help would be appreciated. Also yes i know my code is probably innefficient and outdated, also im not aloud to use a do while or for loop or anything like arrays or multiple classes for this. import java.io.*; import java.util.Scanner; public class PP5_15 { public static void main(String []args) throws IOException { String punctuation; Scanner fileScan,punctuationScan; fileScan=new Scanner(new File("Chp5.txt")); punctuationScan=new Scanner(new File("Chp5.txt")); //Impliment the portion of code here that will read in the "Chp5.txt" file int leftParCounter, rightParCounter, commaCounter, periodCounter, excCounter, questionCounter,vowelCounter,numCounter; //Creating counter variables to use to track punctuations in txt file leftParCounter=0; rightParCounter=0; commaCounter=0; periodCounter=0; excCounter=0; questionCounter=0; vowelCounter=0; numCounter=0; //Set all counter variables to 1 for now //punctuation=punctuationScan.nextLine(); while(fileScan.hasNext()) { punctuation=fileScan.nextLine(); punctuationScan=new Scanner(punctuation); punctuationScan.useDelimiter("");//Delimiter to seperate what the pointer is aiming at. if(punctuation=="(") { leftParCounter++; } if(punctuation==")") { rightParCounter++; } if(punctuation==",") { commaCounter++; } if(punctuation==".") { periodCounter++; } if(punctuation=="!") { excCounter++; } if(punctuation=="?") { questionCounter++; } /*if(punctuation=="a"||"e"||"i"||"o"||"u"||"A"||"E"||"I"||"O"||"U") { vowelCounter++; } if(punctuation=="0"||"1"||"2"||"3"||"4"||"5"||"6"||"7"||"8"||"9") { numCounter++; }*/ } System.out.println("There are "+leftParCounter+" left parentheses."); System.out.println("There are "+rightParCounter+" right parentheses."); System.out.println("There are "+commaCounter+" commas."); System.out.println("There are "+periodCounter+" periods."); System.out.println("There are "+excCounter+" exclamation points."); System.out.println("There are "+questionCounter+" question marks."); System.out.println("There are "+vowelCounter+" vowels."); System.out.println("There are "+numCounter+" numbers."); } }
  2. Oh ok. And also i super appreciate the code it helped me understand what I should be doing but the only problem is that since im only a couple units through a basic java class were not aloud to use things like arrays, if/else/for statements, loops, or even multiple classses(at least for this project). He wants it all in 1 class and were not aloud to use those aforementioned things but believe me if i were aloud to use multiple classes and some if/else statements I would've been done with this project after a couple hours.
  3. See but even when I comment that entire portion of my code out it doesn't seem to do anything to my problem
  4. So idk if that portion of the code is part of my problem cause it seems to work fine and also when i comment it out i still get my main problem from above. However i was trying to add all of the "Quality Points" together but since theyre in string form i added them all together in string form and then converted them from string to an int value. I think i need to change it to convert it from string to float or double later but i think for the most part ill be able to get that to work i just need to figure out why its asking for course number and course credits input at the same time.
  5. So im having issues with a simple-ish program im writing for class and ive got everything done with this part of the project however ive run into a ditch. I cant figure out why my code will print and ask for input twice for the 2nd, 3rd, and 4th scan.nextLine() and scan.nextByte() parts when it is assigning course number and course credits to variables. Ill comment next to the part of the code im talking about. Anyways i just need it to ask for them 1 at a time so it can assign them instead of asking for both of them and then i can finish my code. Honestly dont worry about any of the code past the part that im commenting next to I dont think it will really matter to you what that is. And yes i realize this code may not be very efficient and I probably use outdated methods and whatnot, teacher restricts us to certain content we can use for now. import java.util.Scanner; public class Part1 { public static void main (String []args) { Scanner scan=new Scanner(System.in); System.out.println("Hello, Welcome to the GPA calculator"); String firstName, lastName, majorDegree, courseNum1, courseNum2, courseNum3, courseNum4; byte courseCredits1, courseCredits2, courseCredits3, courseCredits4; String qPoints1, qPoints2, qPoints3, qPoints4; double calc1_1, calc1_2, calc1_3, calc1_4; System.out.println("Please enter your first name."); firstName=scan.nextLine(); System.out.println("Please enter your last name."); lastName=scan.nextLine(); System.out.println("What is your Major?"); majorDegree=scan.nextLine(); System.out.println("Please enter the course number for your first class."); courseNum1=scan.nextLine(); System.out.println("How many credits was this first course worth?"); courseCredits1=scan.nextByte(); { System.out.println(" QUALITY POINTS:\n _________________________ \n A 4.00 A- 3.67 \n B+ 3.33 B 3.00 B- 2.67 \n C+ 2.33 C 2.00 C- 1.67 \n D+ 1.33 D 1.00 \n F 0.00"); } System.out.println("Please enter exactly how many Quality points you received for your first class based on the chart above."); qPoints1=scan.next(); String numConvert1= qPoints1; float finalNum1 = Float.parseFloat(numConvert1); calc1_1= finalNum1*courseCredits1; System.out.println("Please enter the course number for your second class."); //This is where im having trouble..................................................................................................................................................................... courseNum2=scan.nextLine(); System.out.println("How many credits was this second course worth?"); //This is also where im having trouble...................................................................................................................................................................... courseCredits2=scan.nextByte(); System.out.println(" QUALITY POINTS:\n _________________________ \n A 4.00 A- 3.67 \n B+ 3.33 B 3.00 B- 2.67 \n C+ 2.33 C 2.00 C- 1.67 \n D+ 1.33 D 1.00 \n F 0.00"); System.out.println("Please enter exactly how many Quality points you received for your second class based on the chart above."); qPoints2=scan.next(); String numConvert2= qPoints2; float finalNum2 = Float.parseFloat(numConvert2); calc1_2= finalNum2*courseCredits2; System.out.println("Please enter the course number for your third class."); courseNum3= scan.nextLine(); System.out.println("How many credits was this third course worth?"); courseCredits3= scan.nextByte(); { System.out.println(" QUALITY POINTS:\n _________________________ \n A 4.00 A- 3.67 \n B+ 3.33 B 3.00 B- 2.67 \n C+ 2.33 C 2.00 C- 1.67 \n D+ 1.33 D 1.00 \n F 0.00"); } System.out.println("Please enter exactly how many Quality points you received for your third class based on the chart above."); qPoints3=scan.next(); String numConvert3= qPoints3; float finalNum3 = Float.parseFloat(numConvert3); calc1_3= finalNum3*courseCredits3; System.out.println("Please enter the course number for your fourth class."); courseNum4= scan.nextLine(); System.out.println("How many credits was this fourth course worth?"); courseCredits4= scan.nextByte(); { System.out.println(" QUALITY POINTS:\n _________________________ \n A 4.00 A- 3.67 \n B+ 3.33 B 3.00 B- 2.67 \n C+ 2.33 C 2.00 C- 1.67 \n D+ 1.33 D 1.00 \n F 0.00"); } System.out.println("Please enter exactly how many Quality points you received for your fourth class based on the chart above."); qPoints4=scan.next(); String numConvert4= qPoints4; float finalNum4 = Float.parseFloat(numConvert4); calc1_4= finalNum4*courseCredits4; String totalQualityPoints= qPoints1+qPoints2+qPoints3+qPoints4; int finalTotalQPoints= Integer.parseInt(totalQualityPoints); int totalCourseCredits= courseCredits1+courseCredits2+courseCredits3+courseCredits4; int gpa= finalTotalQPoints/totalCourseCredits; } }
  6. Damnit i feel like an idiot. I did what you said but to no avail and it kept returning with a runtime error saying something about java.lang.NumberFormatException and i eventually figured out it was due to how i was scanning in qPoints1 with scan.nextLine() and just ended up changing it to scan.next() and so it works now. Thanks for the help though appreciate it.
  7. So im doing a project for a class and and having just a little issue with a part of my code, towards the bottom you will see that I am trying to create a couple lines of code that will just convert one of the previous strings to a double value and for some reason im not sure what it is. If someone could help point it out that would be great. Also im relatively new to coding so dont judge me, i know some of it is inefficient and some of it may be outdated. Anyways heres the code: import java.util.Scanner; public class Part1 { public static void main (String []args) { Scanner scan=new Scanner(System.in); System.out.println("Hello, Welcome to the GPA calculator"); String firstName, lastName, majorDegree, courseNum1, courseNum2, courseNum3, courseNum4; byte courseCredits1, courseCredits2, courseCredits3, courseCredits4; String qPoints1, qPoints2, qPoints3, qPoints4; System.out.println("Please enter your first name."); firstName=scan.nextLine(); System.out.println("Please enter your last name."); lastName=scan.nextLine(); System.out.println("What is your Major?"); majorDegree=scan.nextLine(); System.out.println("Please enter the course number for your first class."); courseNum1=scan.nextLine(); System.out.println("How many credits was this first course worth?"); courseCredits1=scan.nextByte(); { System.out.println(" QUALITY POINTS:\n _________________________ \n A 4.00 A- 3.67 \n B+ 3.33 B 3.00 B- 2.67 \n C+ 2.33 C 2.00 C- 1.67 \n D+ 1.33 D 1.00 \n F 0.00"); } System.out.println("Please enter how many Quality points you recieved for your first class based on the chart above."); qPoints1=scan.nextLine(); double doublePoints1; doublePoints1= Double.valueOf(qPoints1); System.out.println("The number is: " + doublePoints1); } }
  8. Sweet thank you it worked. Didn't work the other day when i tried it for some reason. Also yea i would use something more up to date but were required to use whats in the certain chapters of the book. I'm sure eventually ill start using something more relevant but in the meantime I'm stuck with some out of date methods as well as Dr. Java which has quite a few problems and is a little out dated.
  9. Im having trouble with my java code and am making an applet for class that is just supposed to make a simple picture. I have everything working and compiled but it needs to have my name printed in the top left corner and for some reason I cant get it to work. The last 3 lines of actual code is what I need help with as I just cant get it to print the name. import javax.swing.JApplet; import java.awt.*; public class PP2_18 extends JApplet { public void paint(Graphics myapp) { myapp.setColor(Color.white); myapp.fillRect(0,0,5000,5000); Color grassGreen = new Color(0,255,85);//Made a new sky and grass color. Others were too bright. Color skyBlue = new Color(0,200,255); myapp.setColor(skyBlue); myapp.fillRect(0,0,5000,5000); myapp.setColor(grassGreen); myapp.fillRect(0,450,5000,5000); myapp.setColor(Color.lightGray); myapp.fillRect(285,275,200,200);//Main part of the house myapp.setColor(Color.black); myapp.fillRect(360,400,50,75);//The Door myapp.setColor(Color.white); myapp.fillOval(400,440,10,10);//The Door handle myapp.setColor(Color.white);//Window Frames myapp.fillRect(300,320,50,60); myapp.fillRect(415,320,50,60); myapp.setColor(Color.black);//Window frame crossbars myapp.fillRect(320,320,5,60); myapp.fillRect(440,320,5,60); myapp.fillRect(300,350,50,5); myapp.fillRect(415,350,50,5); myapp.setColor(Color.lightGray);//Chimney Smoke. Put smoke before chimney so it didnt overlap chimney. myapp.fillOval(430,120,50,75); myapp.fillOval(450,100,50,70); myapp.fillOval(470,80,50,70); myapp.setColor(Color.red);//Chimney myapp.fillRect(425,190,50,75); myapp.setColor(Color.black);//Roof myapp.fillRect(285,250,200,40);//Layer 1 myapp.fillRect(310,230,150,40);//Layer 2 myapp.fillRect(340,210,100,40);//Layer 3 myapp.setColor(Color.lightGray);//Clouds myapp.fillOval(800,100,200,100); myapp.fillOval(900,150,200,100); myapp.fillOval(900,90,200,100);//Last line for cloud 1(Left) myapp.fillOval(2000,180,200,100); myapp.fillOval(2100,180,200,100); myapp.fillOval(2100,130,200,100);//Last line of cloud 2(Right) Color brown = new Color(165,42,42);//Made brown to use on the tree myapp.setColor(brown); myapp.fillRect(1300,500,100,400); Color leafGreen = new Color(0,155,0);//Made green for the tree leaves myapp.setColor(leafGreen); myapp.fillOval(1150,500,200,150); myapp.fillOval(1250,400,200,150); myapp.fillOval(1350,500,200,150); myapp.fillOval(1150,450,200,150); myapp.fillOval(1250,500,200,150); myapp.fillOval(1400,500,200,150); myapp.fillOval(1350,400,200,150); myapp.setColor(brown); myapp.fillOval(2310,810,60,75);//Outer part of turtle legs myapp.fillOval(2475,810,60,75); myapp.fillOval(2295,690,260,170);//Outer part of turtle body myapp.setColor(leafGreen); myapp.fillOval(2300,700,250,150);//Turtle Body myapp.fillOval(2230,700,100,100);//Turtle Head myapp.fillOval(2315,810,50,65);//Inner part of turtle leg myapp.fillOval(2480,810,50,65);//Inner part of turtle leg myapp.setColor(Color.black); myapp.fillOval(2250,720,10,10);//Turtle eye myapp.fillRect(2235,765,30,5);//Turtle mouth myapp.setColor(brown); myapp.fillOval(2350,720,50,30);//Turtle spots myapp.fillOval(2370,780,50,30); myapp.fillOval(2450,740,50,30); myapp.fillOval(2430,750,50,30); myapp.fillOval(2430,790,50,30); myapp.setColor(Color.black); String name = new String("Name"); myapp.drawString(name,0,0); } } If you could help that would be great, thanks
×