String program help

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 5303 Views - Last Post: 01 February 2010 - 08:41 PM Rate Topic: -----

#1 knox0811   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-January 10

String program help

Posted 01 February 2010 - 04:13 PM

I wrote a program for a homework assignment, for background purposes, here is what the assignment was supposed to consist of..

Write a program that asks the user to enter the name of his or her favorite city. Use a string variable to store the input. The program should display the following:
• The number of characters in the city name
• The name of the city in all uppercase letters
• The name of the city in all lowercase letters
• The first character in the name of the city

Here is the code I wrote..

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package stringmanipulator;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 *
 * @author Joseph_Barile
 */
public class Main {

	public Main() {
	}


	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) throws Exception {

		String CurLine="";
	System.out.println(" Please enter your favorite city. ");

		InputStreamReader converter = new InputStreamReader(System.in);
		 BufferedReader in = new BufferedReader(converter);


	 
		  CurLine = in.readLine();
		  System.out.println(" Number of Character in City : "+CurLine.length());
		  System.out.println(" Name of the City (UC) : "+CurLine.toUpperCase());
		  System.out.println(" Name of the City (LC) : "+CurLine.toLowerCase());
		  System.out.println(" First Character of the City : "+CurLine.substring(0, 1));

		

			
	  }

	}



I am new to Java and NetBeans.. What can I clean up or take out of this code?

Also I am getting an error on the line

public class Main {


It says this.. class main is public, should be declared in a file named Main.java

How do I fix this?

Is This A Good Question/Topic? 0
  • +

Replies To: String program help

#2 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: String program help

Posted 01 February 2010 - 04:19 PM

The name of the public class you declare should be the same as the .java file that it's stored in.

Test.java --> public class Test
Calculator.java --> public class Calculator

But your code is spot on. The only thing you can really do is use a Scanner instead of a BufferedReader, but that won't change the functionality (assuming valid user input). It would only change the code.
Was This Post Helpful? 0
  • +
  • -

#3 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: String program help

Posted 01 February 2010 - 04:19 PM

create a new File named Main.java, and there write the code.
what do you mean by take out or clean?

Locke beat me to it :)

This post has been edited by japanir: 01 February 2010 - 04:19 PM

Was This Post Helpful? 0
  • +
  • -

#4 knox0811   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-January 10

Re: String program help

Posted 01 February 2010 - 04:23 PM

So I can name it anything other then Main?
Was This Post Helpful? 0
  • +
  • -

#5 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: String program help

Posted 01 February 2010 - 04:24 PM

You can name it whatever you want. Just make sure your file name and public class name match up. ;)

This post has been edited by Locke: 01 February 2010 - 04:24 PM

Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: String program help

Posted 01 February 2010 - 04:27 PM

Yes, but in public classes, the name of the file must always be the same as the Filename.java

edit: didn't see that Locke was here. Sorry Locke!

This post has been edited by Dogstopper: 01 February 2010 - 04:29 PM

Was This Post Helpful? 0
  • +
  • -

#7 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: String program help

Posted 01 February 2010 - 04:53 PM

View PostDogstopper, on 1 Feb, 2010 - 05:27 PM, said:

edit: didn't see that Locke was here. Sorry Locke!


S'okay. :D
Was This Post Helpful? 0
  • +
  • -

#8 knox0811   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-January 10

Re: String program help

Posted 01 February 2010 - 04:55 PM

When I run the program I get this..

run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal start of expression
at stringmanipulator.Main.main(Main.java:19)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

What does this mean??
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: String program help

Posted 01 February 2010 - 05:48 PM

It means that there is an illegal command on line 19. Can you post the revised code so we can help you debug it?
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: String program help

Posted 01 February 2010 - 05:58 PM

Actually, macosxnerd101, in his particular case, it means that he is trying to run either uncompiled code, or a java file. I ran it, and it worked just fine.

@OP, try recompiling the code and running the class.
javac Main.java
java Main



OR if you are using Netbeans, make sure to rebuild the file before attempting to run it.
Was This Post Helpful? 0
  • +
  • -

#11 knox0811   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-January 10

Re: String program help

Posted 01 February 2010 - 08:21 PM

View Postmacosxnerd101, on 1 Feb, 2010 - 04:48 PM, said:

It means that there is an illegal command on line 19. Can you post the revised code so we can help you debug it?


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package stringmanipulator;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 *
 * @author Joseph_Barile
 */
public class Main {

	public Main() {
	}


	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) throws Exception {

		String CurLine="";
	System.out.println(" Please enter your favorite city. ");

		InputStreamReader converter = new InputStreamReader(System.in);
		 BufferedReader in = new BufferedReader(converter);



		  CurLine = in.readLine();
		  System.out.println(" Number of Character in City : "+CurLine.length());
		  System.out.println(" Name of the City (UC) : "+CurLine.toUpperCase());
		  System.out.println(" Name of the City (LC) : "+CurLine.toLowerCase());
		  System.out.println(" First Character of the City : "+CurLine.substring(0, 1));




	  }

	}


Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: String program help

Posted 01 February 2010 - 08:23 PM

View PostDogstopper, on 1 Feb, 2010 - 06:58 PM, said:

...
@OP, try recompiling the code and running the class.
javac Main.java
java Main



OR if you are using Netbeans, make sure to rebuild the file before attempting to run it.


Did you read my post? Your code works fine!
Was This Post Helpful? 0
  • +
  • -

#13 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: String program help

Posted 01 February 2010 - 08:26 PM

Compiles and runs:

Quote

Please enter your favorite city.
Washington
Number of Character in City : 10
Name of the City (UC) : WASHINGTON
Name of the City (LC) : washington
First Character of the City : W

Was This Post Helpful? 0
  • +
  • -

#14 knox0811   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-January 10

Re: String program help

Posted 01 February 2010 - 08:28 PM

I'm still getting the same error.. In NetBeans, it gives me the option to click on the line that has the error. and it is directing me to a totally different .java file that I'm not even working with at that time. How can I fix this?
Was This Post Helpful? 0
  • +
  • -

#15 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: String program help

Posted 01 February 2010 - 08:33 PM

I ran it in Netbeans...Wow...Ignore the highlight for now. Try to clean and build (F11) and if that is successful, then run it. However, if it is taking you to another Java file, I can believe that You do not have the JDK properly installed. If those steps don't work then please give the name of the java file.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2