Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    1
    Last edited by redbull; September 17th, 2012 at 03:56 PM. Reason: I Ffed up


  2. #2
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    reply to me at honestsportstalk@gmail.com or here.. i will be checking this ever hour.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    problem that I am having is that it is only collecting the first word of the input.
    If you are only entering one word before pressing Enter, use the nextLine() method to read the input from the user.

    For a test with the current code, try typing both words on the same line before pressing Enter. next() should read one and then the other.

    Scanner methods can be tricky. The Scanner buffers input and can block waiting for input.

    For example if you enter: A word to the wise <PRESS ENTER>
    and use next() only "A" is read, and "word to the wise" is left in the buffer.
    Your next attempt to get something from Scanner will be to get "word".

    To clear the buffer, use the nextLine() method.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    thanks norm, i editted it. and no, nextLine() completely skips my input and essentially closes my program. I need a "next" string that will take the whole entire input at g = ps.next(); .... and not just the first word... cause the user was supposed to enter two words... i get that the e.contains(g); catches those two words at string 'g' and compares it to string 'e'. If those two words (really the first word, as it is only reading the first word) is in string e, then the codes executes in the 'if' statement. if not, the error message pops off.... So basically I'm asking you, how do you do this loop or get the next word in the scanner.. I get that all the words the user inputs goes in a "pool". The first word in the "pool" is taken and inputted, and the rest in the "pool" remains there to be retrieved... Or maybe when you press Enter it clears the other words in this "pool". I am not sure.

    But after I figure out a way for the two words to be inputted in the code, and not just one, i guess I would have to do a 'greater than' or 'less than' statement.... because string 'g' takes two words from string 'e' (via the user entering them). And I am supposed to print which word appears first of the two, in the initial string 'e'. So somehow I would have to get the integer values of the two words in respect to their positions in string 'e', and assign them to integers or strings. and do a 'if a>b print a, else print b' or something like that. HELPPPPPPP. how do I get the codes to take both words in the input using the string.next(); and not just the first word!!!!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    nextLine() completely skips my input and essentially closes my program
    Please explain how that happens. Do you get any error messages?
    Can you post the code where that happens and copy the console from when you executed the code.
    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    nextLine() reads a whole line.
    next() reads the next token.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    Quote Originally Posted by Norm View Post
    Please explain how that happens. Do you get any error messages?
    Can you post the code where that happens and copy the console from when you executed the code.
    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    nextLine() reads a whole line.
    next() reads the next token.
    Hello, I changed it from g = ps.next(); to g = ps.nextLine(); Like before, the program does not ever wait for me to input anything, and skips to the next line, which I coded to close out the program.. Here is a copy:

    <C:\Users\Red Bull\Documents\Documents\School\Programming\Programs>java BestClass
     
    The best class ever,,, and the best dxy ever
    The best class ever, and the best day ever.
     
    Type any word to see if it is in the string:
    if
    The string does not contain your word!
     
    The second word capitalized in the previous string makes this string:
    The Best class ever, and the best day ever.
     
    A new string with the 'r' removed looks like this:
    The Best class eve , and the best day eve .
     
    The third word capitalized in the previous string makes this string:
    The best Class ever, and the best day ever.
     
    The last word capitalized in the previous string makes this string:
    The best class ever, and the best day Ever.
     
    Step 4 commas is replaced using the substring method:
    The best class ever, and the best dxy ever
     
    Step 5 'dxy' is replaced with 'day' using the substring method:
    The best class ever, and the best day ever
     
    Pick two words out of the sentence and type it, separating it by a space:
     
     
    C:\Users\Red Bull\Documents\Documents\School\Programming\Programs>>

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    there has got to be a loop or something that I can use to retrieve the other word in that input.... I'm beginning to hate this professor

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    You need to enhance your usage of println() to show what is in the String that was read. What if the String returned by nextLine() was empty? Try this:
    		System.out.println("g="+g+"<");

    Your problem is the buffering that Scanner does. next() does not remove the newline character from the buffer. When nextLine() is called it returns an empty line if the buffer has a newline.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    nope... doesn't work. When I use nextLine() , it still skips the input part, and it prints g=< , most likely interpreting that "g="+g+"<" literally . If I don't use nextLine(), and instead use next() , it still takes the first word only, and prints the "g="+g+"<" literally also...

  10. #10
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    <C:\Users\Red Bull\Documents\Documents\School\Programming\Programs>java BestClass
     
    The best class ever,,, and the best dxy ever
    The best class ever, and the best day ever.
     
    Type any word to see if it is in the string:
    any
    The string does not contain your word!
     
    The second word capitalized in the previous string makes this string:
    The Best class ever, and the best day ever.
     
    A new string with the 'r' removed looks like this:
    The Best class eve , and the best day eve .
     
    The third word capitalized in the previous string makes this string:
    The best Class ever, and the best day ever.
     
    The last word capitalized in the previous string makes this string:
    The best class ever, and the best day Ever.
     
    Step 4 commas is replaced using the substring method:
    The best class ever, and the best dxy ever
     
    Step 5 'dxy' is replaced with 'day' using the substring method:
    The best class ever, and the best day ever
     
    Pick two words out of the sentence and type it, separating it by a space:
    g=<
     
    C:\Users\Red Bull\Documents\Documents\School\Programming\Programs>>

    It prints g=< and still doesn't wait for me to input. If I change ps.nextLine() to ps.next(), it'll print my first word and g=<

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    Looks like an empty String was read into the g variable(between the = and the <). What method was used to read g?

    If the code has called the next() method, there is a newline left in Scanner's buffer.
    Call nextLine() to read that from the buffer before using nextLine() to read a whole line.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    Quote Originally Posted by Norm View Post
    Looks like an empty String was read into the g variable(between the = and the <). What method was used to read g?

    If the code has called the next() method, there is a newline left in Scanner's buffer.
    Call nextLine() to read that from the buffer before using nextLine() to read a whole line.
    MUHAHAHAHAHHAHAH BUAHAHAHAHAH MUAHAHAHAH HAHAHA BUHAHAHA MUHAHAHA I HAVE FIGURED IT OUT. AND YES, THANKS FOR YOUR HELP MEN. WITH THE NEXTLINE() PICKING UP THE NEXT WORD IN THE INPUT, MY INSTRUCTOR DID NOT SHOW US THIS AND EXPECTED US TO FIND IT OUT. I KNOW HE DID THAT ON PURPOSE SO THAT WE WOULD HAVE TO GO ONLINE AND FIGURE IT OUT. AND I PROMISE YOU, THIS WILL NOT BE THE LAST TIME THAT I WOULD VISIT THIS WEBSITE. YOU GUYS ARE BETTER THAN STACKOVERFLOW.COM . THEY WERE LAUGHING AND SH**T AT ME...

    HERE'S WHAT I DID... I DID USE G =NEXTLINE() AFTTTTEERRRR I USED G = NEXT() , AND I ASSIGNED BOTH OF THEM TO DIFFERENT STRINGS... THEN I USED THE INDEXOF() TO GET BOTH OF THEIR POSITIONS... THEN USING A SIMPLE BOOLEAN IF/ELSE STATEMENT, WHICH EVER WAS LOWER IN INTEGER I WOULD PRINT THAT STRING(OR WORD)... I HAD TO TRIM THE SECOND WORD BECAUSE OF THE SPACE... I WILL POST NOW... HOPEFULLY NO ONE AT MY SCHOOL COMES ON THIS WEBSITE AND STEALS MY CODE... OR I MIGHT BE MORE INCLINED TO GO COLUMBINE (OK.... I DIDN'T MEAN THAT. PLEASE DON'T REPORT ME.... IT WAS JUST A METAPHOR... I AM SANE)...

    <import java.util.Scanner;
     
    public class BestClass
    {
    	public static void main(String[] args)
    	{
    		//Create two strings and and int for the position count in string.
    		String a,b,f,g,h,i;
    		int position,position1,position2,position3,position4,position5,position6;
    		a = "The best class ever,,, ";
    		b = "and the best dxy ever";
     
    		Scanner ps = new Scanner(System.in);
     
    		//Concatenate the two strings and assign it to variable 'd'.
    		System.out.println(a + b);
    		String d = a + b;
     
    		//Replace the ",,," in String d with "," . Assign it to String e, add a period, and print.
    		String c = d.replace(",,,",",");
    		String e = c.replace("dxy", "day") + ".";
    		System.out.println(e);
     
    		System.out.println();
     
    		//Ask user to type word to check if it is in string
    		System.out.println("Type any word to see if it is in the string: ");
    		f = ps.next();
     
    		//Condition statements.
    		if (e.contains(f))
    		{
    			System.out.println("The string contains your word!");
    		}
     
    		else
    		{
    			System.out.println("The string does not contain your word!");
    		}
     
    		//Line separator.
    		System.out.println();
     
    		//Collect int position in string 'e', just before the word "best"
    		position = e.indexOf("best");
     
    		//First three strings divide the string 'e' into 3 positions.
    		String s1 = e.substring(0,position);
    		String s2 = e.substring(position,position + 1);
    		String s3 = e.substring(position + 1);
     
    		//Switches the first character in string 's2' to uppercase. Then concatenates three strings and prints
    		String s4 = s2.toUpperCase();
    		System.out.println("The second word capitalized in the previous string makes this string: " + "\n" + s1 + s4 + s3);
    		String s5 = s1 + s4 + s3;
     
    		System.out.println();
     
    		//Removes all 'r's' in previous statement, and replaces it with blank space. Then prints statement.
    		String s6 = s5.replaceAll("r", " ");
    		System.out.println("A new string with the \'r\' removed looks like this: " + "\n" + s6);
     
    		System.out.println();
     
    		//Declare the integer
    		position1 = e.indexOf("class");
     
    		//Create three strings of string 'e'
    		String s7 = e.substring(0,position1);
    		String s8 = e.substring(position1,position1 + 1);
    		String s9 = e.substring(position1 + 1);
     
    		//Switches the first character in string 's8' to uppercase. Then concatenates three strings and prints
    		String s10 = s8.toUpperCase();
    		System.out.println("The third word capitalized in the previous string makes this string: " + "\n" + s7 + s10 + s9);
     
    		System.out.println();
     
    		//Declare the integer for position2
    		position2 = e.lastIndexOf("ever");
     
    		//Create three strings of string 'e'
    		String s11 = e.substring(0,position2);
    		String s12 = e.substring(position2,position2 + 1);
    		String s13 = e.substring(position2 + 1);
     
    		//Switches the first character in string 's12' to uppercase. Then concatenates three strings and prints
    		String s14 = s12.toUpperCase();
    		System.out.println("The last word capitalized in the previous string makes this string: " + "\n" + s11 + s14 + s13);
     
    		System.out.println();
     
    		//Declare the integer for position3
    		position3 = d.indexOf(",,,");
     
    		//Create three strings of 'd'
    		String s15 = d.substring(0,position3);
    		String s16 = d.substring(position3,position3 + 3);
    		String s17 = d.substring(position3 + 3);
     
    		//Switches the ",,," in string d with ","
    		System.out.println("Step 4 commas is replaced using the substring method: " + "\n" + s15 + "," + s17);
     
    		System.out.println();
     
    		//Declare the integer for position4
    		position4 = c.indexOf("dxy");
     
    		//Create three strings of 'c'
    		String s18 = c.substring(0,position4);
    		String s19 = c.substring(position4,position4 + 3);
    		String s20 = c.substring(position4 + 3);
     
    		//Switches the "dxy" in string c with "day"
    		System.out.println("Step 5 'dxy' is replaced with 'day' using the substring method: " + "\n" + s18 + "day" + s20);
     
    		System.out.println();
     
    		//Ask user to input two words, and check it against the String e. If it exists, print which word appears first as it would in String e
    		System.out.println("Pick two words out of the sentence and type it, separating it by a space: ");
     
    		//Take response of first word in input
    		g = ps.next();
     
    		//Take response of second word in input, and trim to eliminate space
    		h = ps.nextLine();
    		i = h.trim();
     
    		//Get integer position of two words
    		position5 = e.indexOf(g);
    		position6 = e.indexOf(i);
     
    		//If and else statement to determine which word comes first, then print word that occurs first in String e
    		if (e.contains(g) && e.contains(h))
    		{
    			if (position5>position6)
    				System.out.println(i);
     
    			else
    				System.out.println(g);
    		}
     
    		else
    		{
    			System.out.println("The string does not contain your words!");
    		}
     
    	}
    }>

    AND HERE IS HOW IT OUTPUTS:

    <C:\Users\Red Bull\Documents\Documents\School\Programming\Programs>java BestClass
     
    The best class ever,,, and the best dxy ever
    The best class ever, and the best day ever.
     
    Type any word to see if it is in the string:
    ever
    The string contains your word!
     
    The second word capitalized in the previous string makes this string:
    The Best class ever, and the best day ever.
     
    A new string with the 'r' removed looks like this:
    The Best class eve , and the best day eve .
     
    The third word capitalized in the previous string makes this string:
    The best Class ever, and the best day ever.
     
    The last word capitalized in the previous string makes this string:
    The best class ever, and the best day Ever.
     
    Step 4 commas is replaced using the substring method:
    The best class ever, and the best dxy ever
     
    Step 5 'dxy' is replaced with 'day' using the substring method:
    The best class ever, and the best day ever
     
    Pick two words out of the sentence and type it, separating it by a space:
    ever the
    ever
     
    C:\Users\Red Bull\Documents\Documents\School\Programming\Programs>>

  13. #13
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    IIIIIIIIIII AMMMMMMMMMMMMMM A GENIUSSSSSSSSSSSSSSSSSSSSSSSSS MUAHHAHAHA BUAHAHA BUHAHAHH AMUHAHAHHAHA. I AM GREATTTTTTTTTTTTTTTT. MUHAHHAHAHA HAHAHHA.... MOVE OVER BILL GATES, MOVE OVER SUCKERBERG... IIII RED BULLL AM DA BESTTTTTTTTTT PROGRAMMER. HAHAHHAHAHAHHAA... GENIUSSSSSS. HAHAHHAHAHAHAHHA RESPECT ME NOWWWWW

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    You need to have your PC's keyboard checked. It's Caps lock is stuck on.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    dam... found another logic error, lol... it reads "The" and "the" as two different words.... help me men... there is like a java string that would ignore the case...

  16. #16
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    how do I use the ignorecase while using the boolean string.contains? or do I have to a make an if/else statement like "if string is capitalized, switch to lowercase"???

  17. #17
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    Hey Norm, did I piss you off?

  18. #18
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    I HAVE FIGUREEEDDD IT OUTTTT. I AMM GREATTT. just a simple e = e.toLowerCase(); before I start collect the user's input from string's g and h/i.... HAHAHAHAHHAHA. some1 gimme a high five... now i got a harder assignment now... i gotta write a program to calculate equations using the ax+by=c , where a and b are constants (1-99 integers, and i do print the 1), and x and y are obviously variables (integers, maybe I'll use double or float).... I am going to work on this tomorrow, but i'd probably need ure guys help... Piece out

Similar Threads

  1. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  2. [SOLVED] "possible loss of precision", except not, code doesn't work, simple question
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 07:11 PM
  3. Replies: 1
    Last Post: March 31st, 2010, 09:42 PM
  4. Replies: 2
    Last Post: October 29th, 2009, 06:13 PM
  5. Drawing "Hello world" on screen
    By shikh_albelad in forum Java SE APIs
    Replies: 4
    Last Post: June 11th, 2009, 03:21 AM

Tags for this Thread