10 Replies - 1610 Views - Last Post: 21 May 2012 - 11:18 PM Rate Topic: -----

#1 toad87  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 197
  • Joined: 21-May 12

Beginner Java homework help please.

Posted 21 May 2012 - 01:33 AM

Attached is a pdf of my homework assignment. It is supposed to be terribly easy. It's basically about object interaction. I completely understand object interaction (objects creating objects/external method calls) but the problem I am having with this assignment is the Scanner utility...

I have to make a "StringPlay" class that displays the users inputs, counts amount of letters, capitalizes all the letters, and lowercases all the letters.

My teacher provided us with a reader/return type class:
import java.util.Scanner;
/**
 * Class InputReader reads user input from the keyboard.
 * @version 2009.10.25
 * @author Colleen Penrowley
 */
public class InputReader
{
    private Scanner scanner;

    /**
     * Create a new InputReader to read user input.
     */
    public InputReader()
    {
        scanner = new Scanner(System.in); // for keyboard input

    }
    /**
     * @return the user's input as a String
     */
    public String getInput()
    {

        return scanner.nextLine();
        
    }

    
    
}

I created the class that will play around with the return and display it:


/**
 * Write a description of class StringPlay here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class StringPlay
{
    // instance variables - replace the example below with your own
    private InputReader reader;

    /**
     * Constructor for objects of class StringPlay
     */
    public StringPlay()
    {
        reader = new InputReader();

    }

    /**
     *
     */
    public void playWithStrings()
    {

        System.out.println("Type a sentence: ");
        //local variable "userInput"
        String userInput = reader.getInput();
        System.out.println("You typed: " + userInput );
        
       

        System.out.print("Your sentence has 22 letters.");
        System.out.print("Here it is uppercase: WHAT SORT OF SENTENCE?");
        System.out.print("Here it is lowercase: what sort of sentence?");

        
    }
}


Ok so I got it to return the input, but how am I supposed to count the number letters, cap, and lower case the input?



The problem is the Scanner method doesn't have these methods.

However, playing around with the Reader class, I found that if I called the getInput method it would return and I could created a new object with the return with variables that contain the so and so amount of characters and the methods of that object is able to lowercase and uppercase the string!

I have no idea how that object was created and I have no idea how to implement it into my StringPlay class...

Any explanation would be greatly appreciated...thanks...my head hurts from this...

This post has been edited by macosxnerd101: 21 May 2012 - 12:56 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Beginner Java homework help please.

#2 Hikaroshi  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 42
  • Joined: 26-April 09

Re: Beginner Java homework help please.

Posted 21 May 2012 - 03:43 AM

I didn't understand the last bit you've said.
You're going to get a scolding for not putting your code in proper tags.

I can try my best to help you out, though.

http://docs.oracle.c...ang/String.html Hm... tell me if something here looks like something you'd like to use.

Scanner's job is mainly to read input if I recall...
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12278
  • View blog
  • Posts: 45,364
  • Joined: 27-December 08

Re: Beginner Java homework help please.

Posted 21 May 2012 - 12:57 PM

You may also find the Character.isLetter() method helpful.
docs.oracle.com/javase/6/docs/api/java/lang/Character.html
Was This Post Helpful? 0
  • +
  • -

#4 toad87  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 197
  • Joined: 21-May 12

Re: Beginner Java homework help please.

Posted 21 May 2012 - 09:29 PM

View PostHikaroshi, on 21 May 2012 - 03:43 AM, said:

I didn't understand the last bit you've said.
You're going to get a scolding for not putting your code in proper tags.

I can try my best to help you out, though.

http://docs.oracle.c...ang/String.html Hm... tell me if something here looks like something you'd like to use.

Scanner's job is mainly to read input if I recall...


I'll try to explain better, I was quite tired and stressed last night. After creating an InputReader object, calling the getInput method, and typing in an input; I receive a "return" box ( I should probably mention I am using BlueJ to write this code). On the return box, there is a button that says "get". When clicking that button, I am able to create a new object called "string1". Upon inspecting, string1, I can see that it possesses all all the qualities of the input I created. It shows the number of characters and it also has like 100 different methods to play around with the string (including capitalizing all the letters and lowercasing them).

