
10-06-2008, 11:13 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 8
Rep Power: 0
|
|
reading in 2 sentence for java, I am lost ...
First of all i am using scanner class and string class, I have to ask the user how many scentence I have to use, witch that part is easy and have no poroblem with that, after that I enter a for loop and just ask the sentences, but the problem I am having is that:
public static void main(String [] args)
{
Scanner console = new Scanner(System.in);
int n;
String a;
String b;
System.out.print("How many sentence would you like to type? ");
n=console.nextInt();
for (int i=1;i<=n;i++)
{
System.out.print("Please Enter your "+i+" sentence: ");
if (i==1)
{ System.out.print("Please enter your 1 Number: ");
b.console.nextLine();
a=console.nextLine();
System.out.println(a);
}
else
a=console.nextLine();
System.out.println(a);
is the "a" string in the if statement when it outputs will not reconznie the "B" string and still prints out 2 lines, I jsut want it to print out 1 line, this particular code wont even print out b, it will go stright to a and print it 2 times in a row:
Please Enter your 1 sentence: Please enter your 1 Number: today is great
today is great
today is great
Please Enter your 2 sentence: 3as
3as
Please Enter your 3 sentence: f
f
Process completed. I just kind of trying to figure this out ... and im kind of lost, so please help me if you can.... the rest of them work, just the 1st sentence does not work, I have tried putting a.console.Next < --- and I also have tried to just put 1 string in there and it will not work right. Please Help me.
|
|

10-07-2008, 12:21 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
Some comments:
Add a unique comment at the beginning of all println()s so that you know which one printed the output.
Add a println() to force the output to a new line
The end of your program is missing.
|
Quote:
|
|
b.console.nextLine();
|
Does this compile?
|
|

10-07-2008, 05:37 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 8
Rep Power: 0
|
|
it does compile, but this is the code:
public static void main(String [] args)
{
Scanner console = new Scanner(System.in);
int n;
String a;
String b;
System.out.print("How many sentence would you like to type? ");
n=console.nextInt();
for (int i=1;i<=n;i++)
{
System.out.print("Please Enter your "+i+" sentence: ");
if (i==1)
{ System.out.print("Please enter your 1 Number: ");
a=console.nextLine();
System.out.println(a);
}
else
a=console.nextLine();
System.out.println(a);
and this is the output:
How many sentence would you like to type? 3
Please Enter your 1 sentence: Please enter your 1 Number:
Please Enter your 2 sentence: what happenend ...
what happenend ...
Please Enter your 3 sentence: today is great
today is great
Process completed.
trying to figure out why the 1st senctiece doesn't really work, I have tried adding another a=console.nextLine(); where the b.console.nextLine(); and hte output just will repeat the retarded senctences 2x, for example:
How many sentence would you like to type? 3
Please Enter your 1 sentence: Please enter your 1 Number: today is great
today is great
today is great
Please Enter your 2 sentence: what the f
what the f
Please Enter your 3 sentence: asdf
asdf
Process completed.
if someone could please help
|
|

10-07-2008, 02:46 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
Quote:
|
|
it does compile, but this is the code:
|
The end of the program is missing!!!!
Add a unique comment at the beginning of all println()s so that you know which one printed the output.
|
|

10-07-2008, 03:19 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 8
Rep Power: 0
|
|
|
import java.util.*;
public class Prog4test
{
public static void main(String [] args)
{
Scanner console = new Scanner(System.in);
int n;
String a;
String b;
System.out.print("How many sentence would you like to type? ");
n=console.nextInt();
for (int i=1;i<=n;i++)
{
System.out.print("Please Enter your "+i+" sentence: ");
if (i==1)
{ System.out.print("Please enter your 1 Number: ");
b=console.nextLine();
a=console.nextLine();
System.out.println(a);
}
else
a=console.nextLine();
System.out.println(a);
}
}
} thats the whole program
|
|

10-07-2008, 07:54 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
The ending else is missing {}.
Is the println() following a= console... part of the else or what?
It is better to ALWAYS use {} with if{}else{} statements.
Also don't put any code following a {. Leave the rest of the line blank.
What are the following three lines of code supposed to do?
|
Quote:
|
b=console.nextLine();
a=console.nextLine();
System.out.println(a);
|
What do you do with the contents of b?
|
|

10-07-2008, 11:42 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 8
Rep Power: 0
|
|
|
b=console.nextLine();
a=console.nextLine();
System.out.println(a);
basicly the b= is a dummy line,
I have to do a dummy line for it to actually read the whole thing ,but I am kinda lost on how to do a dummy line ..
|
|

10-08-2008, 12:50 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
What is a dummy line? Why do you need it? Perhaps if you commented the line:
b=console.nextLine(); // skip this line because .....
then everyone would know and not ask you about it.
You don't need an extra variable b to do that. The following would do just as well:
a=console.nextLine(); // skip this line because ...
a=console.nextLine(); // get the data I need here
|
|

10-08-2008, 05:39 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 8
Rep Power: 0
|
|
|
when I just have 1 a=console.nextLine(); it does not output right .. it out puts like this:
How many sentence would you like to type? 3
Please Enter your 1 sentence: Please enter your 1 Number:
Please Enter your 2 sentence: toda is
toda is
Please Enter your 3 sentence: grea d
grea d
Process completed.
wich is not right ... and then when I put in 3 it doesn't work either it prints 2
|
|

10-08-2008, 10:03 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
|
|
Yous should read the Java doc, about nextLine()
|
Code:
|
public class ReadUserInputs {
public static void main(String [] args) {
Scanner console = new Scanner(System.in);
int n;
String a, b; // Mutiple variables in same type declaration in simple way
System.out.print("How many sentence would you like to type? ");
n = console.nextInt(); // Number of user inputs
for(int i = 1; i <= n; i++) {
System.out.println("Please Enter your " + i + " sentence: ");
a = console.next(); // Read ith sengtence
// Print results.
System.out.println("Sentence is: " + a);
if (i == 1) { // I'm not clear what you are trying to do here
System.out.print("Please enter your 1 Number: ");
b = console.next();
System.out.println("Number is: " + b);
}
}
}
} |
|
|

10-08-2008, 04:01 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
You need to explain WHY it is not right.
What's wrong with it?
Look at Eranga's code and see how it is commented. Comments help others understand what the code is supposed to be doing.
Also see the use of "id strings" in the println()s:
System.out.println("Number is: " + b);
|
|

10-09-2008, 05:04 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
|
|
|
Again Norm, I think most of people doesn't read any materials before ask a question. I don't know how they going to learn new things in that way.
|
|

10-09-2008, 07:04 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 571
Rep Power: 2
|
|
Originally Posted by Eranga
|
|
I don't know how they going to learn new things in that way.
|
But reading and learning is hard. I think they expect enlightenment to fly in the window and make them smart.
I wish it as well, but its never worked that way.
I have to beat my head at new things for a while.
I think we need a FAQ or sticky that explains that before posting, you should try engaging brain.
|
|

10-09-2008, 07:54 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
|
|
Thanks for your thoughts fishtoprecords. Actually I really hate that. The way I learn is different. Read a lots of books and printed documents. Major reason to get that way is, in my child hood and the time I start to learn Java, actually programming, I don't have a computer, nor internet connection though. Simply I write codes in peace of paper and run it in my mind and see the result. Can you imagine how it is. That's my success.
|
|

10-09-2008, 03:31 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 8
Rep Power: 0
|
|
|
thanks guys for all your help, but I finally figured it out, to get it to do what I wanted I had to actually do a dummy line, so the nextInt would drop the \n < --- and the nextLine(), did not pick up the \n and not get some of my statement. Thanks again enjoyed reading all your comments
|
|

10-09-2008, 04:21 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
|
|
|
So that's means you didn't read the Java docs about those two. Why you need a dummy statement there? Do you have an answer for that?
|
|

10-10-2008, 01:21 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 8
Rep Power: 0
|
|
|
yes, the reason u needed a dummy statement is because when you are going from a .nextInt or just a .next inpute, it does not take the whole like for example, lets say u input, "Today is a great day" and the out put would be the same, but what is not showed is the \n < ------ the new line character, so it actaually reads, "Today is a great day \n" so basicly .next() or nextInt() will have the \n at the end. So when you do a nextLine(), the first thing it will read is the \n < ---- so if I type the same thing it will " is a great day" and skip Today, because of the \n line character. So basicly if you add a dummy satement wich can be done just by delcaring your scanner class once again. like lets say your class is called console, to do a dummy class you would just have to type console.nextLine();, so when you actually decide to input the real line, the dummy statement will take out the \n from the previous statement. so your statement will output right.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 04:51 PM.
|
|