Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 16th, 2012, 12:04 AM
Dylskin Dylskin is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 1 Dylskin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 9 sec
Reputation Power: 0
Question New to java, error with initializing variable?

Hey I only started java today, working on a basic program using conditional operators.
The user enters their age, a messagebox opens telling them what age group they are in (under 18, 18-40, 40+)

This is my code:

Code:
import java.util.Scanner;
import javax.swing.JOptionPane;

public class FindAge {


    public static void main(String[] args) {
        String userage = JOptionPane.showInputDialog(userage);
        Integer.parseInt(userage);
        
        if (Integer.parseInt(userage) < 18) {
            JOptionPane.showMessageDialog(null, "You Are Under 18!");
        }
        else if (Integer.parseInt(userage) >= 18 && Integer.parseInt(userage) <= 40) {
            JOptionPane.showMessageDialog(null, "You Are Between 18 and 40!");
        }
    }
}



However, on the line
Code:
String userage = JOptionPane.showInputDialog(userage);


i get an error saying that the variable may not be initialized.
I would like to get this fixed asap so i can continue coding.

Any suggestions?

Reply With Quote
  #2  
Old April 23rd, 2012, 02:50 AM
sumathi sumathi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 1 sumathi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m
Reputation Power: 0
JOptionPane.showInputDialog(userage) -> Instead of userage use someother string.

JOptionPane.showInputDialog(userage); --> In this line userage is used without initializing. Use some other string For eg., String Title.

String title = "Enter Age : ";
String userage = JOptionPane.showInputDialog(title);

Reply With Quote
  #3  
Old January 4th, 2018, 06:57 AM
Hamza1 Hamza1 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2017
Posts: 10 Hamza1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m 29 sec
Reputation Power: 0
New to java, error with initializing variable?

I am getting the following error in my Java program:

But nan and r already initialized, yet I am still getting the same error.

public class cla {
int rn;
String name;

void get(String a, int x) {
name = a;
rn = x;
}

void display() {
System.out.println("student name: " + name + "roll no.:" + rn);
}
}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class child {
public static void main(String args[]) throws IOException {
String nam;
int r;
BufferedReader bReader = new BufferedReader(new InputStreamReader(
System.in));
try {
System.out.println("enter name and roll no.");
nam = bReader.readLine();
r = Integer.parseInt(bReader.readLine());

} catch (Exception e) {
}
cla c = new cla();
c.get(nam, r);
c.display();
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > New to java, error with initializing variable?


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap