Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    osufeth24 is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default [SOLVED] Help with inputing string text, etc

    I'm very new to Java and computer programming overall....

    We have to input a string of characters, for example, The Lord of the Rings and it's supposed to output it's length, then we input an index within the string, and it's supposed to output the character at that position and then we input another text string, and output the location of the first occurrence of the second string within the first one.

    I'm not entirely sure where to even begin with it. I've been reading through my book I have and online, but I can't seem to figure out how to make it so when I type in the sentence in the box it will spit out everything it needs. Thank you for any information!!

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Always try it out on a piece of paper first and write down the steps. This helps you to start to plan the program. Some pseudo-code would probably be:

    Java Code:
    [LIST][*]Ask the user to enter a string (use println)[*]User enter's first string (scanner class would work here)[*]count the number of characters in the string (strng length() method)[*]output the number of chracters to the screen (use println)[*]Ask the user to input an index from 0 to length of string (use println)[*]user enters the index number (porbably would be ggod to verify that hte entered number is in between the string's length) (use scanner class)[*]find the character at that index (string atChar() method)[*]output the character to screen (use println)[*]Ask the user for another string (use println)[*]user enters the second string (scanner class)[*]search the first string for the first occurance of the second string and reuturn the index (use string indexOf() method)[*]output the index to the screen[/LIST]
    Some good links to help you:
    Java Platform SE 6
    The Java™ Tutorials
    java.lang Examples from The Java Developers Almanac 1.4 (last section has string examples)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    osufeth24 is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    it's still all a bit confusing to me, but this is what i have right now...

    PHP Code:
    String s = keyboard.nextLine();		
    		
    		System.out.println("*** Test String operations ***\n");
    		System.out.println("Enter a string of charcters: ");
    and i think that's right so far. But i'm still confused on what i'm supposed to put so when I go to type in the input it spits out " The length of string "The Lord of the Rings" is 21". I'm pretty sure it has to do with stringSize and the char type

    ok, not sure if i really did anything, but i got it where i can put a input in, but when i type it in i got a error, i know theres something else that needs to go in, this is what i have..

    PHP Code:
    String s = keyboard.nextLine();		
    		int stringSize;
    		
    		System.out.println("*** Test String operations ***\n");
    		System.out.println("Enter a string of charcters: ");
    		stringSize = keyboard.nextInt();
    Last edited by osufeth24; 04-09-2009 at 10:11 PM.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default please post code and error

    Post your complete code (including the main)... not just a piece of it... Also, please post the complete error you're getting.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    osufeth24 is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    PHP Code:
    import java.util.Scanner;
    
    public class Lab2 {
    
    		
    	public static void main(String[] args) 
    	{
    		{
    		System.out.println("***************************************"
    );
    		System.out.println("* Welcome to your first Java Program! *");
    		System.out.println("***************************************\n");
    		
    		System.out.println("*** Test integer arithmic ***\n");
    		
    		int firstnum, secondnum, sum, difference, product, quotiant, modulus;
    		
    		Scanner keyboard = new Scanner(System.in);
    		
    		System.out.println("Enter first integer number: ");
    		firstnum = keyboard.nextInt();
    		System.out.println("Enter second integer number: ");
    		secondnum = keyboard.nextInt();
    		sum = firstnum + secondnum;
    		difference = firstnum - secondnum;
    		product = firstnum * secondnum;
    		quotiant = firstnum / secondnum; 
    		modulus = firstnum % secondnum;
    		
    		System.out.println(firstnum + "+" + secondnum + "=" + sum);
    		System.out.println(firstnum + "-" + secondnum + "=" + difference);
    		System.out.println(firstnum + "*" + secondnum + "=" + product);
    		System.out.println(firstnum + "/" + secondnum + "=" + quotiant);
    		System.out.println(firstnum + "%" + secondnum + "=" + modulus);
    		
    		// doubles test
    		double x, y, addition, subtraction, multiply, divide;
    	
    		System.out.println("*** Test real arithmic ***\n");		
    		System.out.println("Enter first real number: ");
    		x = keyboard.nextDouble();
    		System.out.println("Enter second real number: ");
    		y = keyboard.nextDouble();
    		addition = x + y;
    		subtraction = x - y;
    		multiply = x * y;
    		divide = x / y; 
    		
    		System.out.println(x + "+" + y + "=" + addition);
    		System.out.println(x + "-" + y + "=" + subtraction);
    		System.out.println(x + "*" + y + "=" + multiply);
    		System.out.println(x + "/" + y + "=" + divide);
    		
    		// string test
    		String s = keyboard.nextLine();		
    		keyboard.nextLine();
    		
    		System.out.println("*** Test String operations ***\n");
    		System.out.println("Enter a string of characters: ");
    Here's the output box

    PHP Code:
    Enter a string of characters: 
    The Lord of the Rings
    Exception in thread "main" java.util.InputMismatchException
    	at java.util.Scanner.throwFor(Unknown Source)
    	at java.util.Scanner.next(Unknown Source)
    	at java.util.Scanner.nextInt(Unknown Source)
    	at java.util.Scanner.nextInt(Unknown Source)
    	at Lab2.main(Lab2.java:62)

    the first 2 sections i have completed already, it's the string part i just can't figure out.

    this is what it's supposed to look like, the output that is

    Enter a string of characters: The Lord of the Rings
    The length of string "The Lord of the Rings" is 21
    Enter an integer between 0 and 20: 10
    The character at index 10 of string "The Lord of the Rings" is 'f'
    Enter another string of characters: Lord
    The first occurrence of string "Lord" in string "The Lord of the Rings" is at position 4

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Here we go...

    Java Code:
    // string test 
            String s = keyboard.nextLine();         
            keyboard.nextLine(); 
             
            System.out.println("*** Test String operations ***\n"); 
            System.out.println("Enter a string of characters: ");
    I would change the above to :
    Java Code:
    // string test 
            String strFirst = keyboard.nextLine();         
                   
            System.out.println("*** Test String operations ***\n"); 
            System.out.println("Enter a string of characters: "); 
     
            keyboard.nextLine();
    With the above you should have "The Lord of the Rings" in the strFirst variable.
    To find out the size of a string you have to use the String length() method. For example:
    Java Code:
    int strLength = myString.length();
    Apply this example to your variables and then you just have to print the int variable out to screen.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    osufeth24 is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    i'm sorry, but i'm still a bit confused on this, when i went to put in that code, i was able to punch it without an error, but nothing else is coming up, it's not giving me the length of the string

    Am I supposed to put the term "The Lord of the Rings" somewhere in the code?

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    please post what you did. I don't think that you should have troubles if you did the first two sections with numbers.

    CJSL
    Last edited by CJSLMAN; 04-09-2009 at 11:44 PM. Reason: typos...
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    osufeth24 is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    i first did some the changes you suggested

    PHP Code:
    String strFirst = keyboard.nextLine();
    		
    		System.out.println("*** Test String operations ***\n");
    		System.out.println("Enter a string of characters: ");
    		keyboard.nextLine();
    and I'm assuming then i have to do something else with the strFirst??

    I'm basically trying to teach myself here cause the teacher i have for the class i'm in doesn't really teach well at all, so this is why i'm really confused by all this, i figured the one i did with the numbers would be similar, but i just can't seem to grasp it

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    I apologize... the code I posted for you was not correct or complete... you're code should look like this:
    Java Code:
    // string test 
                           
            System.out.println("*** Test String operations ***\n"); 
            System.out.println("Enter a string of characters: "); 
     
            String strFirst = keyboard.nextLine();// strFirst will contain what ever you input... I think you want to put: The Lord of the Rings
    Now, to see if it really does have it there you can print it out to screen:
    Java Code:
    System.out.println("The string you entered is: " + strFirst);
    Again... I apologize... it's not my day.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Once the above is done, you can try the part about getting the length of the string using the length() method (I think that part that I posted is correct:-). In that example I didn't use the variables that your program is using. You're going to have to use you own variables.
    After that you have to ask the user for an index. You want to ask it in a way that it doesn't matter what the length of the first string is. Maybe something like:
    System.out.pirntln ("Please enter an index from 0 to " + strFirst.length());

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  12. #12
    osufeth24 is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    i'm not sure why i just can't seem to grasp this.. ok, i have the code here, and i can type in my input with an error

    PHP Code:
    System.out.println("*** Test String operations ***\n");
    		System.out.println("Enter a string of characters: ");
    		String strFirst = keyboard.nextLine();
    		keyboard.nextLine();
    now, to get the length, you said to use the string length() method.

    so them, i'm assuming i put the
    PHP Code:
    int strLength = myString.length();
    in the code somewhere?? and is the myString the variable that you used??

    I really appreciate the help though, i'm only in my 2nd week here of doing computer programming and i had no background prior to this.

  13. #13
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    OK.. I'm assuming that you didn't do the first sections, because it's almost the same but with string. You also are not reading carefully my posts.
    Java Code:
    System.out.println("*** Test String operations ***\n");
            System.out.println("Enter a string of characters: ");
            String strFirst = keyboard.nextLine();
            [B][COLOR="Red"]keyboard.nextLine();  [/COLOR][/B]
    Please remove the line in red.
    Now, in my past post (about using the length() method) I indicated the following:
    In that example I didn't use the variables that your program is using. You're going to have to use you own variables.
    So substitute the myString variable with your string variable (the one that the user entered).

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  14. #14
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Let me try to break this down for you.
    nextLine() is a method of the Scanner class. When you call it it querys the user for input, then returns the input as a String. Thus
    Java Code:
    String strFirst = keyboard.nextLine();
    calls the nextLine() method, which returns a String. The String then gets assigned to the strFirst variable.
    if you call
    Java Code:
    keyboard.nextLine();
    again, you are going to query the user again if you keep this statement.

    As for where you should put
    Java Code:
    int strLength = myString.length();
    Go through your program and decide where you need the string length and put the statement prior to the statement which references strLength
    I suggest you either put it directly after
    Java Code:
    String strFirst = keyboard.nextLine();
    or right before the statement which reference strLength.

    Mr.Beans

    EDIT: Sorry I'm a slow typer. Didn't mean to restate what CJSLMAN said. Started my post before I saw his.

  15. #15
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Mr Beans... no problem... I think I need some help anyway with this. Hopefully between us, we can help the OP understand the assignment.

    Thanks,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  16. #16
    osufeth24 is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    i did do the first sections on my own, i can figure those, but when i try to do it with strings, i can't seem to figure it out

    and when i remove that part in red, it no longer allows me to input anything output box.

    Thanks for you help though, i'll just keep looking around, trying different things out.


    I'm not sure if it needed to be explained or what you use for Java programming but we are using eclipse, and when i go to hit the run button when i take out that part in red, that you told me to take out, it not longer allows me to input anything
    Last edited by osufeth24; 04-10-2009 at 01:13 AM.

  17. #17
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    This is what I got.

    Java Code:
    import java.util.Scanner;
    
    public class Main {
    
    
        public static void main(String[] args)
        {
            System.out.println("***************************************");
            System.out.println("* Welcome to your first Java Program! *");
            System.out.println("***************************************\n");
    
            System.out.println("*** Test integer arithmic ***\n");
    
            int firstnum, secondnum, sum, difference, product, quotiant, modulus;
    
            Scanner keyboard = new Scanner(System.in);
    
            System.out.print("Enter first integer number:\t");
            firstnum = keyboard.nextInt();
            System.out.print("\n");
            System.out.print("Enter second integer number:\t");
            secondnum = keyboard.nextInt();
            System.out.print("\n");
            sum = firstnum + secondnum;
            difference = firstnum - secondnum;
            product = firstnum * secondnum;
            quotiant = firstnum / secondnum;
            modulus = firstnum % secondnum;
    
            System.out.println(firstnum + "+" + secondnum + "=" + sum);
            System.out.println(firstnum + "-" + secondnum + "=" + difference);
            System.out.println(firstnum + "*" + secondnum + "=" + product);
            System.out.println(firstnum + "/" + secondnum + "=" + quotiant);
            System.out.println(firstnum + "%" + secondnum + "=" + modulus);
    
            // doubles test
            double x, y, addition, subtraction, multiply, divide;
    
            System.out.println("*** Test real arithmic ***\n");
            System.out.print("Enter first real number:\t");
            x = keyboard.nextDouble();
            System.out.print("\n");
            System.out.print("Enter second real number:\t");
            y = keyboard.nextDouble();
            System.out.print("\n");
            addition = x + y;
            subtraction = x - y;
            multiply = x * y;
            divide = x / y;
    
            System.out.println(x + "+" + y + "=" + addition);
            System.out.println(x + "-" + y + "=" + subtraction);
            System.out.println(x + "*" + y + "=" + multiply);
            System.out.println(x + "/" + y + "=" + divide);
    
            // string test
            System.out.println("*** Test String operations ***\n");
            System.out.println("Enter a string of characters: ");
            String myStr = keyboard.next();
            System.out.println("Your string was " + myStr.length() + " characters long");
        }
    }
    The above code worked fine for me.
    Are there any parts of the code which you do not understand?

  18. #18
    osufeth24 is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    i think i understand it, but when i went to type in my input which is The Lord of the Rings it said it was only 3 characters long


    and to get this part

    The character at index 10 of string "The Lord of the Rings" is 'f'

    instead of mystr.length

    do i put mystr.chartAt(); ?

    ok, i got that part right now, but still can't figure out why it says it's only 3 characters long
    Last edited by osufeth24; 04-10-2009 at 01:53 AM.

  19. #19
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Try changing
    Java Code:
    String myStr = keyboard.next();
    to
    Java Code:
    keyboard.nextLine();
    String myStr = keyboard.nextLine();
    The length is only three because it only read in the "The"
    You can also always use
    Java Code:
    System.out.println(myStr);
    to find out what the String is.

  20. #20
    osufeth24 is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    I got it, when i went to go to input it in, i put underscores in like

    The_Lord_of_the_Rings and i got all the answers i needed.

    I have one more question then it's all completed, hopefully this isn't too difficult, but they want me to enter a birthday.

    so the output is supposed look like this
    PHP Code:
     Enter your birthday (mm/dd/yyyy): 10/25/1983
        You were born on 25-10-1983
    so they want it converted to the European style of how they do days. and we haven't even covered anything like this in classes, so i'm not sure how similar it is to what i just did or what this is pretty much all i have right now
    PHP Code:
    System.out.println("*** One last test ***\n");	
    		System.out.println("Enter your birthday (mm/dd/yyyy): ");

Page 1 of 2 12 LastLast

Similar Threads

  1. how to store string in text file
    By santhosh_el in forum AWT / Swing
    Replies: 2
    Last Post: 04-03-2009, 06:21 AM
  2. How to write a string middle of a text file?
    By loggen in forum New To Java
    Replies: 5
    Last Post: 12-19-2008, 08:48 AM
  3. Searching a string from a text file using Swing Buttons
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 2
    Last Post: 09-15-2008, 09:50 AM
  4. Searching a String from Text file using Swings .
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 4
    Last Post: 09-09-2008, 05:29 AM
  5. Replies: 1
    Last Post: 01-11-2008, 10:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •