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 8 of 8

Thread: Beginner,first java program assignment

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner,first java program assignment

    Hey guys,I'm new to java programming and I am taking an introductory class to computer programming; and I am having trouble with our first assignment. Basically all I want my program to do is gather an integer of 5 digits,then after gathering said integer, change the 2nd and 4th numbers into zeros. This is what I have so far...I don't know how to make it change the 2 numbers.

     
     
    //Source listing for Assignment 1.
     
    import javax.swing.JOptionPane;
     
    public class Assignment1
     
     
    {
    	public static void main(String[] args)
    	{
    		int number,modnumber;	
    		String inputString;
     
    		//Accept the data.
     
    		inputString =
    		JOptionPane.showInputDialog ("Enter the number:");
     
    		number= Integer.parseInt (inputString);
     
    		modnumber=number;
     
     
     
    		System.out.println("The number entered is:" + number);
    		System.out.println("The modified number is:" + modnumber);
    	}
    }


  2. #2
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Beginner,first java program assignment

    Well the input is a string before you convert it back into a number. Why not use a String method to manipulate it just after input?

    One of the methods in this link to the Java API should do the trick. Use one that you have been taught.
    String (Java Platform SE 7 )

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Beginner,first java program assignment

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner,first java program assignment

    Oh sorry I did not know about that.I will use that for future posts.Also,thanks for your input Starstreak, I think I know what you are trying to tell me...but its still not quite clear.I'm gonna try to work on it for a bit.

    --- Update ---

    Yeah..I'm still having trouble with this.The only one that we've learned right now and that I think might be useful is .CharAt() but I can't seem to make it work.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Beginner,first java program assignment

    I can't seem to make it work.
    Post the code you are having problems with and explain what the problem is.
    Copy the full text of any error messages and post it here.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner,first java program assignment

    //Source listing for Assignment 1.
     
    import javax.swing.JOptionPane;
     
    public class Assignment1
     
     
    {
    	public static void main(String[] args)
    	{
    		int number,modnumber,modnum1,modnum2,int1;	//The number that the user wants the program to modify. In our case it is 98765; and its modified version.
    		String inputString;
    		int1=2;
     
     
     
     
     
    		//Accept the data.
     
    		inputString =
    		JOptionPane.showInputDialog ("Enter the number:");
     
    		modnum1=inputString.CharAt(int1);
     
     
     
     
    		number= Integer.parseInt (inputString);
     
     
     
     
     
     
     
     
     
    		modnumber=number-8000-60;
     
     
     
     
     
    		System.out.println("The number entered is:" + number);
    		System.out.println("The modified number is:" + modnumber);
    	}
    }

    Well,what I've been trying to do so far is make my program fetch the number in the second position of the integer and do that times 1000 in order for it to cancel itself out and make that spot equal to zero.However,m instructor gave the class a hint and said that we should use modulus...I don't know where to begin.Sorry I'm completely new to this.The error code I am getting right now is

    C:\360\Assignment1.java:24: error: cannot find symbol
    modnum1=inputString.CharAt(int1);
    ^
    symbol: method CharAt(int)
    location: variable inputString of type String
    1 error

    Tool completed with exit code 1

    Again,sorry if I don't completely understand.I hope I explained it as well as I could.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Beginner,first java program assignment

    java is case sensitive. Check your spelling.

    Method names begin with a lowercase letter.

    You will need to consider the differences between the character '1', the String "1" and the int 1. They are different. Look up the ASCII values table to see some of what I am talking about. You read the user's input into a String and extract a char from the String.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Jan 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner,first java program assignment

    Thanks for your help Norm,you solved the error that I was having,now I will try to finish writing the program.I will post anything if something happens.

Similar Threads

  1. Replies: 2
    Last Post: January 18th, 2013, 10:12 AM
  2. Help with beginner assignment
    By Jarruda in forum Object Oriented Programming
    Replies: 4
    Last Post: October 8th, 2012, 03:40 PM
  3. Java program (beginner) help?!
    By rk2010 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 10th, 2012, 12:30 PM
  4. Help with a beginner's java assignment: Survey results
    By lavloki in forum Java Theory & Questions
    Replies: 17
    Last Post: October 14th, 2010, 09:08 AM
  5. Beginner needs help with simple java assignment.
    By joachim89 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 6th, 2010, 06:53 PM