Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-06-2008, 11:13 PM
Member
 
Join Date: Oct 2008
Posts: 8
Rep Power: 0
madatlis is on a distinguished road
Default 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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-07-2008, 12:21 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
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?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-07-2008, 05:37 AM
Member
 
Join Date: Oct 2008
Posts: 8
Rep Power: 0
madatlis is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-07-2008, 02:46 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-07-2008, 03:19 PM
Member
 
Join Date: Oct 2008
Posts: 8
Rep Power: 0
madatlis is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-07-2008, 07:54 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
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?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-07-2008, 11:42 PM
Member
 
Join Date: Oct 2008
Posts: 8
Rep Power: 0
madatlis is on a distinguished road
Default
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 ..
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-08-2008, 12:50 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 10-08-2008, 05:39 AM
Member
 
Join Date: Oct 2008
Posts: 8
Rep Power: 0
madatlis is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 10-08-2008, 10:03 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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);
            }
        }
    }
}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 10-08-2008, 04:01 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Quote:
wich is not right
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);
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 10-09-2008, 05:04 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 10-09-2008, 07:04 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 571
Rep Power: 2
fishtoprecords is on a distinguished road
Default
Originally Posted by Eranga View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 10-09-2008, 07:54 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 10-09-2008, 03:31 PM
Member
 
Join Date: Oct 2008
Posts: 8
Rep Power: 0
madatlis is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 10-09-2008, 04:21 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 10-10-2008, 01:21 PM
Member
 
Join Date: Oct 2008
Posts: 8
Rep Power: 0
madatlis is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
String/sentence to unicode convertion sandeepvreddy New To Java 5 11-20-2008 04:33 PM
Has Java lost its way? alexj44 Advanced Java 4 09-23-2008 02:26 PM
New to Java - totally lost as to what to download RR_QQ New To Java 1 09-04-2008 10:07 AM
I am so lost in my java class taliGLM New To Java 1 04-30-2008 11:02 PM
How to extract info from a sentence luisarca XML 1 06-07-2007 06:43 PM


All times are GMT +2. The time now is 04:51 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org