Now this string1 object is confusing to me. What is it? How do I use it to do what I need to do? I feel that this is key to my little assignment here. Unfortunately, my textbook has said nothing about it yet and my professor is one of those that kind of expects you to figure things out yourself.

I hope I cleared things up.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12278
  • View blog
  • Posts: 45,364
  • Joined: 27-December 08

Re: Beginner Java homework help please.

Posted 21 May 2012 - 09:31 PM

See the methods listed in the String class, with the documentation provided. Specifically the toUpperCase() and toLowerCase() methods.
Was This Post Helpful? 0
  • +
  • -

#6 toad87  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 197
  • Joined: 21-May 12

Re: Beginner Java homework help please.

Posted 21 May 2012 - 09:45 PM

I just can't seem to figure out how to call upon that "string1" object method that counts and returns the number of characters in the inputted string as an integer, the method that caps all the letters, and the method that lowercases all the letters.

I searched the Scanner methods and can't seem to find any methods that can help me out with what I need. Arrrgh! This is driving me crazy! It makes me feel like a total idiot because this is just an introductory programming course. It shouldn't be this hard!
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12278
  • View blog
  • Posts: 45,364
  • Joined: 27-December 08

Re: Beginner Java homework help please.

Posted 21 May 2012 - 09:47 PM

An example on invoking the length() method. This syntax should help you get started.
String one = "abc";
int length = one.length(); //returns 3


Was This Post Helpful? 0
  • +
  • -

#8 toad87  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 197
  • Joined: 21-May 12

Re: Beginner Java homework help please.

Posted 21 May 2012 - 10:38 PM

OK~!! So I figured out how to create the string object in my method! lol. Pretty easy once I figured out String is actually a class.

But now I am having trouble with the methods. lengthen(), toUpperCase(), and toLowerCase should return an int, String, and String value respectively. But it is not returning anything on my terminal screen...take a look at my code:


public void playWithStrings()
    {

        System.out.print("Type a sentence: "  );
        //local variable "userInput"
        String userInput = reader.getInput();
        String string1 = new String();
        System.out.println("You typed: " + userInput );
        System.out.println("Your sentence has " + string1.length() +  " letters.");
        System.out.println("Here it is in all uppercase: " + string1.toUpperCase());
        System.out.println("Here it is in all lowercase: " + string1.toLowerCase());

    }


See how I created the new string? hehe I'm actually pretty proud of myself for figuring that one out. Anyways, I implemented the methods into my System.out.print's but they are not showing up!

The length always comes up as 0. And nothing even appears for the upper and lower case ones...

I am stumped!!!
Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Beginner Java homework help please.

Posted 21 May 2012 - 10:48 PM

Quote

The length always comes up as 0. And nothing even appears for the upper and lower case ones...

string1 is a reference to "newly" allocated memory (hence the new operator). Did you initialize string1 to any value? And what do you think the purpose is of string1 if the string that you are dealing with is the one read from the user's input?

example:

String a = new String();
int len_a = a.length(); //length = 0 since it initializes to an empty string

String b = new String("abc");
int len_b = b.length(); //length = 3 since it is initialized to "abc"



Hint: You want the length of the user input string, so don't you think you want to call userInput.length() ?

This post has been edited by jjl: 21 May 2012 - 10:51 PM

Was This Post Helpful? 0
  • +
  • -

#10 toad87  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 197
  • Joined: 21-May 12

Re: Beginner Java homework help please.

Posted 21 May 2012 - 10:57 PM

OMG I did it! MUuhahahah!

Just to let you guys know, all I had to do was put my local variable, "userInput" as a parameter for the string class I created...hahah so simple!

Thanks so much for your guys' help!

You guys seriously opened up my mind a bit and made me thing a bit. It always helps to bounce ideas off people.

Thanks again!
Was This Post Helpful? 1
  • +
  • -

#11 toad87  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 197
  • Joined: 21-May 12

Re: Beginner Java homework help please.

Posted 21 May 2012 - 11:18 PM

Oh jjl,

I didn't even see that you had posted the solution to my answer before I had figured it out myself lol.

Thanks so much man! Your time is very much appreciated.

You rock!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1