473,654 Members | 3,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Java Assignment

1 New Member
Hello I am new but I have been in a intro programming class with Java as the main entree. I have this program where I need to end input characters at 65 characters and break the line and then add a new line.

This is my code:

import static java.lang.Syste m.*;
import java.util.Scann er;

class formatter3 {

public static void main (String[] args) {
Scanner stdin = new Scanner (System.in);
int maxlen = 65;
int outlen = 0;
int spaces = 0;
while (stdin.hasNext ()) {
String word = stdin.next();
System.out.prin tf(word); ++(spaces);
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
}
}

}


and i get these error messages when i try to complie:

bash-2.03$ javac formatter3.java
formatter3.java :14: cannot find symbol
symbol : variable length
location: class java.lang.Strin g
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
^
formatter3.java :14: operator + cannot be applied to int,java.lang.S tring.length
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
^
formatter3.java :14: operator > cannot be applied to <nulltype>,in t
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
^
formatter3.java :14: cannot find symbol
symbol : method printf(java.lan g.String)
location: class formatter3
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
^
4 errors


Waaahhhh!!!! any suggestions would be helpful. please I've been searching aorund campus for a community that does java and could help out and let me be a part, but santa cruz is HIPPY! x_x
Jul 19 '07 #1
8 2435
Atli
5,058 Recognized Expert Expert
Hi, and welcome to TSDN!

You have come to the right place my friend!
Let me just move this question over to the Java forums, where our Java experts are more likely to find it.

Moderator
Jul 20 '07 #2
adarcorerlreth
1 New Member
1. you need to import java.lang.Strin g class

code:
import java.lang.Strin g;

2. there is nop printf() in Java, you need to use:

System.out.prin tln("");

think that should fix it

Hello I am new but I have been in a intro programming class with Java as the main entree. I have this program where I need to end input characters at 65 characters and break the line and then add a new line.

This is my code:

import static java.lang.Syste m.*;
import java.util.Scann er;

class formatter3 {

public static void main (String[] args) {
Scanner stdin = new Scanner (System.in);
int maxlen = 65;
int outlen = 0;
int spaces = 0;
while (stdin.hasNext ()) {
String word = stdin.next();
System.out.prin tf(word); ++(spaces);
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
}
}

}


and i get these error messages when i try to complie:

bash-2.03$ javac formatter3.java
formatter3.java :14: cannot find symbol
symbol : variable length
location: class java.lang.Strin g
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
^
formatter3.java :14: operator + cannot be applied to int,java.lang.S tring.length
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
^
formatter3.java :14: operator > cannot be applied to <nulltype>,in t
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
^
formatter3.java :14: cannot find symbol
symbol : method printf(java.lan g.String)
location: class formatter3
if ((outlen + spaces + word.length) > maxlen) printf ("%n");
^
4 errors


Waaahhhh!!!! any suggestions would be helpful. please I've been searching aorund campus for a community that does java and could help out and let me be a part, but santa cruz is HIPPY! x_x
Jul 20 '07 #3
prometheuzz
197 Recognized Expert New Member
1. you need to import java.lang.Strin g class

code:
import java.lang.Strin g;
No everything from the java.lang package is automatically imported.


2. there is nop printf() in Java, you need to use:

System.out.prin tln("");

think that should fix it
Yes there is:
http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.htm l#printf(java.u til.Locale,%20j ava.lang.String ,%20java.lang.O bject...)
and since the OP imported System.out with a static import, this should work:

Expand|Select|Wrap|Line Numbers
  1. out.printf("%n"); // the 'out' must be in front of it!
But the real problem is the fact that the OP is trying to access the length of the String as if it was a public attribute, like this:

Expand|Select|Wrap|Line Numbers
  1. int i = aString.length;
but the method length() should be called:

Expand|Select|Wrap|Line Numbers
  1. int i = aString.length();
Jul 20 '07 #4
nickyeng
254 Contributor
word.length

if want to get word's length...you should call

word.length()
Jul 20 '07 #5
prometheuzz
197 Recognized Expert New Member
word.length

if want to get word's length...you should call

word.length()
How is that different from my answer?
Jul 20 '07 #6
JosAH
11,448 Recognized Expert MVP
How is that different from my answer?
Your answer was first and his answer wasn't?

kind regards

Jos ;-)
Jul 20 '07 #7
prometheuzz
197 Recognized Expert New Member
Your answer was first and his answer wasn't?

kind regards

Jos ;-)
No, his answer was last and mine wasn't!
; )
Jul 20 '07 #8
JosAH
11,448 Recognized Expert MVP
No, his answer was last and mine wasn't!
; )
We'd call it an 'ex equo' then and split the prize!

kind regards,

Jos ;-)
Jul 20 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1343
by: Mondo Dynamo | last post by:
A friend of mine is learning java as part of a Business Studies Degree and is finding it very hard to get to grips with. She has an assignment coming up soon and needs some help, she is offering a pretty unique reward, if you are interested please visit http://www.mondodynamo.com/hazeljava.htm Thanks in advance for any help you can offer. --
458
21132
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers simply aren't smart enough to understand C++? This is not merely a whimsical hypothesis. Given my experience with Java programmers --- the code they write and the conversations they have --- Occam's Razor points to this explanation. For example,...
51
3673
by: erikcw | last post by:
DiveIntoPython.org was the first book I read on python, and I really got a lot out of it. I need to start learning Java (to maintain a project I've inherited), and was wondering if anyone knew of any similar books for Java? Maybe once I know my way around the language, I can sneak Jython in... :-) Thanks! Erik
0
7110
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past the due date 10% of the grade is taken off. I think I'm coming down with the flu and my brain is just not processing this assignment. Here is the assignment: Modify the Inventory program by adding a button to the GUI that allows the user to move...
4
2499
by: Tigger2000 | last post by:
I keep getting an error when trying to compile this for Java prog class, below is the instructions for the assignment and my code. Loop Assignment Write, compile, and run a Java program that demonstrates the following: #1. Uses ach off number down to zero, except the number 3 and 33. Program must use a continue and an if statement. #2. Uses a while loop to count down from 30 to zero, printing each number, except 27 until the...
6
4641
by: penny | last post by:
In this assignment we shall look at a possible representation of rational numbers in java using objects. The java language represents rational numbers using the same representation used for other real numbers. This is the floating-point representation. However as we may all know, floating-point numbers are quite inaccurate. This means that ½ might actually be represented as 0.49998, which may not be good enough for some applications. In this...
2
1491
by: toprahman | last post by:
Hi guys, I'm a student teacher at a high school in Washington D.C. and I've come across a slight problem. See, I'm a math major, but one of the computer science teachers just quit and they need me to fill the position. I have to write this program out by tomorrow and I've tried to learn basic java, but I have no idea how to do this assignment. Basically, I need an answer key for this class and the teacher before didn't leave one. I'm expected to...
29
2335
by: s0suk3 | last post by:
Hello, I was hoping to get some opinions on a subject. I've been programming Python for almost two years now. Recently I learned Perl, but frankly I'm not very comfortable with it. Now I want to move on two either Java or C++, but I'm not sure which. Which one do you think is a softer transition for a Python programmer? Which one do you think will educate me the best?
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8709
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8494
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.