0

Hi there,

I'm currently a student in an intro to Java course at the college level. I've been doing well all semester, but I've ran into trouble with our latest lab. It's a bonus lab, so naturally the content wasn't covered in class. After playing around with what I found in my textbook, I have a start but not much else.

Basically, it's a string analysis assignment and I'm sure its easy for most programmers. I'm a telecom student taking Java as opposed to a computer programming student taking java, so this stuff is a little more difficult for me.

After asking the user to enter some text and storing that text to a string variable while using a scanner object, I need to be able to output the following:

* The number of keystrokes in the string.
* The number of letters in the string.
* The number of upper-case and lower-case letters in the string.
* The number of digits in the string.
* The number of even and odd digits in the string.

So far, all I can do is output the number of keystrokes in the string. This is my program so far. I've been putting some serious time into this, but like I said, programming certainly doesn't come naturally for me. Otherwise I wouldn't be asking for help.

import java.util.*;
public class BonusLab 
{
	public static void main(String[] args) 
	{
		
		Scanner keysIn = new Scanner(System.in);
		System.out.print("Enter some text: ");
		String str = keysIn.next();

		System.out.println();
		System.out.println("String Analysis:");
		System.out.println("Keystrokes: " + str.length());
	
	}

}

Any feedback would be much appreciated and thanks in advance
Rich

5
Contributors
4
Replies
9
Views
7 Years
Discussion Span
Last Post by JamesCherrill
0

You are correct programming doesn't come naturally at the beginning. You must try and try at your best. see Java documentation to study more functions. http://java.sun.com/j2se/1.4.2/docs/api/

import java.util.Scanner;

class test
{
	static int getDigits(String str)
	{
		int count=0;
		for(int i=0;i<str.length();i++)
		{
			if(str.charAt(i)>=48 && str.charAt(i)<=57)
				count++;
		}
		return count;
	}

	public static void main(String args[])
	{
		String s;
		Scanner sc=new Scanner(System.in);
		s=sc.next();
		System.out.println(getDigits(s));
	}
}
0

Im also new to Java and stuck on arrays and needing help on this first portion of my program. i want the user to enter the 20 numbers in one line opposed to asking them 20 times how I have it in this loop. I don't know if I need the user to separate the numbers by commas or not.

1.import java.util.Scanner;
2.
3.public class NumberAnalysis
4.{
5.   public static void main(String[] args)
6.   {
7.      Scanner keyboard = new Scanner( System.in );
8.        int[] nums = new int[20];                                
9.
10.        for( i = 0; i < 20; i++ )                                
11.        {
12.            System.out.println( "Enter a series of 20 numbers:" );
13.            nums[i] = keyboard.nextDouble();
14.
15.        }
16.        for( i = 0; i < 20; i++ )
17.        {
18.            System.out.print( nums[i] + " " );  
19.        }
20.        System.out.println();       
21.   }
22.}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.